Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include <iomanip>
- using namespace std;
- const int NUM_MONTHS = 12;
- double getTotal(double[]);
- double getAverage(double[]);
- double getLargest(double[], int &);
- double getSmallest(double[], int &);
- int main()
- {
- double rainFall[NUM_MONTHS];
- for (int month = 0; month < NUM_MONTHS; month++)
- {
- cout << "Enter the rainfall for month ";
- cout << (month + 1) << ": ";
- cin >> rainFall[month];
- while (rainFall[month] < 0)
- {
- cout << "Rainfall must be 0 or more.\n"
- << "Please re-enter: ";
- cin >> rainFall[month];
- }
- }
- cout << fixed << showpoint << setprecision(2) << endl;
- cout << "The total rainfall for the year is ";
- cout << getTotal(rainFall)
- << " inches." << endl;
- cout << "The average rainfall for the year is ";
- cout << getAverage(rainFall)
- << " inches." << endl;
- int subScript = 0;
- cout << "The largest amount of rainfall was ";
- cout << getLargest(rainFall, subScript)
- << " inches in month ";
- cout << (subScript + 1) << "." << endl;
- cout << "The smallest amount of rainfall was ";
- cout << getSmallest(rainFall, subScript)
- << " inches in month ";
- cout << (subScript + 1) << "." << endl << endl;
- return 0;
- }
- double getTotal(double rainFall[])
- {
- double total = 0;
- for (int count = 0; count < NUM_MONTHS; count++)
- total += rainFall[count];
- return total;
- }
- double getAverage(double rainFall[])
- {
- double average = 0.0;
- average = getTotal(rainFall) / NUM_MONTHS;
- return average;
- }
- double getLargest(double rainFall[], int &subscript)
- {
- double largest;
- largest = rainFall[0];
- for (int month = 0; month < NUM_MONTHS; month++){
- if (rainFall[month] > largest){
- largest = rainFall[month];
- subscript = month;
- }
- }
- return largest;
- }
- double getSmallest(double rainFall[], int &subscript)
- {
- double smallest;
- smallest = rainFall[0];
- for (int month = 0; month < NUM_MONTHS; month++){
- if (rainFall[month] < smallest){
- smallest = rainFall[month];
- subscript = month;
- }
- }
- return smallest;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement