/**************************************************************** Johnathan Raphael Patrick MAY 5 2007 Project 5 : THIS PROGRAM WILL ASK THE USER,FOR THE AVERAGE RAIN FALL FOR EACH MONTH,ENTER THIS INTO AN ARRAY, OUTPUT INTO A NEAT TABLE,THE MONTH AND IT'S AVERAGE RAINFALL NEXT CALCULATE THE AVERAGE FOR ALL 12 MONTHS LASTLY OUTPUT IN A NEAT TABLE, THE MONTH, IT'S AVERAGE AND IT'S DIFFERENCE FROM THE AVERAGE FOR ALL 12 MONTHS. *****************************************************************/ #include #include using namespace std; // Here are the Function Prototypes double sumArray(double[], int); double getHighest(double[], int); double getLowest(double[], int); void sortArray(double [], int); void showArray(double [], int); int getindex(int array[], int); const int MONTHS = 12; const int string_SIZE = 10; struct rainfall // structure for the rainfall amounts, months, and position of the elements. { double amount; double values[MONTHS]; char rain[MONTHS][string_SIZE]; }; int main() { char inrain[MONTHS][string_SIZE] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; rainfall yearly; double total; double average; double highest; double lowest; int i,j; for (i = 0; i < MONTHS; i++) for (j = 0; j < string_SIZE; j ++) yearly.rain[i][j] = inrain[i][j]; // dynamically ask the user for input. for (int count = 0; count < MONTHS; count++) { cout << "Enter the amount of rainfall for "<>yearly.values[count]; } cout<<"\n"; cout<<"\n"; // Get the total rainfall. total = sumArray(yearly.values, MONTHS); // Calculate the average. average = total / MONTHS; // Find the highest rainfall amount. highest = getHighest(yearly.values, MONTHS); // Find the lowest rainfall amount. lowest = getLowest(yearly.values, MONTHS); // Find the position of the lowest amount // Display the total cout<<"The total amount of rainfall for the year is: "< highest) highest = array[count]; } return highest; } double getLowest(double array[], int size) { double lowest; lowest = array[0]; for (int count = 1; count < size; count++) { if (array[count] < lowest) lowest = array[count]; } return lowest; } /*********************************************************** Definition of function sortArray This function performs an ascending order bubble sort on array. size is the number of elements in the array. ***********************************************************/ void sortArray(struct rainfall array[MONTHS], int size) { bool swap; struct rainfall temp; do { swap = false; for (int count = 0; count < size; count++) { if (array[count].amount < array[count + 1].amount) { temp = array[count]; array[count] = array[count + 1]; array[count + 1] = temp; swap = true; } } } while (swap); } /************************************************************* Definition of function showArray. This function displays the contents of array. size is the number of elements. *************************************************************/ void showArray(double array[], int size) { for (int count = 0; count < size; count++) cout << array[count] << " "; cout << endl; }