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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
/*
Rainfall Statistics
Design a program that lets the user enter the total rainfall for each of 12 months
into an array.
The program should calculate and display the total rainfall for the year,
the average monthly rainfall, and the months with the highest and lowest amounts.
*/
#include <iostream>
#include <string.h>
#include <string>
#include <consoleapi.h>
using namespace std;
float getMonths(float month[], int size);
float calcTotal(float month[], float *totalRainfall);
float findHighest(float month[], float *highest, int *highMonth);
float findLowest(float month[], float *lowest, int *lowMonth);
float findAverage(float *average, float *total);
void printInformation(float total, float highest, float lowest, int *highMonth, int *lowMonth, float average);
// Main Function ---------------------------------------------------------------------
int main()
{
string keepGoing = "yes";
while (keepGoing == "yes")
{
// Declaring Variables
const int size = 12;
float month[size] = { 0,0,0,0,0,0,0,0,0,0,0,0 };
float totalRainfall = 0;
float averageMonthlyRainfall = 0;
int highestMonth = 0;
int lowestMonth = 0;
float highest = 0;
float lowest = 0;
// Function Calls
getMonths(month, size); // Function for getting the input for the array
calcTotal(month, &totalRainfall); // gets the total rainfall for the year (all elements of the array summed up)
// add function for finind the average yearly rainfall *****************************************
findHighest(month, &highest, &highestMonth); // finds the highest number in the array
findLowest(month, &lowest, &lowestMonth); // finds the lowest number in the array
findAverage(&averageMonthlyRainfall, &totalRainfall);
printInformation(totalRainfall, highest, lowest, &highestMonth, &lowestMonth, averageMonthlyRainfall); // prints the output to screen
cout << "Do you want to run the program again? (yes or no?)" << endl;
cin >> keepGoing;
system("cls");
}
return 0;
}
// End main ---------------------------------------------------------------------------
float getMonths(float month[], int size)
{
int x = 0;
for (int x = 0; x < size; x++) /* x is the index of the array */
{
cout << "Enter the amount for rain for month #" << (x + 1) << " in inches: ";
cin >> month[x];
system("cls");
}
return x;
}
float calcTotal(float month[], float *totalRainfall)
{
for (int x = 0; x < 12; x++)
{
*totalRainfall += month[x];
}
return *totalRainfall;
}
float findHighest(float month[], float *highest, int *highMonth)
{
*highest = month[0]; // assigns the element to be the highest value
for (int x = 0; x < 12; x++)
{
if (month[x] > *highest)
{
*highest = month[x];
*highMonth = x;
}
}
return *highest, *highMonth;
}
float findLowest(float month[], float *lowest, int *lowMonth)
{
*lowest = month[0]; // assigns the element to be the lowest value
for (int x = 0; x < 12; x++)
{
if (month[x] <= *lowest)
{
*lowest = month[x];
*lowMonth = x;
}
}
return *lowest, *lowMonth;
}
float findAverage(float *average, float *total)
{
*average = (*total / 12);
return *average;
}
void printInformation(float total, float highest, float lowest, int *highMonth, int *lowMonth, float average)
{
// local variables
string monthNameHigh = "";
string monthNameLow = "";
// Selection Case to find the name of the month that had the HIGHEST amount of rain
switch (*highMonth)
{
case 0:
monthNameHigh = "January";
break;
case 1:
monthNameHigh = "February";
break;
case 2:
monthNameHigh = "March";
break;
case 3:
monthNameHigh = "April";
break;
case 4:
monthNameHigh = "May";
break;
case 5:
monthNameHigh = "June";
break;
case 6:
monthNameHigh = "July";
break;
case 7:
monthNameHigh = "August";
break;
case 8:
monthNameHigh = "September";
break;
case 9:
monthNameHigh = "October";
break;
case 10:
monthNameHigh = "November";
break;
case 11:
monthNameHigh = "December";
break;
default:
cout << "Error <swtich1>";
}
// Selection Case to find the name of the month that had the LOWEST amount of rain
switch (*lowMonth)
{
case 0:
monthNameLow = "January";
break;
case 1:
monthNameLow = "February";
break;
case 2:
monthNameLow = "March";
break;
case 3:
monthNameLow = "April";
break;
case 4:
monthNameLow = "May";
break;
case 5:
monthNameLow = "June";
break;
case 6:
monthNameLow = "July";
break;
case 7:
monthNameLow = "August";
break;
case 8:
monthNameLow = "September";
break;
case 9:
monthNameLow = "October";
break;
case 10:
monthNameLow = "November";
break;
case 11:
monthNameLow = "December";
break;
default:
cout << "Error <switch2>";
}
// Prints total rainfall
cout << endl;
cout << "Total rainfall for the year was: " << total << endl;
cout << "The average amount of rainfall total over the year is: " << average << " in." << endl;
// Prints the High's
cout << "The month with the highest rainfall was: " << monthNameHigh << " with an amount of " << highest << " in. of rain" << endl;
// Prints the Low's
cout << "The month with the lowest rainfall was: " << monthNameLow << " with an amount of " << lowest << " in." << endl;
}
|