Here is the problem:
Write a program that displays the name of each month in a year and its rainfall amount, sorted in order of rainfall from highest to lowest. The program should use an array of structures, where each structure holds the name of a month and its rainfall amount. Use a constructor to set the month names. Make the program modular by calling on different functions to input the rainfall amounts, to sort the data, and to display the data.
I got the function that asks for user input to work but I am trying to figure out how to call the sort function with the users data so I can sort it. However I think I screwed it up big time. I followed the book's example on how to run the sort but I have a feeling it is incorrect. Sorry I cannot comprehend this info that well. I am a PC hardware specialist after all. New flavor of IT that I am trying to understand. Thanks for all the help in advance and if you can explain what you are doing so I can comprehend better I would greatly appreciate it as well.
Here is my code:
#include <iostream>
#include <string>
using namespace std;
void rainfall(int [], string [], int);
void selectionSort(int [], string [], int);
int main()
{
int const MONTHS = 12;
int values[MONTHS];
string name[MONTHS] = {"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"};
rainfall(values, name, MONTHS);
selectionSort(values, name, MONTHS);
return 0;
}
void rainfall(int values[], string name[], int MONTHS)
{
int rain;
for(int month = 0; month <= MONTHS -1; month++)
{
cout << "Enter the total rainfall for " << name[month] << ": ";
cin >> rain;
if(rain < 0)
{
cout << "The number you have entered is invalid." << endl;
cout << "Please reenter valid number: ";
cin >> rain;
}
values[month] = rain;
}
}
void selectionSort(int [], string [], int MONTHS)
{
int startscan,
minIndex,
minValue;
for(startScan = 0; start Scan < (MONTHS - 1); startScan++)
{
minIndex = startScan;
minValue = values[month];
for(int index = startScan + 1; index < MONTHS; index++)
{
if(values[month] < minValue)
{
minValue = values[index];
minIndex = index;
}
}
values[minIndex] = values[startScan];
values[startScan] = minValue;
}
}