Write a C++ program that reads the amount of rainfall in inches for each month o
ID: 3705842 • Letter: W
Question
Write a C++ program that reads the amount of rainfall in inches for each month of one-year from a file into an array. The file is named "rainfall.dat" and has 12 value, one for each month. (i.e. 5.92). After all of the values have been input, the program should calculate and output the following two statistics, with labels:
a. the total amount of rainfall for the year
b. which month has the highest rainfall (output the number, 1 for Jan, 2 for Feb, etc)
Please help with this quick problem! I want to make sure I'm doing this correctly.
Explanation / Answer
#include #include using namespace std; int main() { ifstream in("rainfall.dat"); int count = 0, max_month = 0; double max = 0, temp; double total = 0; double arr[12]; for(int i = 0; i < 12; ++i) { in >> temp; arr[i] = temp; } for(int i = 0; i < 12; ++i) { temp = arr[i]; if(temp > max) { max = temp; max_month = i+1; } total += temp; } coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.