Hi,
I am new in programming. I would appreciate if anyone can help me out to find maximum value of each column in the following matrix.

int array={{1,2,30,4,5},{3,41,5,6,7},{5,6,7,81,9}};

Thanks,
Vishowntar

## All 14 Replies

Would you know how to find the max value in a 1-D array, say {3,41,5,6,7} ?

It's exactly the same, only you change the way you index the data, that's all.

Hi Salem,

Thank you so much for your help. Here is my program for 1D but couldn't figure out how to change it to 2D.

``````#include <cstdlib>
#include <iostream>

using namespace std;

int max(const int *array, const int count)
{
int Maximum = array;

for(int i = 0; i < count; i++)
if( Maximum < array[i] )
Maximum = array[i];

return Maximum;

}
int main(int argc, char *argv[])
{
int a[]= {3,41,5,6,7};
int count = sizeof(a)/sizeof(int);
int Maximum = max(a, count);
cout << "Maximum: " << Maximum << endl;

system("PAUSE");
return EXIT_SUCCESS;
}``````

Compare

``````for ( row = 0 ; row < 3 ; row++ )
for ( col = 0 ; col < 5; col++ )``````

for say averaging each row...

Vs.

``````for ( col = 0 ; col < 5; col++ )
for ( row = 0 ; row < 3 ; row++ )``````

Compare

``````for ( row = 0 ; row < 3 ; row++ )
for ( col = 0 ; col < 5; col++ )``````

for say averaging each row...

Vs.

``````for ( col = 0 ; col < 5; col++ )
for ( row = 0 ; row < 3 ; row++ )``````

Hi Salem,

Thanks for your quick response. I am sorry, I couldn't understand. Will you please give me little detail hint? Unlike in 1D, i don't have a constant to compare with.

That's as close as I can get without giving you the answer on a plate.

That's as close as I can get without giving you the answer on a plate.

Everybody doesn't think in code, and that example is pretty vague.

Will you please give me little detail hint?

When you loop over a 2D array, you use two loops, right? One loop on the outside goes over the rows and another on the inside that goes over the columns. You do it like this.

``````for ( int row = 0; row < nRows; ++row ) {
for ( int col = 0; col < nCols; ++col ) {
// do something with array[row][col]
}
}``````

When you do that it goes over the array like this with the numbers showing the order that you visit each cell.

``````

``````

That's because the outer loop handles the rows and the inner loop handles the columns. But if you reverse the loops so that the outer loop handles the columns and the inner loop handles the rows, it goes over the array like this.

``````

``````

You do that with code like this.

``````for ( int col = 0; col < nCols; ++col ) {
for ( int row = 0; row < nRows; ++row ) {
// do something with array[row][col]
}
}``````

What that loop does is pretend the columns are rows and kind of flips the array over and rotates it by by 90 degrees clockwise. But you do it without really flipping and rotating, you just pretend that it's that way by changing the order that you work with the cells. :)

``````



``````

Hi Hamrick,

Thank you so much for the detail explanation. However, I still have problem. I flip the matrix but still couldn't figure out how to find the maximum value in each row.

As I set try to initialize value like this:

int maximum[row] = maximum[row];

it doesn't take, saying that you can not initialize with a variable. I am sorry I am giving trouble to you guys but I will appreciate your help. I am quite new in this field.

How can i treat each column individually?

Regards,

Everybody doesn't think in code, and that example is pretty vague.

When you loop over a 2D array, you use two loops, right? One loop on the outside goes over the rows and another on the inside that goes over the columns. You do it like this.

``````for ( int row = 0; row < nRows; ++row ) {
for ( int col = 0; col < nCols; ++col ) {
// do something with array[row][col]
}
}``````

When you do that it goes over the array like this with the numbers showing the order that you visit each cell.

``````

``````

That's because the outer loop handles the rows and the inner loop handles the columns. But if you reverse the loops so that the outer loop handles the columns and the inner loop handles the rows, it goes over the array like this.

``````

``````

You do that with code like this.

``````for ( int col = 0; col < nCols; ++col ) {
for ( int row = 0; row < nRows; ++row ) {
// do something with array[row][col]
}
}``````

What that loop does is pretend the columns are rows and kind of flips the array over and rotates it by by 90 degrees clockwise. But you do it without really flipping and rotating, you just pretend that it's that way by changing the order that you work with the cells. :)

``````



``````

All you want to do is find the maximum of each row? You already have a function that finds the maximum of a single row, why can't you call it on each row in a loop?

``````#include <cstdlib>
#include <iostream>

using namespace std;

int max( const int *array, const int count ) {
int Maximum = array;

for ( int i = 0; i < count; i++ ) {
if ( Maximum < array[i] ) {
Maximum = array[i];
}
}

return Maximum;
}

int main() {
int a[]= {
{3,41,5,6,7}
, {2,7,33,77,1}
, {9,3,7,1,8}
};

for ( int rows = 0; rows < 3; ++rows ) {
cout<<"Maximum: "<< max( a[rows], 5 ) <<endl;
}

return 0;
}``````

Hi Hamrick,

Thank you so much; you made my day.

Many thinks to Salem too. I am sorry I could not be able to figure out your hint.

Regards,
vishowntar

now that you have solved it, can you make it more efficient? ie. find the max for each row and each col by making one single pass through the elements of the array.

Hi Vijayan,

Thanks for the work. I will definitely try; please explain to me what do you mean by:

"by making one single pass through the elements of the array"

now that you have solved it, can you make it more efficient? ie. find the max for each row and each col by making one single pass through the elements of the array.

``````enum { ROWS = 3, COLS = 5 };
const int array[ROWS][COLS] = { /* ... */ } ;

int max_in_row[ROWS] = { /* ... */ }, min_in_row[ROWS] = { /* ... */ },
max_in_col[COLS] = { /* ... */ }, min_in_col[COLS] = {/* ... */ };

for( int row=0 ; row<ROWS ; ++row )
for( int col=0 ; col<COLS ; ++col )
{
// ...
}
// this is the only loop that iterates thru the elements (once).
// at this point you should have arrays max_in_row,  min_in_row,
// max_in_col, min_in_col filled with the right values``````

Thanks Vijayan, appreciated.

Hi Everyone,

Can you please find my mistake on this program? if I change the numbers in the array, the result doesn't change all the time.

``````#include <cstdlib>
#include <iostream>

using namespace std;
int b;
int temp;
int max(const int *array, const int num){
int maximum =  array;
for(int i =0; i<num; i++)
if(maximum < array[i]){
maximum = array[i];
temp =i;
}
//return maximum;
//return temp;

return b[temp];
}

int a = {{1,20,30,40,50,60},{2,2,50,70,40,60},{3, 5,8,300,15,6}};

int main(int argc, char *argv[])
{

for(int j =0; j<6; j++){
for(int i =0; i< 3; i++){

b[j][i] = a[i][j];
cout << "  " <<b[j][i];
}
cout << endl;
}
cout << endl;
for(int k =0+1; k<6; k++){
int maximum = max(b[k],3);

cout <<" " <<maximum;
}
cout << endl;

system("PAUSE");
return EXIT_SUCCESS;
}
``````

Thanking you,
Vishowntar

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.