Lab 1 (10 points)
 January 19-20, 2010



Laboratory Assignment #1 —
Getting Started

Due: at 11:59 pm on the day of your lab session

Abstract

Write two C programs, compile, and run them in a Linux shell on CCC systems.

Outcomes

After successfully completing this assignment, you should be able to:–

·        Develop a C program on a Linux platform

·        Read in and print out numbers using formatting strings

·        Do ordinary calculations using floating point arithmetic

Before Starting                       

Read Chapters 1 and 2 of The C Programming Language, 2nd edition, by Kernighan and Ritchie, or Chapter 2 of C: How to Program, 5th edition, by Deitel and Deitel. (Throughout the course, these will be referred to as K&R or D&D.)

Part 1 of this assignment is either the “Hello, world!” program of page 7 of K&R or the “Welcome to C” program of Figure 2.1 of D&D.

Part 2 of this assignment is a simple arithmetic computation. Before coming to the lab, write out your proposed solution to Part 2!

Getting Started

1.      Sign the attendance sheet.

2.      If XWin32 is running on your computer, there will be a blue X-monitor icon in the system tray at the lower right hand corner of your screen. If XWin32 is not running you must start it on your computer by clicking:–

Start ® All Programs ® Utilities ® XWin32 9.0 ® X-Win32

XWin32 is a terminal application for Windows computers; it allows Windows users to connect to Linux servers on a local network or via the Internet. X applications running on those servers will be displayed onto the Windows desktop. There are two reasons why we run XWin32 when connecting to Linux:–

·        to enable copy/paste between Linux windows and other windows, and

·        to allow an incoming connection to be accepted. It is this feature that allows you to run emacs, kwrite, and other programs on the CCC servers so that their windows show up on your desktop.

3.      Log onto the Linux system using your WPI username and password:–

·        Double-click the PuTTY icon (or start PuTTY from the Start menu)

·        Type ccc.wpi.edu in the connection window and click OK

·        Enter your CCC Linux username and password

4.      Create a directory called cs2303 and subdirectory called lab1; make lab1 your working directory.

5.      Using emacs, kwrite, or your favorite editor, type out the “Hello, world” program or the “Welcome to C” program exactly as it appears in the respective textbook. Call your resulting program lab1a.c. A copy of the “Hello World” program appears here.

6.      In the Linux shell, compile the program with the command

  gcc -Wall lab1a.c

The -Wall “switch” means “Warnings: all”; it instructs the gcc compiler to display all warning messages. If you find discrepancies, make changes and recompile. If you have difficulty, consult a classmate or one of the TAs. When you can compile with no errors, run the program with the Linux command

./a.out

7.      Experiment with inducing compilation errors. For example, remove the semicolon at the end of line starting with printf and recompile. Note that error messages in C usually appear one or more lines after the actual error. Moreover, a single error can cause the appearance of a zillion false errors after it.

Also experiment with warnings. Remove the zero after the return. Compile using the Wall switch as in Step 6 and then again without the Wall the switch. In this course, all programs must compile free of warnings (i.e., using the Wall the switch) unless an exception is allowed by the Professor.

8.      Submit this assignment using the turnin system. From your CCC Linux shell, submit your files using the following turnin command:–

/cs/bin/turnin submit cs2303 LAB1 lab1a.c

Be sure to submit your program by 11:59 PM on the date of your lab session in order to get credit for this part of the lab.

Part 2: A simple numerical calculation

1.      Write a C program called lab1b.c to prompt the user for the x- and y-coordinates of the three corners of a triangle, calculate and print the lengths of the three sides, calculate and print the circumference of the triangle, and calculate and print the area of the triangle. The following mathematical formulas may be used in these calculations:–

The formula for determining the length lAB of the line between points (xA, yA) and (xB, yB) is

.

If lAB, lBC, and lCA are the lengths of the three sides of a triangle, then the area of the triangle is

where s is one-half the circumference of the triangle – i.e., s = ½ ´ (lAB + lBC + lCA).

The following illustrates a sample of the program output and input. The program prompts are shown as non-bold and do not end with a new-line character, while the user responses are shown as bold and do end with a new-line character. All result lines end with the new-line character.

Enter the x- and y-coordinates of point A:- 1.05 -2
Enter the x- and y-coordinates of B:- 1.115e+2 21.1
Enter the x- and y-coordinates of point C:- -25 -3.14159
 
Length of AB is 112.840
Length of BC is 138.636
Length of CA is 26.075
Circumference is 277.551
Area is 237.833

The function sqrt() is declared in the interface <math.h>. It returns the square root of its argument. The argument of sqrt() must any non-negative numerical value, and the result of sqrt() is of type double. If the argument is negative, sqrt() fails with an error and the result is undefined. (The program may or may not crash.)

The printf() function is declared in the interface <stdio.h>. It takes the following form:–

printf("string in double quotes", arg1, arg2, arg3, )

The string, which is enclosed in double quotes, contains zero or more conversion specifiers. Each conversion specifier begins with a '%' character and specifies how to convert the corresponding argument to printed characters. The ith conversion specifier corresponds to the ith argument following the string. For example, a conversion specifier of '%f' says to treat the corresponding argument as a floating point number and print it with the default precision – e.g., 3.14159. See Table 7-1 of K&R or Chapter 9 of D&D for more details.

The scanf() function is declared in the interface <stdio.h>, and takes the following form:–

scanf("string in double quotes", &arg1, …)

The string, enclosed in double quotes, contains one or more conversion specifiers. For this assignment, two conversion specifiers are sufficient. As with printf(), each conversion specifier begins with a '%' character and specifies how to convert the input characters into a numerical or other value. The ith conversion specifier corresponds to the ith argument following the string, which must be the name of a variable and which must be preceded by a '&'character. For example, a conversion specifier of '%f' says to treat the input string as a floating point number with optional sign and optional exponent and store it in the variable named by the corresponding argument. The variable must be declared as double. More details about scanf() can be found in §7.4 of K&R or Chapter 9 of D&D.

There are no restrictions on the input data; that is, the user may enter any real or integer values for the x- and ­y­-coordinates of each of the three points of the triangle. You should not define any functions in the solution for this program other than the main() function. You should not use any loops or conditional statements.

2.      Submit this assignment using the turnin system. From your CCC Linux shell, submit your files using the following turnin command:–

/cs/bin/turnin submit cs2303 LAB1 lab1b.c

Be sure to submit your program by 11:59 PM on the date of your lab session in order to get credit for this part of the lab.

If you have trouble with this lab, ask for help right away. You may ask your classmates, other students, the TAs, the Professor, or anyone else who can help you.

“Hello, World” program

In case you forgot, here is a copy of the “Hello, World” program:–

/* Lab 1 -- type your name here

   Getting started

 */ 

 

#include <stdio.h>

 

int main(void)

{

  /* Print a single string on "standard output" */

  printf("Hello, World!\n");

 

  return 0;

}