Parallel Arrays

I'm making a simple program that is supposed to log rainfall data for a year. The program is supposed to display two months with the most and least amount of rainfall. I'm having a hard time understanding why my program can find the highest month but not the lowest when it runs. I show the whole code because I'm not sure if the issue is in the for loop or at the top. Suggestions are much appreciated!

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
58
59
60
61
62
63
64
65
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main ()
{
	int total = 0;
	double rain [12]; // holds rainfall data
	string months [12] = {"January", "February", "March", "April","May", "June", "July","August","September","October","November","December"};
	double least = rain [0];
	double most = rain [0];
	string leastmonth;
	string mostmonth; 
	

	for (int i=0; i<12; i++)		// enter rainfall for each month
	{
		cout << "Enter rain for month of " <<months[i] <<" ";
		cin >> rain [i];
		cout <<endl;
	}

	for (int x=0 ; x<12; x++)	// loop to colect monthly rainfall total
	{
		total += rain[x];
	}

	double avgrainmonth = total/12;		// average monthly rainfall

	for (int t = 1; t<12; t++)	// find month with least rainfall
	{	
		 if (rain [t] < least)
		 {
		    least = rain [t];
		    leastmonth = months[t]; 
  		  }

 	}

	for (int q = 1; q<12; q++)	// find month with most rainfall
	 {
 		if (rain[q] > most)
		 {
		    most = rain[q]; 
		    mostmonth = months[q];
		 }
	 }




cout <<"The month with the least rainfall was " <<leastmonth <<"With "<<least <<" rainfall" <<endl;

cout <<"The month with the most rainfall was " <<mostmonth <<"With "<<most <<" rainfall" <<endl;

cout <<"average rainfall is " <<avgrainmonth;

	int y;
	cin >>y;

	return 0;
}
What is exact problem? Does it shows least rainfall but not month name? Is month in question January?
line 12-13: rain[0] is uninitialized at this point. You are initializing most and least to garbage.
At lines 12 and 13, the rain array hasn't been filled with valid values yet, so they have some uninitialized garbage value.

lines 14 and 15 - these variables don't get initialized. So if the lowest or highest value happens to be in the first element, these variables won't get set, since you only set them in the if statement if the later elements are less than or greater than the first.

I moved lines 12 and 13 to after the for loop that gathers the rainfall data, and initialized the strings at line 14 and 15 to months[0] and it seems to work ok.

line 9 - I'd make this a double since you're summing up double values in the array.
closed account (j3Rz8vqX)
Logical Errors:
1
2
    double least;// = rain [0];//Error prone, rain[0] is not initialized
    double most;// = rain [0];//Error prone, rain[0] is not initialized 

Count from 0:
1
2
for (int t = 1; t<12; t++)	// find month with least rainfall
for (int q = 1; q<12; q++)	// find month with most rainfall 

Optional:
Prevent program crash in character entry:
1
2
3
4
        cout << "Enter rain for month of " <<months[i] <<" ";
        cin >> rain [i];
        cin.clear();                //Unnecessary, but removes crash from alpha input
        cin.ignore(255,'\n');       //Tries to clear cin buffer with a 255 character extraction 

Prompt the user for exit, instead of hang-like behavior:
1
2
    cout<<"\n\nPress <enter> to exit: ";//Inform the user of exit:
    cin.get();

!Spoiler!:
A working example: http://pastie.org/private/pb3kadbyx06xqwowkg5fdw
Last edited on
The problem is the the least and leastmonth variables in lines 36 & 37. I tried AbstractionAnon's suggestion and the least variable was fixed (Thanks for that! :)). for some reason though, the leastmonth variable still displays blank when I run the program.
This is how I tweaked your code.

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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int main ()
{
	double total = 0.0;
	double rain [12]; // holds rainfall data
	string months [12] = {"January", "February", "March", "April","May", "June", "July","August","September","October","November","December"};
	string leastmonth = months[0]; //initialize to first month
	string mostmonth = months[0]; //initialize to first month
	
	// enter rainfall for each month
	for (int i=0; i<12; i++)		
	{
		cout << "Enter rain for month of " <<months[i] <<" ";
		cin >> rain [i];
		total += rain[i]; //increment total
		cout <<endl;
	}

	// average monthly rainfall
	double avgrainmonth = total/12;		

	// find month with least rainfall
	double least = rain [0];//initialize to first month's rainfall
	for (int t = 1; t<12; t++)	
	{	
		 if (rain [t] < least)
		 {
		    least = rain [t];
		    leastmonth = months[t]; 
  		 }
 	}

	// find month with most rainfall
	double most = rain [0];//initialize to first month's rainfall
	for (int q = 1; q<12; q++)	
	 {
 		if (rain[q] > most)
		 {
		    most = rain[q]; 
		    mostmonth = months[q];
		 }
	 }

	cout <<"The month with the least rainfall was " <<leastmonth <<" with "<<least <<" rainfall" <<endl;
	cout <<"The month with the most rainfall was " <<mostmonth <<" with "<<most <<" rainfall" <<endl;
	cout <<"average rainfall is " <<avgrainmonth;

	return 0;
}


Enter rain for month of January 
Enter rain for month of February 
Enter rain for month of March 
Enter rain for month of April 
Enter rain for month of May 
Enter rain for month of June 
Enter rain for month of July 
Enter rain for month of August 
Enter rain for month of September 
Enter rain for month of October 
Enter rain for month of November 
Enter rain for month of December 
The month with the least rainfall was January with 1.5 rainfall
The month with the most rainfall was December with 12.5 rainfall
average rainfall is 6.90833
Last edited on
Thanks for your help everyone, I got it working. Especially wildblue, your explanation about the placement and initialization of those variables really cleared up the issue for me
Topic archived. No new replies allowed.