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;
	}
}

Based on your specification, you'll need to revamp your code a little bit to use a structure. So you'll need it outside of main() like:

struct rainfall_data
{
      int amount;
      string month;
      
       rainfall_data(string monthname){month = monthname;}
       //you can read anything on class constuctors too because they are
         //virtually identical
};

Then you'll need an array of those rainfall_data yearly[12]; I didn't examine your sort too carefully but it looks like it's pretty good. Now you just need to sort based on rainfall.
(see this for some info as to how you should access the members

What happens if you flip right and left of the = operator in line 57 around?

I tried to switch line 57 around, the errors I received did shorten. Now I only get errors stating: "values" and "months" undeclared identifier. Shouldn't those identifiers have come in from the "void rainfall()" function?

void selectionSort(int [], string [], int MONTHS)
{
	int startScan,
		minIndex,
		minValue;

	for(startScan = 0; startScan < (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[startScan] = values[minIndex];
		values[startScan] = minValue;
	}
}

Functions know nothing about variables declared in other functions. If you want selectionSort() to use variables in another function then those variables have to be passed to selectionSort() as parameters.

in function prototypes you don't need to name the variables acting as arguments/parameters. In function definitions you have to name the argument/parameter being passed in addition to it's type.

OK I got rid of the errors I was receiving but now I cannot get it to sort the information. This is my new code.

#include <iostream>
#include <string>
using namespace std;

int 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;
}
int rainfall(int values[], string name[], int MONTHS)
{		
	for(int month = 0; month <= MONTHS -1; month++)
	{
		cout << "Enter the total rainfall for " << name[month] << ": ";
		cin >> values[MONTHS];

		if(values[MONTHS] < 0)
		{
		cout << "The number you have entered is invalid." << endl;
        cout << "Please reenter valid number: ";
        cin >> values[MONTHS];
		}
	}
	return values[MONTHS];
}
void selectionSort(int values[], string name[], int MONTHS)
{
	int startScan,
		minIndex,
		minValue;

	for(startScan = 0; startScan < (MONTHS - 1); startScan++)
	{
		minIndex = startScan;
		minValue = values[MONTHS];

		for(int index = startScan + 1; index < MONTHS; index++)
		{
			if(values[MONTHS] < minValue)
			{
				minValue = values[index];
				minIndex = index;
			}
		}
		values[minIndex] = values[startScan];
		values[startScan] = minValue;
	}
}

but you didn't use struct ... did you?

I dont even know what struct is. I am new to this and this is the last of my homework for the semester and final tomorrow. I am trying to comprehend all this programming talk and it's no easy task as you can see on my behalf. Sorry if I am dumb founded by all this. I tried using the bubble sort to see if that was easier and I do not get any errors but it will not display the results after the bubble sort. Here is my new code. Like I said I am new and I am tryihg to figure this out.

#include <iostream>
#include <string>
using namespace std;

int 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);

	cout << "Here is the sorted values\n";
	selectionSort(values, name, MONTHS);

	return 0;
}
int rainfall(int values[], string name[], int MONTHS)
{		
	for(int month = 0; month <= MONTHS -1; month++)
	{
		cout << "Enter the total rainfall for " << name[month] << ": ";
		cin >> values[month];

		if(values[month] < 0)
		{
		cout << "The number you have entered is invalid." << endl;
        cout << "Please reenter valid number: ";
        cin >> values[month];
		}
	}
	return values[MONTHS];
}
void selectionSort(int values[], string name[], int MONTHS)
{
	int temp;
	bool swap;
	
	do
	{ swap = false;
	  for(int count = 0; count < MONTHS - 1; count++)
	  {
		  if(values[count] > values[count + 1])
		  {
			  temp = values[count];
			  values[count] = values[count + 1];
			  values[count + 1] = temp;
			  swap = true;
		  }
	  }
	} while(swap);
}

I feel for you ^^
are you saying that your sorting function don't work properly ?

if so .. why don't you try this simple one
changing it to your purpose

# include <iostream>
using namespace std;

void swap(int &x, int &y);
void selection_sort(int b[], int size);

int main()
{
	int numbers[3];
    int size=3;
		
	cout <<"Please enter 3 numbers"<<endl;
	for(int i=0; i<size; i++)
	{
		cin>> numbers[i];
	}

 selection_sort(numbers,  size);

	for(int i=0; i<size; i++)
	{
		cout << numbers[i]<<" ";
	}
 
}

void swap(int &x, int &y)
{
	int temp;
	temp=x;
	x=y;
	y=temp;
}

void selection_sort(int b[], int size)
{
	for (int j=0; j<size-1; j++)
	{
		int min = b[j];
		int index_min=j;

		for ( int i=j+1; i<size; i++)
		{
			if (b[i]<min)
			{
				index_min=i;
				min=b[i];
			}
		}
		if (index_min!=j)
			swap(b[j], b[index_min]);
	}
}

Good luck in your exams!

Thanks for the good luck, I will need it. I understand the code you have there somewhat and I am sure I can get my sort to work but in the homework problem it says to use functions so I assume I need to call a function for the user to enter, then return the value to main, have my sort function call on that value to sort the data. That is the issue I am having. If i dont need to call a function to have the user input the data then I am sure I can get the sort function to work. This damn book just has plain examples nothing detailed. Now do you see my dilema?

functions shouldn't set you back and ruin your code

you just place a protoype
then the main
then the function

--

call the function from the main using the variables of the function you created with no types

If I had no bus to catch I'd trace your code..

when do you need that done?

I just need to turn it in tomorrow by 5:30pm CST.

The program in post #9 works ok for me after making one small change. You are calling the sort function twice, and not displaying the results after sort is done.

cout << "Here is the sorted values\n";
    for(int i = 0; i < MONTHS; i++)
        cout << values[i] << " ";
	//selectionSort(values, name, MONTHS);

I put your line of code nto the sort function and it worked but it was from lowest to highest. When I changed < to > it prints out nothing. I even tried >= MONTHS; but that didnt work either. How would I sort it highest to lowest and print out the name of the month with the number?
Thanks for the input btw~

The ordering is not done in the code snipped I posted, but in the sort function you posted. On line 48 of post #9, change > to <

>>When I changed < to > it prints out nothing.
Of course it will print nothing because 0 is never > MONTHS.

Yeah I got that part. I put it in the sort function and it sorted it from lowest to highest but I need it sorted from highest to lowest. When I change it to the Greater than sign > it fails.

#include <iostream>
#include <string>
using namespace std;

int 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;
}
int rainfall(int values[], string name[], int MONTHS)
{		
	for(int month = 0; month <= MONTHS -1; month++)
	{
		cout << "Enter the total rainfall for " << name[month] << ": ";
		cin >> values[month];

		if(values[month] < 0)
		{
		cout << "The number you have entered is invalid." << endl;
        cout << "Please reenter valid number: ";
        cin >> values[month];
		}
	}
	return values[MONTHS];
}
void selectionSort(int values[], string name[], int MONTHS)
{
	int temp;
	bool swap;
	
	do
	{ swap = false;
	  for(int count = 0; count < MONTHS - 1; count++)
	  {
		  if(values[count] > values[count + 1])
		  {
			  temp = values[count];
			  values[count] = values[count + 1];
			  values[count + 1] = temp;
			  swap = true;
		  }
	  }
	} while(swap);
	cout << "Here are the sorted values\n";
	for (int index = 0; index > MONTHS; index++)
		cout << values[index] << " ";
}

line 55 doesnt work because I have the sort function setup incorrectly?

I already told you the problem -- line 55 doesn't work because 0 is never > than MONTHS. You changed the wrong line. You should have changed line 45, not line 55. Put line 55 back the way it was with < character.

Aswesom, You are the MAN! Thank you very much!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.