C++ - I\'m having trouble understanding why the function isn\'t being recognized
ID: 3902313 • Letter: C
Question
C++ - I'm having trouble understanding why the function isn't being recognized after being called. The function is to get the total of the array totalMonthlyRainfall. I posted the text code below the image. Thank you for your help.
#include <iostream>
using namespace std;
void monthlyRainfallInput(string[], double[], int);
double totalMonthlyRainfall(double, int);
int main() {
const int MONTHS = 12;
string months[MONTHS] = {"Jan", "Feb", "March", "Apr", "May", "June", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
double monthlyRainfall[MONTHS];
monthlyRainfallInput(months, monthlyRainfall, MONTHS );
totalMonthlyRainfall(monthlyRainfall, MONTHS);
return 0;
}
//Function that collects rainfall amount and puts into array
void monthlyRainfallInput(string months[], double monthlyRainfall[], int MONTHS){
for (int x = 0 ; x < MONTHS; x++){
cout << "Please enter rainfall amount for " << months[x] << endl;
cin >> monthlyRainfall[x];
}
for (int x = 0 ; x < MONTHS; x++){
cout << months[x] << ": " << monthlyRainfall[x] << " inches" << endl;
}
}
//Function that calculates total rainfall
double totalMonthlyRainfall(double monthlyRainfall[], int MONTHS){
int total = 0;
for (int x = 0; x < MONTHS; x++){
total = total + monthlyRainfall[x];
}
return total;
}
12 #include monthlyRainfall[x]; for (int x = 0 ; xExplanation / Answer
#include <iostream>
using namespace std;
void monthlyRainfallInput(string[], double[], int);
// here in the prototype you gave double instead of double[] which represents an array
double totalMonthlyRainfall(double[], int);
int main() {
const int MONTHS = 12;
string months[MONTHS] = {"Jan", "Feb", "March", "Apr", "May", "June", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
double monthlyRainfall[MONTHS];
monthlyRainfallInput(months, monthlyRainfall, MONTHS );
totalMonthlyRainfall(monthlyRainfall, MONTHS);
return 0;
}
//Function that collects rainfall amount and puts into array
void monthlyRainfallInput(string months[], double monthlyRainfall[], int MONTHS){
for (int x = 0 ; x < MONTHS; x++){
cout << "Please enter rainfall amount for " << months[x] << endl;
cin >> monthlyRainfall[x];
}
for (int x = 0 ; x < MONTHS; x++){
cout << months[x] << ": " << monthlyRainfall[x] << " inches" << endl;
}
}
//Function that calculates total rainfall
double totalMonthlyRainfall(double monthlyRainfall[], int MONTHS){
int total = 0;
for (int x = 0; x < MONTHS; x++){
total = total + monthlyRainfall[x];
}
return total;
}
/*SAMPLE OUTPUT
Please enter rainfall amount for Jan
10
Please enter rainfall amount for Feb
12
Please enter rainfall amount for March
14
Please enter rainfall amount for Apr
16
Please enter rainfall amount for May
18
Please enter rainfall amount for June
8
Please enter rainfall amount for Jul
6
Please enter rainfall amount for Aug
4
Please enter rainfall amount for Sep
2
Please enter rainfall amount for Oct
1
Please enter rainfall amount for Nov
0
Please enter rainfall amount for Dec
9
Jan: 10 inches
Feb: 12 inches
March: 14 inches
Apr: 16 inches
May: 18 inches
June: 8 inches
Jul: 6 inches
Aug: 4 inches
Sep: 2 inches
Oct: 1 inches
Nov: 0 inches
Dec: 9 inches
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.