### Finding Lowest/Highest in Multi-dimensional Array

I have this multi-dimensional array. There are 12 rows, each represents the months, and 2 columns, the 1st column represents the lowest temperature and the 2nd column represents the highest temperature.

I need to find the lowest temperature in only the first column and the highest in only the 2nd column. How would I go about this?

This takes in the temperatures. LOHI = 2, MONTHS = 12, LOW = 0, HIGH = 1

 ``12345678910111213141516`` ``````void PopulateTemp(int temp[][LOHI]) { for(int month = 0; month < MONTHS; month++) { cout << "Month " << month+1 << endl; temp[month][LOW] = ReadTemp(" LOW: "); temp[month][HIGH] = ReadTemp(" High: "); while (temp[month][HIGH] < temp[month][LOW]) { cout << " -- Low must be lower than high. Derp!" << endl; temp[month][LOW] = ReadTemp(" LOW: "); temp[month][HIGH] = ReadTemp(" High: "); } } }``````

Here is my approach to it:

 ``12345678910111213141516`` ``````int FindLow(const int temp[][LOHI]) { int low = temp[0][0]; for(int i = 0; i < 1; i++) { for(int j = 0; j < MONTHS; j++) { if(temp[i][j] < low) { low = temp[i][j]; return low; } } } }``````

My professor said we only needed 1 for loop as you only need to read the data from one column in each?

Thank you.
 ``12345678`` ``````for(int j = 0; j < MONTHS; j++) { if(temp[i][0] < low) { low = temp[i][0]; } if (temp[i][1] > high) { high = temp[i][1] } }``````
@MiiNiPaa Is that suppose to be only 1 loop? Because you're using j and i in the same loop.

Also, that doesn't seem to work as it always just gives me the first input which is temp[0][0].

Here is how I'm doing it:

 ``1234567891011`` ``````int FindLow(const int temp[][LOHI]) { int low = temp[0][0]; for(int j = 0; j < MONTHS; j++) { if(temp[j][0] < low) { low = temp[j][0]; } } return low; }``````

And how will I cout it? Like this:

 ``12345`` ``````void ReportTemp(const int temp[][LOHI], int low) { cout << "Lowest: " << temp[low][0]; }``````

or just

`cout << "Lowest: " << low;`
?
change all i to j. I was distracted midway and made a mistake.
In low variable you store the value of lowest temperature, not index, so you should just do `std::cout << "Lowest: " << low;`

Maybe temp[0][0] is the lowest temperature?
I figured, so I changed them. However, that did not help me find the lowest value. I am making sure that the lowest input is not in temp[0][0] but it keeps returning that for the value of low.

Maybe it has something to do with initializing low to = temp[0][0]? And the value of low is never changed?
To find low:

 ``1234567891011121314`` ``````int FindLow(const int temp[][LOHI]) { int low = temp[0][0]; for(int j = 0; j < MONTHS; j++) { if(temp[j][0] < low) { low = temp[j][0]; } } return low; } }``````

and it is being outputted here:

 ``123456789101112131415161718192021`` ``````void ReportTemp(const int temp[][LOHI], int low) { const string MONSTR[MONTHS] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; cout << endl; cout << "FULLERTON" << endl; cout << "MONTH" << setw(4) << "|" << setw(6) << "LOW" << setw(2) << "|" << setw(6) << "HIGH" << " |" << endl; for(int i = 0; i < MONTHS; i++) { cout << MONSTR[i] << setw(6) << "|"; for(int j = 0; j < LOHI; j++) { cout << setw(6) << temp[i][j] << setw(2) << "|"; } cout << endl; } cout << "Lowest: " << low; }``````

The way it is now, it is just outputting 0 for low every time.
Last edited on
And how these functions are used?
Because FindLow function works fine. I have tested it.
Last edited on
Here's the whole program:
[code]
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

const int MONTHS = 12;
const int LOHI = 2;
enum{LOW, HIGH};

void PopulateTemp(int temp[][LOHI]);
int FindLow(const int temp[][LOHI]);
void ReportTemp(const int temp[][LOHI], int low);

template <class T>

int main()
{
int temp[MONTHS][LOHI];
int low;

low = FindLow(temp);

PopulateTemp(temp);
ReportTemp(temp, low);

return 0;
}

It asks the user to enter a low and high temperature for each month and at the end I'm suppose to output the lowest temperature under the low column and the highest temperature under the high column.
Last edited on
In your main(): You are finding lowest value in uninitializated array and then inialize it. Don't you think it should be other way around?
@MiiNiPaa Wow! I was so focused on the function it self that I completely forgot about main. You were right,

I had to call FindLow() after PopulateTemp(). Thank you very much.
Last edited on
Topic archived. No new replies allowed.