I was assigned to create a program that lets the user enter the total rainfall for each of the 12 months, into an array of doubles. Then it needed to calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts. Then sort and display the months based on their descending rainfall amount.
Here's what I'm really having trouble with. How do I tell what element position the data came out of from my double array? I need to know that, at least I think I do to tell my char array which element to display for the name of the month with the right rainfall amount.
Can anyone help me please? I'm new to this and am really stuck. Any info would be greatly appreciated!!
// This program allows the user to enter the amount of rainfall for each month.
// It can then calculate the total rainfall for the year as well as the average monthly rainfall,
// and the months with the highest and lowest amounts of rain.
#include <iostream>
#include <iomanip>
using namespace std;
// Function Prototypes
double sumArray(double[], int);
double getHighest(double[], int);
double getLowest(double[], int);
void sortArray(double [], int);
void showArray(double [], int);
int main()
{
double total;
double average;
double highest;
double lowest;
const int MONTHS = 12;
const int cols = 2;
const int string_SIZE = 10;
double values[MONTHS]; // An Array for the values
// The rain Array contains the months for the data to be entered
char rain[MONTHS][string_SIZE] =
{ "January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December" };
// dynamically ask the user for input.
for (int count = 0; count < MONTHS; count++)
{
cout << "Enter the amount of rainfall for "<<rain[count]<< " in inches."<<endl;
cin>>values[count];
}
cout<<"\n";
cout<<"\n";
// Get the total rainfall.
total = sumArray(values, MONTHS);
// Calculate the average.
average = total / MONTHS;
// Find the highest sales amount.
highest = getHighest(values, MONTHS);
// Find the lowest sales amount.
lowest = getLowest(values, MONTHS);
// Display the total
cout<<"The total amount of rainfall for the year is: "<<setprecision(3)<<total<<" inches."<<endl;
cout<<"\n";
cout<<"\n";
// Display the average
cout<<"The average amount of rainfall for the year is: "<<setprecision(3)<<average<<" inches."<<endl;
cout<<"\n";
cout<<"\n";
// Display the highest
cout<<"The highest amount of rain was in "<<" with: "<<highest<<" inches."<<endl;
cout<<"\n";
cout<<"\n";
// Display the lowest
cout<<"The lowest amount of rain is: "<<lowest<<" inches."<<endl;
cout<<"\n";
cout<<"\n";
// Display the values.
cout << "The unsorted values are:\n";
showArray(values, MONTHS);
// Sort the values.
sortArray(values, MONTHS);
// Display them again.
cout << "The sorted values are:\n";
showArray(values, MONTHS);
return 0;
}
double sumArray(double array[], int size)
{
double total = 0; // Accumulator
for (int count = 0; count < size; count++)
total += array[count];
return total;
}
double getHighest(double array[], int size)
{
double highest;
highest = array[0];
for (int count = 1; count < size; count++)
{
if (array[count] > 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(double array[], int size)
{
bool swap;
int temp;
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);
}
//*************************************************************
// 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;
}