sorting char arrays, assistance please

Mk so, I have to do this program for class,input rainfall for 6 months, total them, output total, average, highest month, and lowest month.
Here's what I've got so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iomanip>
#include <iostream>
using namespace std;

int main()
{
char month[6][12]={"January","February","March","April","May","June"};
char holdermonth[6][12]={};
double rain[6];
double rainamt, rainavg,raintot,holdernum;
holdernum=0;
raintot=0;
//    input for rainfall amount
for (int n=0;n<6;n++)
	{
	cout<<"Input Rainfaill amount for the month of "<<month[n]<<endl;
	cin>>rainamt;
	rain[n]=rainamt;
	}
//     totaling for 6 months of rain and average
for (int n=0;n<6;n++)
	{
	raintot=raintot+rain[n];
	}
rainavg=raintot/6;
cout<<"raintot="<<raintot<<endl;
cout<<"rainavg="<<rainavg<<endl;

//    random cout to make sure month array will output correctly 
for (int n=0; n<6;n++)
 {cout<<month[n]<<endl;}

//Bubble Sort Ascending Order
for (int n=0; n<6;n++)
{
	for (int y=0; y<5; y++)
	{
		if (rain[y]>rain[y+1])	
		{
			holdernum=rain[y];
			rain[y]=rain[y+1];
			rain[y+1]=holdernum;
//Problem Area Here VVVVVVVV	
			holdermonth[y][12]=month[y][12];
			month[y][12]=month[y+1][12];
			month[y+1][12]=holdermonth[y][12];
//Problem Area There ^^^^^^^^
		}
	}
}
//  cout to test bubble sort
for (int n=0; n<6;n++)
{
cout<<"month "<<month[n]<<" rain "<<rain[n]<<endl;
}
return 0;
}


The problem I'm getting is the program errors out when I input a larger number
in any of the 1st 5 array slots than what is in the 6th slot I get:

Run-Time Check Failure #2 Stack around variable 'month' was corrupted.

What am I doing wrong in the code to cause this?
Also, If I have the input for rain[6] looking like this:
[0]=5,[1]=4,[2]=1,[3]=8,[4]=12,[5]=10,[6]=14
No error, and it sorts the numbers correctly, but it only changes the 1st letter of the months. How do I sort char arrays?
An array allocated to hold 12 elements has indices from 0 to 11.
Well changed
1
2
3
			holdermonth[y][12]=month[y][12];
			month[y][12]=month[y+1][12];
			month[y+1][12]=holdermonth[y][12];

to
1
2
3
			holdermonth[y][11]=month[y][11];
			month[y][11]=month[y+1][11];
			month[y+1][11]=holdermonth[y][11];

And wah lah, no errors now, but it still wont sort the months, thanks so far Hamm.
Ok, so I came up with a different way to figure out the highest and lowest rainfall months, the following code is what I've replaced the bubble sort with

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

//Hi calculator
double high=rain[0];
double low=rain[0];
int highx=0;int lowx=0;int x=0;
for (x=1;x<5;x++);
{
	if(high<rain[x])
	{
		high=rain[x];
		highx=x;
	}
}
//Low calculator
for (x=1;x<5;x++);
{
	if(low>rain[x])
	{
		low=rain[x];
		lowx=x;
	}
}

And it looks like it should work, but it doesn't and I can't come up with why not.
Help please?
Isn't rain an array of 6 elements?
for (x=1;x<5;x++);

Dude! You have ';' after your for loops! No wonder it isn't working. This is actually a good lesson and demonstrates why you should prefer std algorithms over handwritten loops!
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <algorithm>


int main()
{
	double rain[5] = { 1.1, 2.2, 1.0, 0.5, 4.4 };
	std::cout << "the highest is: " << *std::max_element(rain, rain + 5) << std::endl;
	std::cout << "the lowest is: " << *std::min_element(rain, rain + 5) << std::endl;
	
	return 0;
}

Last edited on
Topic archived. No new replies allowed.