Homework 5: More Bar Graphs
Due: Tuesday 3/6 by 11:59pm
Objectives
More practice with nested while loops.
Assignment
This assignment is a continuation of Classwork 8. (Note: you must still submit Classwork 8 and Homework 5 separately.) For this homework assignment, embellish your program for Classwork 8 with the following features:
- Since bar graphs do not make sense for negative numbers, add another inner while loop to your program that keeps pestering the user until he enters a number strictly greater than zero.
- After the outer while loop terminates, print out the largest number entered by the user.
- After the outer while loop terminates, print out the average number entered by the user.
A sample run of your program might look like:
PT[203]% gcc bar2.c PT[204]% ./a.out Enter a positive number (type 'quit' to end) 30 ****************************** Enter a positive number (type 'quit' to end) 29 ***************************** Enter a positive number (type 'quit' to end) 17 ***************** Enter a positive number (type 'quit' to end) 28 **************************** Enter a positive number (type 'quit' to end) 50 ************************************************** Enter a positive number (type 'quit' to end) 67 ******************************************************************* Enter a positive number (type 'quit' to end) 36 ************************************ Enter a positive number (type 'quit' to end) 19 ******************* Enter a positive number (type 'quit' to end) 17 ***************** Enter a positive number (type 'quit' to end) quit Sum = 293, count = 9 Largest number found = 67 Average number found = 32.5556 PT[205]% PT[205]% ./a.out Enter a positive number (type 'quit' to end) -2 *** -2 is not positive, re-enter. *** Enter a positive number (type 'quit' to end) 12 ************ Enter a positive number (type 'quit' to end) 14 ************** Enter a positive number (type 'quit' to end) 9 ********* Enter a positive number (type 'quit' to end) 0 *** 0 is not positive, re-enter. *** Enter a positive number (type 'quit' to end) -2 *** -2 is not positive, re-enter. *** Enter a positive number (type 'quit' to end) 8 ******** Enter a positive number (type 'quit' to end) quit Sum = 43, count = 4 Largest number found = 14 Average number found = 10.75 PT[206]%
Notes
- Make a copy of your program from
Classwork 8
using the Unix cp command:
cp bar.c bar2.c
- Think about how to calculate the sum of the numbers entered by the user. Use a double variable so you get a floating point value when you compute the average.
- Since the numbers entered by the user must be positive, you can initialize the maximum value to 0. (Why does this work?)
- Make sure that you handle the case where the user types in quit without entering any numbers.
- You cannot use a break statement to break out of two nested while loops. A break statement in an inner while loop only breaks from the inner-most loop that contains the break statement. It will not exit from the outer loop.
What to submit
Use the script command to record yourself compiling your program and running your program 3 times using different numbers. Use exit to terminate the recording. Only record yourself compiling and running your program. DO NOT record yourself editing your program. If you mistakenly start up nano while running script, just exit from script and start over. (The new typescript file will overwrite the old one.)
When you are done. Use:
cat typescript
submit cs104_chang hw05 bar2.c typescript