### 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;
 ``12345678910111213141516171819`` ``````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
 ``12345`` ``````//... 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

 ``123456789101112131415`` ``````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

 ``1234`` ``````int maxrow = FindMax( YourArray, MONTHS ); std::cout << "The high weather " << YourArray[maxrow][1] << " was in month # " << maxrow << std::endl;``````

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950`` ``````#include #include 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? :)

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