-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRainfallWithArrays.cpp
102 lines (82 loc) · 2 KB
/
RainfallWithArrays.cpp
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// This program will do the rainfall statistics for 12 months
#include <iostream>
#include <iomanip>
using namespace std;
// Constant
const int SIZE = 12;
// Function prototype
void getRainfall(double amount[SIZE]);
double getTotal(double amount[SIZE]);
double getAverage(double amount[SIZE]);
double getLargest(double amount[SIZE]);
double getSmallest(double amount[SIZE]);
// Main function
int main()
{
double amount[SIZE];
double total, average, largest, smallest;
cout << fixed << showpoint << setprecision(2);
getRainfall(amount);
total = getTotal(amount);
average = getAverage(amount);
largest = getLargest(amount);
smallest = getSmallest(amount);
return 0;
}
// Gets Rainfall
void getRainfall(double amount[SIZE])
{
for (int month = 0; month < SIZE; month++)
{
cout << "Please enter rainfall for month #" << month + 1 << ": ";
cin >> amount[month];
// Inputs validation
if (amount[month] < 0)
{
cout << "Error! Enter a positive integer\n\n";
cout << "Please enter rainfall for month #" << month + 1 << ": ";
cin >> amount[month];
}
}
}
// Gets Total
double getTotal(double amount[SIZE])
{
double total = 0;
for (int month = 0; month < SIZE; month++)
total += amount[month];
return total;
}
// Gets Average
double getAverage(double amount[SIZE])
{
double total;
double average;
total = getTotal(amount);
cout << "\nThe total is :" << total << endl;
average = total / SIZE;
cout << "The average is :" << average << endl;
return average;
}
// Gets Largest
double getLargest(double amount[SIZE])
{
double largest;
largest = amount[0];
for (int month = 1; month < SIZE; month++)
if (largest < amount[month])
largest = amount[month];
cout << "The largest is :" << largest << endl;
return largest;
}
// Gets Smallest
double getSmallest(double amount[SIZE])
{
double smallest;
smallest = amount[0];
for (int month = 1; month < SIZE; month++)
if (smallest > amount[month])
smallest = amount[month];
cout << "The smallest is:" << smallest << endl;
return smallest;
}