cpp visual studio
Write a program that lets the user enter the total rainfall
for
each of the 12 months into an array of doubles. The program should
calculate and display the total rainfall
for the year, the average monthly rainfall, and the months with the
highest and lowest amounts. Use main( )
as your driver function. Write main( ) so that it allows the user
to run the program as many times as desired,
as indicated by input from the user. Write functions that main( )
calls to accomplish the following tasks:
a) Prompts and gets the total rainfall for each of the twelve
months and stores these values in a one-
dimensional array using a loop. Do not allow the user to enter a
negative amount.
b) Calculates the total rainfall for the year.
c) Calculates the average rainfall for the year.
d) Determines the largest amount of rainfall recorded for one
month. (Hint: See Section 7.6 in Chapter 7
e-text or Slide #36 in Chapter 7 PowerPoint Lecture Notes)
*Note: The purpose of functions is to allow code to be reusable.
You will need this function again for
your next assignment.
e) Determines the smallest amount of rainfall recorded for one
month. (Hint: See Section 7.6 in Chapter 7
e-text or Slide #37 in Chapter 7 PowerPoint Lecture Notes)
*Note: The purpose of functions is to allow code to be reusable.
You will need this function again
for your next assignment.
f) Displays the original rainfall amounts entered by the user for
all twelve months using a loop, the total
and average amount of rainfall for the year displayed to a
hundredth of a decimal, and the amounts and
month(s) that recorded the most and least amounts of
rainfall.
Sample Input:
Enter the rainfall for month #1: 3.2
Enter the rainfall for month #2: 5.6
Enter the rainfall for month #3: 4.1
Enter the rainfall for month #4: 2.3
Enter the rainfall for month #5: 5.6
Enter the rainfall for month #6: 3.7
Enter the rainfall for month #7: 2.3
Enter the rainfall for month #8: 4.5
Enter the rainfall for month #9: 3.4
Enter the rainfall for month #10: 2.5
Enter the rainfall for month #11: 2.3
Enter the rainfall for month #12: 5.1
Sample Output:
The rainfalls for the following months were:
Month 1: 3.2
Month 2: 5.6
Month 3: 4.1
Month 4: 2.3
Month 5: 5.6
Month 6: 3.7
Month 7: 2.3
Month 8: 4.5
Month 9: 3.4
Month 10: 2.5
Month 11: 2.3
Month 12: 5.1
The total rainfall for the year was 44.60 inches.
The average rainfall for the year was 3.72 inches.
The month(s) that recorded the highest amount of rain of 5.60
inch(es) was (were):
Months #: 2 5
The month(s) that recorded the lowest amount of rain of 2.30
inch(es) was (were):
Months #: 4 7 11
If program does NOT run, does NOT use multiple functions, or
does NOT use a 1D array, then 0 points
for lab grade.
-1 point – if no name or description of code
-1 point – Step (f) – for each inaccurate output item (total,
average, highest value, lowest value, months
for highest and lowest)
-1 point – Step (f) – for each output item not displayed in
sample output (each monthly value, total,
average, highest value, lowest value, months for highest and
lowest)
-2 points – Step (a) – if input/data validation is not performed
(rainfall cannot be a negative value) with
error message displayed and allows the user to reenter a correct
value multiple times
-1 point – Step (f) – if values not display to a hundredth of a
decimal (2 decimal positions)
-2 points – Step (a) – if a loop is not used for input of the
array
-2 points – Step (f) – if a loop is not used for output of the
array
-1 point – if no comment for each function header
Code:
using System;
class Program
{
static void Main()
{
double[] rainFall = new double[12];
do {
for(int i = 0; i < 12; i++) {
Console.WriteLine("Enter the rainfall for month #" + i +
":");
rainFall[i] = Convert.ToDouble(Console.ReadLine());
}
double totalRainFall = getTotalRainFall(rainFall);
double avgRainFall = totalRainFall / 12;
double maxRainFall = getMaxRainFall(rainFall);
double minRainFall = getMinRainFall(rainFall);
Console.WriteLine("The rainfalls for the following months
were:");
for(int i = 0; i < 12; i++) {
Console.WriteLine("Month " + i + ":" + rainFall[i]);
}
Console.WriteLine("The total rainfall for the year was " +
totalRainFall + " inches");
Console.WriteLine("The average rainfall for the year was " +
avgRainFall + " inches");
Console.WriteLine("The month(s) that recorded the highest amount of
rain of " + maxRainFall + " inches");
for(int i = 0; i < 12; i++) {
if(rainFall[i] == maxRainFall) {
Console.Write((i+1)+ "\t");
}
}
Console.WriteLine("The month(s) that recorded the lowest amount of
rain of " + minRainFall + " inches");
for(int i = 0; i < 12; i++) {
if(rainFall[i] == minRainFall) {
Console.Write((i+1)+ "\t");
}
}
Console.WriteLine("\nWant to run again?");
string cont = Console.ReadLine();
if(cont != "y") {
break;
} //Stop when user want to
} while(true);// Run as many times as you want
}
static double getTotalRainFall(double[] rainFall){
double sum = 0.0;
for(int i = 1; i < 12; i++) {
sum += rainFall[i];
}
return sum;
}
static double getMaxRainFall(double[] rainFall){
double max = rainFall[0];
for(int i = 1; i < 12; i++) {
if(max < rainFall[i]) {
max = rainFall[i];
}
}
return max;
}
static double getMinRainFall(double[] rainFall){
double min = rainFall[0];
for(int i = 1; i < 12; i++) {
if(min > rainFall[i]) {
min = rainFall[i];
}
}
return min;
}
}
Sample Output:
Enter the rainfall for month #0:
Enter the rainfall for month #1:
Enter the rainfall for month #2:
Enter the rainfall for month #3:
Enter the rainfall for month #4:
Enter the rainfall for month #5:
Enter the rainfall for month #6:
Enter the rainfall for month #7:
Enter the rainfall for month #8:
Enter the rainfall for month #9:
Enter the rainfall for month #10:
Enter the rainfall for month #11:
The rainfalls for the following months were:
Month 0:3.2
Month 1:5.6
Month 2:4.1
Month 3:2.3
Month 4:5.6
Month 5:3.7
Month 6:2.3
Month 7:4.5
Month 8:3.4
Month 9:2.5
Month 10:2.3
Month 11:5.1
The total rainfall for the year was 41.4 inches
The average rainfall for the year was 3.45 inches
The month(s) that recorded the highest amount of rain of 5.6
inches
2 5 The month(s) that recorded the lowest
amount of rain of 2.3 inches
4 7 11
Want to run again?
Sample Input:
3.2
5.6
4.1
2.3
5.6
3.7
2.3
4.5
3.4
2.5
2.3
5.1
n
Screenshot:

Please comment for doubts.
cpp visual studio Write a program that lets the user enter the total rainfall for each...