Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- //Prototypes go here
- double calc(int rainfall[], int totalRainfall, double averageRainfall);
- void output(int totalRainall, double averageRainfall, int rainHigh, int rainLow, string months[], string monthHigh, string monthLow);
- int main()
- {
- //Variables go here
- string months[12] = {"January", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
- string monthHigh = " ";
- string monthLow = " ";
- char error_ch = ' ';
- char again = ' ';
- int rainfall[12] = {0};
- int rainHigh = 0;
- int rainLow = 500000;
- int totalRainfall = 0;
- double averageRainfall = 0;
- for(int i = 0; i < 12; i ++)
- {
- cout << "How much rain was collected in " << months[i] << "?" << endl;
- cin >> rainfall[i];
- if(cin.fail())
- {
- cin.clear();
- cin >> error_ch;
- cout << endl << "You used a letter please use a number instead." << endl;
- cout << endl;
- cout << "Repeat enter." << endl;
- if(i == 0)
- {
- main();
- }
- else if(i > 0)
- {
- i--;
- }
- }
- if(rainfall[i] > rainHigh)
- {
- rainHigh = rainfall[i];
- monthHigh = months[i];
- }
- if(rainfall[i] < rainLow)
- {
- rainLow = rainfall[i];
- monthLow = months[i];
- }
- }
- averageRainfall = calc(rainfall, totalRainfall, averageRainfall);
- output(totalRainfall, averageRainfall, rainHigh, rainLow, months, monthHigh, monthLow);
- system("Pause");
- system("cls");
- //Runs the program again
- cout << "Would you like to run this program again? y or n: " << endl;
- cin >> again;
- again = tolower(again);
- if(again == 'y')
- {
- system("cls");
- main();
- }
- else
- {
- return 0;
- }
- }
- double calc(int rainfall[], int totalRainfall, double averageRainfall)
- {
- for(int i = 0; i < 12; i ++)
- {
- totalRainfall = rainfall[i] + totalRainfall;
- }
- averageRainfall = totalRainfall / 12;
- return averageRainfall;
- }
- void output(int totalRainfall, double averageRainfall, int rainHigh, int rainLow, string months[], string monthHigh, string monthLow)
- {
- cout << endl;
- cout << "Over the 12 months recorded the average rainfall for the year was " << averageRainfall << "." << endl << endl;
- cout << "Over the 12 month period the highest recorded rainfall is: " << rainHigh << endl << " which was during the month of " << monthHigh << "." << endl << endl;
- cout << "Over the 12 month period the lowest recorded rainfall is: " << rainLow << endl << " which was during the month of" << monthLow << "." << endl << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement