Lab 10: More Loops

Objectives

The purpose of this lab is to give you a chance to play with different loop constructs provided in C and different ways to organize loops. One of the things you will notice about the programs provided below, is that they are inadequately documented. Part of your job is to comment the programs you turn in to properly show the algorithms they are implementing. As usual, you should begin by creating a new directory called Lab10 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 Lab10 directory.

3Programs

    • (6 Points). Exploring the "for" and "while" statements.
        • Compile and run the counting program, counters.c. Save its output into a file.
        • This program currently uses all while loops. Copy it into a new file counters2.c, and rewrite the program so that it uses only for loops rather than while loops. Make this change one loop at a time, and make sure your change doesn't change the output the program produces.
        • Compile and run the avg.c. It computes the average of the values given to it as input.
        • It now uses a for loop to read its input. Rewrite it to use a while loop rather than a for.
        • Compile and run the program questions.c. It reads in a line from the user and determines whether it is a yes or no based on the first character on the line. The program uses for loops (an "infinite" for loop and another "for" loop that is missing its initialization parts). Rewrite it using "while" loops instead. To help you figure out what's going on, questions2.c is the same program, but with printf statements that say exactly what's going on. After you get questions2.c working using "while" loops, delete all of its debugging statements.
    • (4 Points). Exploring leaving loops early.
        • Compile and run the program averager.c. It is supposed to repeatedly read a number, then reads that number of values and prints their average. When you run it, you should see input and output like this:
            5
            10 20 30 40 50
            Average of 5 values is 30.00
            7
            15 20 40 50 60 40 50
            Average of 7 values is 39.29
        • Determine what the program does if it encounters garbage in the input or end of file when it is trying to read an input value. There are two cases to worry about. What does it do if instead of the 5 above, you enter "garbage"? And what does it do if instead of the "10 20 ... 50", you enter "10 20 garbage"? Fix the program so that if it receives garbage input instead of integers for the expected number of values, it prints the following error message and quits.
            5
            10 20 30 40 50
            Average of 5 values is 30.00
            some garbage input
            Error!  Can't read number of expected values.
        • Fix the program so that it prints the following error message and quits if it receives garbage input instead of integers when it is trying to read one of the values it should be averaging.
            5
            10 20 30 40 50
            Average of 5 values is 30.00
            6
            50 70 whatever 40 60
            Error!  Can't read expected value #2.
            Average of 2 values is 60.00

What You Turn In

Use the "grade" command to turn in the all of the source files (.c and .h files) in your Lab10 directory. Your command will look like the following.


If you are in Section 001 use:

           grade -lab10s1,ee160  *.c *.h 


If you are in Section 002 use:

           grade -lab10s2,ee160  *.c *.h 


If you are in Section 003 use:

           grade -lab10s3,ee160  *.c *.h 


If you are in Section 004 use:

           grade -lab10s4,ee160  *.c *.h 


If you are in Section 005 use:

           grade -lab10s5,ee160  *.c *.h 


NOTE: this command will send in ALL files named with .c and .h extensions in the current directory which are really your files. The grade command will give you a message for files that are links and not submit the files - that is ok, we only want the files which you wrote. 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 -lab10s1,ee160
           OR
           grade -lab10s2,ee160
           OR
          grade -lab10s3,ee160
           OR
           grade -lab10s4,ee160
           OR
           grade -lab10s5,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.