Description
Program #1 & #2 (NOTE: TWO DIFFERENT PROGRAMS)
The distance a vehicle travels can be calculated as follows: distance = speed * time. For
example, if a train travels 40 miles per hour for 3 hours, the distance traveled is 120 miles.
Write a program that asks the user for the speed of a vehicle (in miles per hours) and how many
hours it has traveled. The program should then use a loop to display the distance the vehicle
has traveled for each hour of that time period. Do not accept a negative number for speed and
do not accept any value less than 1 for time traveled.
• Program #1: Write only using while loops
• Program #2: Write only using do-while loops
• Program should loop until the user enters correct data [INPUT VALIDATION]
Sample Output:
What is the speed of the vehicle in mph? 50
How many hours has it traveled? -4
How many hours has it traveled? 4
Hour Distance Traveled
——————————-
1 50
2 100
3 150
4 200
Press any key to continue . . .
Program #3
Write a program that calculates how much a person would earn over a period of time if his or
her salary is one penny the first day and two pennies the second day, and continues to double
each day. The program should ask the user for the number of days. Display the table showing
how much the salary was for each day, and then show the total pay at the end of the period.
The output should be displayed in a dollar amount, not the number of pennies. Do not accept a
number less than 1 for the number of days worked.
• Use a do-while loop for input validation
• Use a for loop to calculate the pay increase and display the table
Sample Output:
For how many days will the pay double? 6
Day Total Pay
———————
1 $ 0.01
2 $ 0.02
3 $ 0.04
4 $ 0.08
5 $ 0.16
6 $ 0.32
———————
Total $ 0.63
Press any key to continue . . .
Program #4
Write a program that uses nested loops to collect data and calculate the average rainfall over a
period of years. The program should first ask for the number of years. The outer loop will
iterate once for each year. The inner loop will iterate twelve times, once for each month. Each
iteration of the inner loop will ask for the inches of rainfall for that month.
After all iterations, the program should display the number of months, the total inches of
rainfall, and the average rainfall per month for the entire period. Do not accept a number less
than 1 for the number of years. Do not accept negative number for the monthly rainfall.
• Input validation not required
• Only use for loops and nested loops for computation
• Use a const for the number of months in the year. (for ease of testing interactively,
consider a calendar with only 4 months in a year, your program should be able to work
by just changing this constant)
Sample Output:
This program will calculate average rainfall over a
period of years. How many years do you wish to average? 2
Year 1
Number of inches of rain for month 1? 4
Number of inches of rain for month 2? 5
Number of inches of rain for month 3? 6
Number of inches of rain for month 4? 7
Number of inches of rain for month 5? 8
Number of inches of rain for month 6? 9
Number of inches of rain for month 7? 1
Number of inches of rain for month 8? 2
Number of inches of rain for month 9? 3
Number of inches of rain for month 10? 4
Number of inches of rain for month 11? 5
Number of inches of rain for month 12? 6
Year 2
Number of inches of rain for month 1? 7
Number of inches of rain for month 2? 8
Number of inches of rain for month 3? 9
Number of inches of rain for month 4? 4
Number of inches of rain for month 5? 5
Number of inches of rain for month 6? 6
Number of inches of rain for month 7? 7
Number of inches of rain for month 8? 8
Number of inches of rain for month 9? 9
Number of inches of rain for month 10? 1
Number of inches of rain for month 11? 2
Number of inches of rain for month 12? 3
Over a period of 24 months, 129 inches of rain fell.
Average monthly rainfall for the period is 5.375 inches.
Press any key to continue . . .