CMSC104 Spring 2014 Variables in C Lab Exercise

Objectives

Resources:

The Assignment

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.

Compiling with gcc

  1. Log into your gl account. Use the cd command to change to the folder Desktop/cs104/lab2. If you do not already have a lab2 folder in Desktop/cs104, create one now:

    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
    
    

  2. Get a copy of the file lab2.c from the course website. You can do this with the wget command:

    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
    
    

  3. Now try to compile the source file using gcc - you will get lots of error messages:

    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.

Debugging the Code

Now that you can find the errors in the code, it's time to fix them.

  1. Edit the source file in emacs using the command emacs lab2.c. Here's a link to the list of common emacs commands.
  2. Find as many errors as you can and correct them. You may need to look at the slides (Variables in C) to remind yourself of what variables names are legal and how declarations work. Once you've fixed as much as you can, re-compile the program using gcc. You should see fewer errors.

    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]% 
    
    

  3. Repeat the process of editing in emacs, compiling, editing, etc. until the program compiles without errors. Then you can run it with the command ./a.out.

    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
    
    

Enhancing the Program

If you made it this far, then it's time to add some features to the program.

  1. First, it would be nice if the user could enter values for x and y rather than have them hard-coded to 3 and 4. Use the example from the slides to figure out how to do this (Hint: you'll need to use printf() and scanf() functions).
  2. Second, add code to compute sin(x*y) and display it. The C function to compute sin() is just sin().

    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]%
    
    

Submitting Your Work

Please complete the lab for homework and turn it in by 23:59 on Tuesday, 25 February.

  1. Before turning in your work, edit the header comments as necessary. In particular, be sure to change the name and email to your name and email!
  2. Use the submit command to turn-in your completed lab exercise:

    linux1[24]% submit cmsc104_cmarron hw2 lab2.c
    linux1[25]%
    
    
          

Be sure to logout completely when you have finished!