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

Lets do some sorting.

First of all the sorting technique you are using is sort by swapping. famous as a bubble sort.
before sorting lets improve your code. which is more strutured

Why are you keeping the two related things separate? why don't keep these two things rainfall & month as an aggregate. here comes structure.

struct RainFallInfo { 
     double rainFall;         // stores information about rain
     char month[10];       // stores information about month.
};

how to sort this.
is simple

void Swap (RainFallInfo& first, RainFallInfo& second)
{
		// swapping technique...
		RainFallInfo temp;
		temp.rainFall = first.rainFall;
		strcpy (temp.month, first.month);

		first.rainFall = second.rainFall;
		strcpy (first.month, second.month);

		
		second.rainFall = temp.rainFall;
		strcpy (second.month, temp.month);
}


void Sort (RainFallInfo rInfo[], int MONTHS)
{
	for (int i=0; i<MONTHS; ++i)
	{
		for (int j=i+1; j<MONTHS; ++j)
		{
			if (rInfo[i].rainFall> rInfo[j].rainFall)
			{
					Swap(rInfo[i], rInfo[j]);
			}
		}
	}
}

Hope you understand.

where you lack can be seen by comparing my sort function with yours.
like these.

startIndex < (MONTHS - 1); // should be replaced by MONTHS

strcpy is required to copy char array to other char array.
Hope this helps...

if you don't know references, 'RainFallInfo&' you can replace this by RainFallInfo* and do the same.

strcpy is not required as you're copying the whole structure. So the swap function could be as simple as:

void Swap(RainFallInfo& first, RainFallInfo& second){
   // swapping technique...
   RainFallInfo temp = first;
   first = second;
   second = temp;
   }

dougy83
thanks, I know this is the portable and most suitable way to swap, there exists several other swapping techniques, but I was concentrating on technique adopted by Yellowdog428. I want him to find out this technique himself.

Thanks anyway.

Thank you for your help!

Problem is I have not learned structures yet....

is there another way to do it without them?

Thanks again

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...

You can't assign arrays in the same way that you can assign an int, long, double, etc.

You can use strcpy/strncpy for copying the contents of the char arrays:

strcpy(swapMonth, month[startIndex]);
				strcpy(month[startIndex], month[count]);
				strcpy(month[count], swapMonth);

char swapMonth[12][10],
highestMonth[12][10];

You should realise that swapMonth and highestMonth only needs to hold a single month, so use:

char swapMonth[10],
		highestMonth[10];

Any pointers would be great!

So you like pointers then?? The strcpy can be avoided if we swap pointers instead. I have a feeling that this will confuse more than it's worth so I won't include any info, unless you want it -- so stick to the strcpy method shown above.

Thanks guys!

I used the strcpy command like dougy83 suggested!

void sortString(double rainfall[], char month[][10], int MONTHS)
{
	double swapRain = 0,
		highest = 0;
	char swapMonth[10],
		highestMonth[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];
				strcpy(highestMonth, month[count]);
				swapRain = rainfall[startIndex];
				strcpy(swapMonth, month[startIndex]);
				rainfall[count] = swapRain;
				strcpy(month[count], swapMonth);
				rainfall[startIndex] = highest;
				strcpy(month[startIndex], highestMonth);
			}
		}
	}
}

works like a charm!

This question has already been answered. Start a new discussion instead.