-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ5.cpp
More file actions
58 lines (49 loc) · 1.16 KB
/
Q5.cpp
File metadata and controls
58 lines (49 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<iostream>
using namespace std;
double rainfall[12];
double total(double rainfall[12]) {
double sum = 0;
for (int i = 0; i < 12; i++) {
sum += rainfall[i];
}
return sum;
}
double average(double rainfall[12]) {
return total(rainfall) / 12;
}
void low(double rainfall[12]) {
double low = rainfall[0];
int number=1;
for (int i = 0; i < 12; i++) {
if (low > rainfall[i]) {
low = rainfall[i];
number = i+1;
}
}
cout << "\nThe lowest rainfall was " << low << " in month " << number << ".";
}
void high(double rainfall[12]) {
double high = rainfall[0];
int number=1;
for (int i = 0; i < 12; i++) {
if (high < rainfall[i]) {
high = rainfall[i];
number = i+1;
}
}
cout << "\nThe highest rainfall was " << high << " in month " << number << ".";
}
int main() {
for (int i = 0; i < 12; i++) {
cout << "Enter the rainfall for month " << i + 1 << ": ";
cin >> rainfall[i];
while (rainfall[i] < 0) {
cout << "Error, enter a positive number: ";
cin >> rainfall[i];
}
}
cout << "\nTotal rainfall for the year: " << total(rainfall);
cout << "\nAverage monthly rainfall: " << average(rainfall);
low(rainfall);
high(rainfall);
}