Lab 9: Character Processing & Debugging Programs

Objectives

The purpose of this lab is to give you more experience working with character data and using debug lines to determine where a program is not working and fix it. As usual, you should begin by creating a new directory called Lab9 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 Lab9 directory. For the programs in this lab, you may call the files anything you want, but you will need to provide a makefile to compile the program. You can find a skeleton makefile at:

          ~ee160/Labs/Lab9/makefile

which you can copy to your directory and modify. You will need to complete the target and action lines for all three programs so that they will compile using make (1 Point).

The executables for these programs are called mygrader, mygrader2 and countgrades.

Programs

    • (6 Points). Exploring decision-making statements.
        • Compile and run the program found in grader.c. This program is intended to read test scores and print out a grade. Note that it currently assigns all students a grade of '?', not what we ultimately want. Also note that this program is again undocumented! and not well organized, even for such a small program. Your first task is to break this program up into three files, one for the driver, and one for the function with it's associated .h file. Your second task to add comments to your files to properly document the program showing the algorithm (with comments showing the meaning of the steps, NOT describing the C code), and properly documenting the function and its prototype. Compile this program using make to verify it compiles and runs at this point.
        • Update the body of the assign_grade() function so that it returns an appropriate letter grade: 'A' for a score between 90 and 100, 'B' for a score between 80 and 89, 'C' for a score between 70 and 79, 'D' for a score between 60 and 69, and an 'F' for a score between 0 and 59. An out-of-range score (less than 0 or over 100) should result in a '?' being returned for the grade. Here's the sample output for the data in mygrader.dat (also located in the ee160 folder):
            95: A
            85: B
            80: B
            75: C
            70: C
            68: D
            50: F
            104: ?
            -10: ?
        • Hint: There are two ways to do this: one using an else-if, the other as a set of simple if statements, each having a return statement in the then-part of the statement.
        • Now extend the mygrader program to print an error message for bad grades, rather than just a question mark.
            95: A
            85: B
            80: B
            75: C
            70: C
            68: D
            50: F
            104: illegal score
            -10: illegal score
        • Hint: Think about where you should put the change; in main() or the function assign_grade().
        • Copy your files for this program to new file names and extend this program to read in the grading scale. That is the first 4 input values are taken as the minimum grade for an A, for a B, for a C, and for a D. Here's an example using the traditional grading scale with input from the file mygrader2.dat (also available on wiliki):
            95: A
            85: B
            80: B
            75: C
            70: C
            68: D
            50: F
            104: illegal score
            -10: illegal score
        • Hint: It helps to change the assign_grade() function to take the different items of the grading scale as parameters.
        • In addition, extend the mygrader2 program to verify that the grading scale is reasonable. That is, the minimum score for an A must be larger than the minimum score for a B, and the minimum score for a B must be larger than the minimum score for a C, and so on. The minimum score for an A must also be no larger than 100 and the minimum score for a D must be no smaller than a 0. If it is not, you print a message before reading and processing any of the scores. Here is an example of an illegal grading scale and the resulting program output.
90 95 70 60
invalid grading scale
        • Hint: It helps to write a function that determines whether the grading scale is valid.
        • Extend the program to print the total number of students who pass or fail, where passing is a grade of 'C' or better and failing is a 'D' or 'F'. This output goes at the end.
            95: A
            85: B
            80: B
            75: C
            70: C
            68: D
            50: F
            104: illegal score
            -10: illegal score
            Passing scores: 5
            Failing scores: 2
            Illegal scores: 2
    • (3 Points). Exploring how to debug decision statements.
        • Compile and run the program found in countgrades.c. This program is supposed to read a series of input characters representing grades (e.g., an 'a' or 'A' represents an A, a 'b' or 'B' represents a 'B', and so on) and print counts of each grade. Here is some sample input and the output it is supposed to produce:
                • abcDFEGH
                • DdFFEEaA
                • Grade counts:
                • A's: 3
                • B's: 1
                • C's: 1
                • D's: 3
                • F's: 3
                • Other grades: 5
        • None of the counts will seem to be correct. Don't try to fix it in one shot. Before making any changes, put a print statement at the beginning of the loop (before the switch) that prints each character that's read. Also, add one statement for each case in the switch (as well as the default) that prints the value of the counter after it is updated. Again, you will note this program needs documentation. You should comment the code in your file to show the algorithm. This will also help you understand the code, so you can debug it. This output should help explain at least partially why the counters have the wrong values. After you make these fixes, re-run the program to see if it is fixed, and if it is not, try to add more printf's to help you see what's going on. Your task is to fix this program to produce output like the above.

What You Turn In

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


If you are in Section 001 use:

           grade -lab9s1,ee160  *.c *.h makefile


If you are in Section 002 use:

           grade -lab9s2,ee160  *.c *.h makefile


If you are in Section 003 use:

           grade -lab9s3,ee160  *.c *.h makefile


If you are in Section 004 use:

           grade -lab9s4,ee160  *.c *.h makefile


If you are in Section 005 use:

           grade -lab9s5,ee160  *.c *.h makefile


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 -lab9s1,ee160

           OR

           grade -lab9s2,ee160

           OR

           grade -lab9s3,ee160

           OR

           grade -lab9s4,ee160

           OR

           grade -lab9s5,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.