Find Row in 2D array

So im working on a small program that allow user to input low and high weather for each month. Im having trouble outputting the Month that the weather is lowest and highest. I can find the actual lowest and highest temperatures but I have no idea about the months.
const int MONTHS = 12;
const int LOHI = 2;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int FindMax(const int temp[MONTHS][LOHI])
{  
	int maxindex = 0;

	for(int i=0; i < MONTHS; i++)
	{
		
		for(int j = 0; j < LOHI; j++)
		{ 
			if(temp[i][j] > maxindex) 
			{
				maxindex = temp[i][j];
			}
		}	
	} 

	return maxindex;
}

Last edited on
1
2
3
4
5
//...
const std::string month_names[] = {"January", "February", //...
maxmonth = FindMax(temp);
std::cout << "Maximum weather was in " << month_names[maxmonth] << 
             " with amount" << temp[maxmonth][1];

You should modify your FindMax function to return index.
If I understood you correctly you can rewrite your function the following way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int FindMax( const int temp[][LOHI], int months )
{  
	int maxrow = 0;

	for ( int i = 1; i < months; i++ )
	{
		
		if ( temp[maxrow][1] < temp[i][1] ) 
		{
			maxrow = i;
		}
	} 

	return maxrow;
}



It can be called the following way

1
2
3
4
int maxrow = FindMax( YourArray, MONTHS );

std::cout << "The high weather " << YourArray[maxrow][1]
              << " was in month # " << maxrow << std::endl;

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
#include <iostream>
#include <string>

using namespace std;

//fp
void compare(int [], int [], int [], int []);
void show(int [], int [], string []);

int main()
{
	//declare and initialization
	const int month = 12;
	string month_name[month] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
	int tempe1[month] = {12,85,36,15,99,62,19,62,43,22,71,66};
	int tempe2[month] = {55,25,67,80,21,14,42,83,24,60,94,56};
	int tempeh[month], tempel[month];

	compare(tempe1, tempe2, tempeh, tempel);

	show(tempeh, tempel, month_name);

	cin.get();
	cin.get();
}

void compare(int t1 [], int t2 [], int h [], int l [])
{
	for (int i = 0; i < 12; ++i)
	{
		if (t1[i] > t2[i])
		{
			h[i] = t1[i];
			l[i] = t2[i];
		}
		else
		{
			h[i] = t2[i];
			l[i] = t1[i];
		}
	}
}

void show(int th[], int tl[], string n[])
{
	for (int i = 0; i < 12; ++i)
	{
		cout << "[" << n[i] << "] High : " << th[i] << ". Low : " << tl[i] << "." << endl; 
	}
}
@cc9355


Have I understood you correctly that you showed how code should not be written? :)
@vlad from moscow

lol
it's a bit long~ but runable : >

show me how to make it short and smart
The problem is not that the code is "bit long". The problem is that it is a bad code.

For example why do you use magic number 12?
Or consider function compare. All such functions with that name usually return a value either int or bool. Your function has return type void. What does it compare? Can someone seeing its declaration to understand what it compares?

void compare(int [], int [], int [], int []);

Why did you declare two separate arrays to store low and high values? Now you need some synchronization between these two arrays. The original declaration of the array by the author of the thread is much better than yours.

For example I would declare one array of std::pair<int, int>.

There are other remaks relative your code but if to summiarize it may be said that it is a bad code.
Last edited on
First and main problem with the code is that it doesnt do what topicstarter wants.
Second is bad naming as vlad says.
Third is, again, bad data storage.
Fourth — magic number in show(). Move const month declaration in global namespace and use it in your show function;
Fifth: double get is not a good way to stop program from closing.
Thanks all of you guys were so helpful . @vladfrommoscow can you explain to me what the 1 in temp[maxrow][1] means?
I guess that the second dimension of the array is equal to 2 and the high value is in temp[maxrow][1] while the low value is in temp[maxrow][0]
Exact problem was in this ( http://cplusplus.com/forum/general/96831/ ) topic.
@vlad from moscow @ MiiNiPaa

Thanks, and i've learnt a lot. and i find that the author just want to know the month of the highest temperature and the lowest. misunderstand.. :P

vlad,could i ask what kind of sync problem it would have if i declare two seperate arrays rather than a 2d array?

MiiNiPaa, is that a good way to use system("pause") instead of cin.get()? or some other ways?
Last edited on
Topic archived. No new replies allowed.