In: Computer Science
In this assignment, the program will keep track of the amount of rainfall for a 12-month period. The data must be stored in an array of 12 doubles, each element of the array corresponds to one of the months. The program should make use of a second array of 12 strings, which will have the names of the months. These two arrays will be working in parallel. The array holding the month names will be initialized when the array is created using an initialization list (could also be created as an array of constants). The second array will hold doubles which will be the total rainfall for each month. Using a function, the program will prompt the user for the rainfall for each month (using both arrays) and store the value entered into the array with the rainfall totals; the other is used to display which month the program is asking for the rainfall total.
The output of the program will display the following once the data is all entered:
The program must have the following functions:
Pseudocode must be provided in the comment block at the top of the file. This must be done with Visual Studio 2019 comm edition C++
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
Simple pseudocode:
Initialize string array ‘months’ containing month names
Declare double array ‘rain’ to store rainfall for each month
Declare total=0, average=0, lowest=0, highest=0, monthLowest=-1, monthHighest=-1
For i in range(12):
Read rainfall for the month months[i] to set rain[i]
For I in range(12):
Add rain[i] to total
If monthLowest is -1 or monthHighest is -1:
Set monthLowest=i, monthHighest=i, lowest=rain[i], highest=rain[i]
Else:
If rain[i] < lowest:
lowest=rain[i]
monthLowest=i
if rain[i] > highest:
highest=rain[i]
monthHighest=i
Set average= total/12
Display total
Display average
Display months[monthLowest] and lowest
Display months[monthHighest] and highest
//Code
#include<iostream>
#include<iomanip>
using namespace std;
//reads rainfall for all 12 months and fill the rain array
void CollectRainData(double rain[], string months[], int count){
for(int i=0;i<count;i++){
cout<<"Enter total rainfall for the month of "<<months[i]<<": ";
cin>>rain[i];
}
}
//finds and returns the sum total of rainfall in the rain array
double CalculateTotalRainfall(double rain[], int count){
double sum=0;
for(int i=0;i<count;i++){
sum+=rain[i];
}
return sum;
}
//returns the average rainfall, given the total and count
double CalculateAverage(double total, int count){
double avg=(double) total/count;
return avg;
}
//returns the lowest rainfall, also updates monthIndex with the index of month with
//lowest rainfall. assuming rain array is not empty
double FindLowest(double rain[], int count, int& monthIndex){
monthIndex=-1;
for(int i=0;i<count;i++){
//if monthIndex is still -1 or current month has less rain than month pointed by
//monthIndex, updating monthIndex
if(monthIndex==-1 || rain[i] < rain[monthIndex]){
monthIndex=i;
}
}
//returning rainfall for the month at monthIndex
return rain[monthIndex];
}
//returns the highest rainfall, also updates monthIndex with the index of month with
//highest rainfall
double FindHighest(double rain[], int count, int& monthIndex){
monthIndex=-1;
for(int i=0;i<count;i++){
//if monthIndex is still -1 or current month has more rain than month pointed by
//monthIndex, updating monthIndex
if(monthIndex==-1 || rain[i] > rain[monthIndex]){
monthIndex=i;
}
}
return rain[monthIndex];
}
int main(){
//setting up the required arrays and variables
const int count=12;
string months[]={"January","February","March","April","May","June","July",
"August","September","October", "November","December"};
double rain[count];
int monthLowest,monthHighest;
double lowest, highest,total,avg;
//collecting rain data
CollectRainData(rain,months,count);
//finding total, average, lowest and highest rainfall
total=CalculateTotalRainfall(rain,count);
avg=CalculateAverage(total,count);
lowest=FindLowest(rain,count,monthLowest);
highest=FindHighest(rain,count,monthHighest);
//using a precision of 2 digits after decimal point
cout<<setprecision(2)<<fixed;
//displaying all stats
cout<<"Total rainfall: "<<total<<endl;
cout<<"Average rainfall: "<<avg<<endl;
cout<<"The month with highest rainfall: "<<months[monthHighest]<<" ("<<highest<<")"<<endl;
cout<<"The month with lowest rainfall: "<<months[monthLowest]<<" ("<<lowest<<")"<<endl;
return 0;
}
/*OUTPUT*/
Enter total rainfall for the month of January: 2.3
Enter total rainfall for the month of February: 2.4
Enter total rainfall for the month of March: 1.8
Enter total rainfall for the month of April: 2.9
Enter total rainfall for the month of May: 2.5
Enter total rainfall for the month of June: 4.6
Enter total rainfall for the month of July: 6.3
Enter total rainfall for the month of August: 6.1
Enter total rainfall for the month of September: 4.3
Enter total rainfall for the month of October: 2.2
Enter total rainfall for the month of November: 1.1
Enter total rainfall for the month of December: 2.1
Total rainfall: 38.60
Average rainfall: 3.22
The month with highest rainfall: July (6.30)
The month with lowest rainfall: November (1.10)