Laboratory 2
Testing, Debugging and Conditional Statements

Step 1:

Step 2:

Step 3:

 

Step 4:

Question

Question

Modification

Loops

Step 1:

    Set up another project. This one has a program with a loop. It is going to have problems as well. Again, you should be able to copy and paste the program from the web pages to your session and not have to type it in.

Step 2:

    Compile the program. There will be some warnings but should be no errors. When you run the program under Microsoft Visual C++ you will notice that it doesn't wait for any input, outputs an average of -1, and exits. This is due to the fact that when we reach the top of the while loop value has not been initialized (could have been a warning when you compiled it). When a variable is not initialized it may be any value. In the case of Microsoft it is evidently a negative value.

Step 3:

    Fix this by initializing value to 1 by putting an assignment statement outside of the loop and try running it again. Use input values of 10, 20, 30, and -1. It should produce an average of 20.

Step 4:

    Now, we haven't tested it completely. Lets test with a "unusual" case. Lets assume there are NO scores. Rerun the program and use a -1 as the input. What is the value output for the average?

Step 5:

    Add an if statement that will prevent the average from being printed when the number of scores is 0. Print the modified program out and hand it in. Make sure your name is on the listing!