Classwork 8: Bar Graphs
Objectives
To practice using nested while loops.
The Assignment
For this assignment, you will write a program that prints out a horizontal "bar graph" using numbers entered by the user.
PT[225]% gcc -Wall bar.c PT[226]% ./a.out Enter a positive number (type 'quit' to end): 3 *** Enter a positive number (type 'quit' to end): 17 ***************** Enter a positive number (type 'quit' to end): 23 *********************** Enter a positive number (type 'quit' to end): 22 ********************** Enter a positive number (type 'quit' to end): 9 ********* Enter a positive number (type 'quit' to end): 4 **** Enter a positive number (type 'quit' to end): quit PT[227]% PT[227]% ./a.out Enter a positive number (type 'quit' to end): quit No numbers were entered PT[228]%
The "bars" in this bar graph are the asterisks (*) printed after each user entry. The number of asterisks must be equal to the number entered by the user.
Notes
- Please name your program bar.c
- You must use nested while loops for this assignment. The outer while loop should iterate once for each number entered by the user. The inner loop should print out the right number of asterisks.
- To print the asterisks, use a new variable and a while loop that counts up to the number entered by the user. (Each iteration of this inner while loop adds one to the counter.) For each iteration of the inner while loop, print out a single asterisk without a newline. After the loop ends, your program should print out a newline character. This way, the asterisks appear on one line.
- A good starting point for your program is: min3.c
- Homework 5 is a continuation of this assignment. If you are done early, you can continue to Homework 5, but be sure to submit both to this classwork and to Homework 5.
What to submit
Use the script command to record yourself compiling and running your program several times with different numbers. (Do not record yourself editing your program!) Exit from script. Submit your program and the typescript file:
submit cs104_chang cw08 bar.c typescript