Lab 6: If Statements & Separate Compilation

Objectives

The objectives of this lab are to give you more practice at using if statements as well as functions and while loops and to begin using conditional and separate compilation for your programs. You are still expected to be thinking in terms of the algorithms for your programs before coding and to write your algorithms in the comments in your files.

Programs

You should begin by creating a new directory called Lab6 in your EE160/Labs directory (which you have created in previous labs). All of the program files you write for this lab should be in this Lab6 directory.

    • (3 Points). Do problem 4 in section 2.9 of the text. THINK about this program before beginning to code. Try to minimize the number of times each integer is tested as well as the number of if statements you use. My program only tests each integer twice, and has only 3 if statements in the code.
        • Implement this program incrementally; i.e. first build the loop and test for only one set of conditions (e.g. even and odd). Get that program to work, and then add features to it a little at a time, testing as you go along. If you use "yank and put" in vi (copy and paste) and then modify the copy, you can save yourself alot of typing, since the different sections of this program are very similar to each other.
        • Use #ifdef DEBUG conditional compilation around your debug statements to turn on and off debugging. Turn debugging off when you turn in your program. You can write this program with just a single function, main(). Put the code in a file called numbers.c. A sample output from my program looks like:
          15
          debug:15 is positive(count = 1, total = 15)
          debug:15 is odd(count = 1, total = 15)
          debug:Total(count = 1, total = 15)
          2
          debug:2 is positive(count = 2, total = 17)
          debug:2 is even(count = 1, total = 2)
          debug:2 is positive and even(count = 1, total = 2)
          debug:Total(count = 2, total = 17)
          -3
          debug:-3 is negative(count = 1, total = -3)
          debug:-3 is odd(count = 2, total = 12)
          debug:-3 is negative and odd(count = 1, total = -3)
          debug:Total(count = 3, total = 14)
          -8
          debug:-8 is negative(count = 2, total = -11)
          debug:-8 is even(count = 2, total = -6)
          debug:Total(count = 4, total = 6)
          0
          There were 2 positive numbers totaling 17
          There were 2 negative numbers totaling -11
          There were 2 even numbers totaling -6
          There were 2 odd numbers totaling 12
          There were 1 positive even numbers totaling 2
          There were 1 negative odd numbers totaling -3
          There were 4 total numbers totaling 6


    • (2 Points). This is a variation of problem 4 in section 3.9 of the text. We will do this problem in two parts.
        • For the first part, write the functions max() and min() in the file maxmin.c. Put the prototypes for these functions in the file maxmin.h (and don't forget to include this header where needed). Then write a test driver, main(), to test these functions in the file driver1.c. The driver should allow the user to test these functions by entering one pair of float values and verify the functions return the correct values each time. Use end-of-file to end the test input. You can then compile this program with the command:
         cc driver1.c maxmin.c


    • (3 Points). This will complete the variation of the program described in section 3.9. You will use the same maxmin.c file from above for this program. Write a new driver main() in the file driver2.c which reads numbers one at a time, and keeps track of the biggest and smallest number seen so far. Use your functions max() and min() to do the comparisons. Think about the algorithm you will use for this one. If you were asked to perform the same task (find the maximum and minimum of a bunch of numbers that you are given just one at a time), how would you do it? As an example, if the input numbers are:
        23.5
        16.1
        -35.2
        90
        -13.1
        0
          • your program should print that the maximum is 90.0 and minimum is -35.2.
          • Use debug statements (with conditional compilation) to show that the maximum and minimum are being updated correctly with each input number. You can compile this program with the command:
         cc driver2.c maxmin.c


    • (2 Points). Do problem 18 in section 3.9 of the text. Put your function pos_power() in the file exponent.c and the prototype in the file exponent.h.
        • Another useful debugging technique is to put debug lines into your functions to show when it is called, with what values are passed to it, and when the function returns with what value is being returned. Try using this technique for your function pos_power(). Put a printf() that looks like:
      printf("debug:Enter pos_power: base = %f exponent= %d\n", base, exponent);
        • as the first statement executed in your function. Put a printf() that looks like:
      printf("debug:Exit pos_power: result = %f\n", value);
        • (or whatever your variable name is for the result) as the statement just before the function returns. Use #ifdef to allow these debug lines to be turned on and off. Turn debugging off when you hand in your programs. Write a driver, main(), to allow you to test the function. Put the driver in the file named driver3.c You can compile this program with the command:
         cc driver3.c exponent.c

What You Turn In

Use the "grade" command to turn in the six source files (numbers.c, maxmin.c, exponent.c, driver1.c, driver2.c, and driver3.c) and the two header files (maxmin.h and exponent.h). Your command will look like the following.


If you are in Section 001 use:

           grade -lab6s1,ee160  *.c *.h


If you are in Section 002 use:

           grade -lab6s2,ee160  *.c *.h


If you are in Section 003 use:

           grade -lab6s3,ee160  *.c *.h


If you are in Section 004 use:

           grade -lab6s4,ee160  *.c *.h


NOTE: this command will send in ALL files named with .c and .h extensions in the current directory. You should verify that you turned in things successfully, which you can do with the command (which simply leaves the file names off from the previous command).

           grade -lab6s1,ee160

           OR

           grade -lab6s2,ee160

           OR

           grade -lab6s3,ee160

           OR

           grade -lab6s4,ee160

NOTE: after the files are prepared for grading, you will no longer be able to see your file listing using the above command. NOTE: BE CAREFUL to use the correct form of the grade command given above. If you do not, your files will be sent to the wrong place, and we will not guarantee we will find them for grading.