Need some help.
I have an assignment that calls for the sorting of two arrays. One is an array of doubles, the other chars (two dimensional). I cannot figure it out for the life of me.
Here is the whole program.
#include<iostream>
#include<iomanip>
using namespace std;
//Function Prototypes
double sumRain(double[], int);
double getHighest(double[], int);
double getLowest(double[], int);
void sortString(double[], char[][10], int);
void displaySort(double[], char[][10], int);
void displayResults(double total, double average, double lowest, double highest);
int main()
{
const int MONTHS = 12;
double rainfall[MONTHS] = {0},
total = 0,
average = 0,
highest = 0,
lowest = 0;
char month[MONTHS][10] = {"January",
"Febuary",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"};
//Collect the rainfall for each month and store the data in an array
for (int count = 0; count < MONTHS; count++)
{
cout << "Please enter the the rainfall for " << month[count] << endl;
cin >> rainfall[count];
if (rainfall[count] < 0)
{
cout << "You MUST enter an amount greater or equal to zero!\n";
cout << "Please enter the rainfall for the " << month[count] << " month.\n";
cin >> rainfall[count];
}
}
//Calculate the total rainfall
total = sumRain(rainfall, MONTHS);
//Calculate the average
average = total / MONTHS;
//Find the month with the highest rainfall
highest = getHighest(rainfall, MONTHS);
//Find the month with the lowest rainfall
lowest = getLowest(rainfall, MONTHS);
//Display results
displayResults(lowest, highest, average, total);
//Sort The strings
sortString(rainfall, month, MONTHS);
//display the sorted strings
displaySort(rainfall, month, MONTHS);
return 0;
}
void displayResults(double total, double average, double lowest, double highest)
{
//Format output
cout << fixed << showpoint << setprecision(2);
//Display results
cout << "The total rainfall for the year is " << total << endl;
cout << "The average rainfall is " << average << endl;
cout << "The the lowest amount of rain for any given month was " << lowest << endl;
cout << "The the highest amount of rain for any given month was " << highest << endl;
}
double sumRain(double rainfall[], int MONTHS)
{
double total = 0;
for (int count = 0; count < MONTHS; count++)
{
total += rainfall[count];
}
return total;
}
double getHighest(double rainfall[], int MONTHS)
{
double highest = rainfall[0];
for (int count = 0; count < MONTHS; count++)
{
if (rainfall[count] > highest)
highest = rainfall[count];
}
return highest;
}
double getLowest(double rainfall[], int MONTHS)
{
double lowest = rainfall[0];
for (int count = 0; count < MONTHS; count++)
{
if (rainfall[count] < lowest)
lowest = rainfall[count];
}
return lowest;
}
void sortString(double rainfall[], char month[][10], int MONTHS)
{
double swapRain = 0,
highest = 0;
char swapMonth[12][10],
highestMonth[12][10];
for (int startIndex = 0; startIndex < (MONTHS - 1); startIndex++)
{
highest = rainfall[startIndex];
swapRain = rainfall[startIndex];
for (int count = startIndex + 1; count < MONTHS; count++)
{
if (rainfall[count] > highest)
{
highest = rainfall[count];
swapRain = rainfall[startIndex];
swapMonth[startIndex] = month[startIndex];
rainfall[count] = swapRain;
//month[count] = swapMonth[12][10];
rainfall[startIndex] = highest;
//month[startIndex] = highestMonth[12][10];
}
}
}
}
void displaySort(double rainfall[], char month[][10], int MONTHS)
{
for (int count = 0; count < MONTHS; count++)
{
cout << "The total rain for " << month[count] << " is " << rainfall[count] << endl;
}
}
The function that I am sorting the arrays is
void sortString(double rainfall[], char month[][10], int MONTHS)
{
double swapRain = 0,
highest = 0;
char swapMonth[12][10],
highestMonth[12][10];
for (int startIndex = 0; startIndex < (MONTHS - 1); startIndex++)
{
highest = rainfall[startIndex];
swapRain = rainfall[startIndex];
for (int count = startIndex + 1; count < MONTHS; count++)
{
if (rainfall[count] > highest)
{
highest = rainfall[count];
swapRain = rainfall[startIndex];
swapMonth[startIndex] = month[startIndex];
rainfall[count] = swapRain;
//month[count] = swapMonth[12][10];
rainfall[startIndex] = highest;
//month[startIndex] = highestMonth[12][10];
}
}
}
}
I have some stuff commented because I was just trying to get the thing to compile...
What I have figured so far is I cannot assign in a char array the same that I can in a 9int or double array...
Any pointers would be great!
Jay