This is my program so far. I am having trouble getting the program to sort and display the array based on the rainfall data entered and the month which it was entered in. I have the program where it sorts the numbers entered for rainfall but cant seem to be able to get the month name to stay with the number it correlates to. Would also like to know how to make the two columns of data line up if anyone could answer that as well. Thanks in advance.
This program was an assignment but I have sense turned it in and received a final score. I asked my professor where I went wrong and got no response hence me putting it on here so I can hopefully learn how to improve.
***************************************
Here are the original instructions for the Rainfall Program:
Write a program that lets the user enter the total rainfall for each of 12 months into an array of doubles. The program should use two 12 element arrays. One array will hold strings, the names of the 12 months. This array will be initialized when the array is created using an initialization list (could also be created as a array of constants). The second array will hold doubles which will be the total rainfall for each month. The program will prompt the user for the rainfall for each month (using both arrays) and store the value entered into the array with the rainfall totals, the other is used to display which month the program is asking for the rainfall total. The program should display the following once the data is all entered:
The total rainfall for the year
The average monthly rainfall
The month with the highest amount of rainfall (must display the month as a string)
The month with the lowest amount of rainfall (must display the month as a string)
The program must have the following functions:
double getTotal(double [ ], int);
double getAverage(double [ ], int);
double getLowest(double [ ], int, int&); //returns the lowest value, provides the index of the lowest value in the last parameter.
double getHighest(double [ ], int, int&); //returns the highest value, provides the index of the highest value in the last parameter.
Modify the Rainfall Statistics program so that it displays a list of months, sorted in the order of rainfall from highest to lowest.
**********************************************
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
|
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//Global constant for size of array
const int SIZE = 12;
//Function prototypes
double getTotal(double array[]);
double getAverage(double array[], int);
double getLowest(double array[]);
double getHighest(double array[]);
void sortArray(double array[], int);
void showOrder(const double array[], const string array2[], int);
/*/////////////////////////////////////////////
Allows user to enter data for rainfall amount
then calls functions back to main to display
results to the screen. Uses a const string for
the months in order to correlate the user
entered data with the correct month.
//////////////////////////////////////////////*/
int main()
{
char choice;
do {
double months[SIZE];
const string monthName[SIZE] = { "January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December" };
int i = 0;
for (double & rainfall : months)
{
cout << "Enter the total rainfall for " + monthName[i] + "." << endl;
cin >> rainfall;
i++;
}
cout << "The total rainfall for the year was " << getTotal(months)
<< " inches." << endl;
cout << "The average monthly rainfall for the year was "
<< getAverage(months, SIZE) << " inches." << endl;
int month = getLowest(months);
cout << "The month with the lowest rainfall was " << monthName[month]
<< " with " << months[month] << " inches." << endl;
month = getHighest(months);
cout << "The month with the highest rainfall was " << monthName[month]
<< " with " << months[month] << " inches.\n" << endl;
cout << "The rainfall per month is listed below in descending order\n" << endl;
//Call functions with months array, monthName array, and SIZE data as parameters
sortArray(months, SIZE);
showOrder(months, monthName, SIZE);
//Repeat program?
cout << "Would you like to run this program again? Y/N?" << endl;
cin >> choice;
}
while (choice == 'Y' || choice == 'y');
return 0;
}
// Function calculates total rainfall
double getTotal(double array[])
{
double total = 0;
for (int count = 0; count <= (SIZE - 1); count++)
{
total += array[count];
}
return total;
}
// Function calculates average rainfall based on user input
double getAverage(double array[], int size)
{
double total = 0;
double average = 0;
for (int count = 0; count <= (SIZE - 1); count++)
{
total += array[count];
}
average = total / size;
return average;
}
// Function calculates lowest rainfall month during year
double getLowest(double array[])
{
double lowest = array[0];
int month = 0;
for (int count = 1; count <= (SIZE - 1); count++)
{
if (lowest > array[count])
{
lowest = array[count];
month = count;
}
}
return month;
}
// Function calculates highest rainfall month during year
double getHighest(double array[])
{
double highest = array[0];
int month = 0;
for (int count = 1; count <= (SIZE - 1); count++)
{
if (highest < array[count])
{
highest = array[count];
month = count;
}
}
return month;
}
// Function sorts array elements high to low
void sortArray(double array[], int size) {
int temp;
bool swap;
do {
swap = false;
for (int count = 0; count < size - 1; count++)
{
if (array[count] < array[count + 1]) {
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
//Function to display the results to the user with a column for months and a column for rainfall data
void showOrder(const double array[], const string name[], int size) {
cout << "Month\t\tRainfall\n" << endl;
cout << "_________________________________\n";
for (int index = 0; index < size; index++) {
cout << name[index] << "\t\t" << setw(8) << array[index] << endl;;
}
}
|