The file lab2.c is a C source file which contains a number of errors in the variable names, their declarations, and their usage. You will need to fix the errors in the code and add some additional functionality.
Note: This lab must be completed as homework and turned in by 23:59 on Tuesday, 25 February.
linux1[2]% cd Desktop/cs104 linux1[3]% mkdir lab2 linux1[4]% cd lab2 linux2[5]% pwd /afs/umbc.edu/users/c/m/cmarron/Desktop/cs104/lab2
linux1[6]% wget http://www.csee.umbc.edu/~cmarron/cmsc104/labs/lab2.c --2014-02-18 22:08:19-- http://www.csee.umbc.edu/~cmarron/cmsc104/labs/lab2.c Resolving www.csee.umbc.edu (www.csee.umbc.edu)... 130.85.36.80 Connecting to www.csee.umbc.edu (www.csee.umbc.edu)|130.85.36.80|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 1146 (1.1K) [text/x-c] Saving to: 'lab2.c' 100%[======================================>] 1,146 --.-K/s in 0s 2014-02-18 22:08:19 (14.6 MB/s) - 'lab2.c' saved [1146/1146] linux1[7]% ls lab2.c
Note: The gcc command line includes the -lm option. This tells the linker to include the math library, which is needed for the square root (sqrt) function.
linux1[8]% gcc -Wall lab2.c -lm lab2.c: In function 'main': lab2.c:20: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'of' lab2.c:20: error: 'of' undeclared (first use in this function) lab2.c:20: error: (Each undeclared identifier is reported only once lab2.c:20: error: for each function it appears in.) lab2.c:20: error: expected ';' before 'rectangle' lab2.c:27: error: 'X' undeclared (first use in this function) lab2.c:27: error: 'Y' undeclared (first use in this function) lab2.c:31: error: 'hypotenuse' undeclared (first use in this function) lab2.c:35: error: 'area' undeclared (first use in this function) lab2.c:35: error: expected ';' before 'of' lab2.c:36: error: expected ')' before 'of' lab2.c:41: warning: format '%f' expects type 'double', but argument 2 has type 'int' linux1[9]%
Each error message includes a line number after the name of the source file. For example, lab2.c:20 shows that there is an error on line 20 of the source file lab2.c.
The emacs editor displays the current line number in the grey bar at the bottom of the screen, preceded by the letter "L". See the example below. Thus, we can use the line numbers from gcc and the line number display in emacs to find the lines of source code that are causing errors.
Now that you can find the errors in the code, it's time to fix them.
linux1[19]% gcc -Wall lab2.c -lm lab2.c: In function 'main': lab2.c:31: error: 'hypotenuse' undeclared (first use in this function) lab2.c:41: warning: format '%f' expects type 'double', but argument 2 has type 'int' linux1[20]%
linux1[29]% gcc -Wall lab2.c -lm linux1[30]% ./a.out Starting program with x = 3.000000 and y = 4.000000 Hypotenuse is 5.000000 Area of rectangle is 12.000000 Area of circle with radius 3.000000 is 28.274311
If you made it this far, then it's time to add some features to the program.
Here's a sample run of the completed program:
linux1[22]% ./a.out Enter x: 3.5 Enter y: 8.1 Starting program with x = 3.500000 and y = 8.100000 Hypotenuse is 8.823832 Area of rectangle is 28.350002 Area of circle with radius 3.500000 is 38.484478 The sin of x*y is -0.075596 linux1[23]%
Please complete the lab for homework and turn it in by 23:59 on Tuesday, 25 February.
linux1[24]% submit cmsc104_cmarron hw2 lab2.c linux1[25]%
Be sure to logout completely when you have finished!