954,487 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to find the maximum value in each column of 2D?

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[3][5]={{1,2,30,4,5},{3,41,5,6,7},{5,6,7,81,9}};

Thanks,
Vishowntar

VISHOWNTAR
Newbie Poster
8 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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[0];

	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;
}
VISHOWNTAR
Newbie Poster
8 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

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++ )
Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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.

VISHOWNTAR
Newbie Poster
8 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

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

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 
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.

[01][02][03][04][05]
[06][07][08][09][10]
[11][12][13][14][15]

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.

[01][04][07][10][13]
[02][05][08][11][14]
[03][06][09][12][15]

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

[01][02][03]
[04][05][06]
[07][08][09]
[10][11][12]
[13][14][15]
Hamrick
Posting Whiz
325 posts since Jun 2007
Reputation Points: 180
Solved Threads: 34
 

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][0];

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.

[01][02][03][04][05]
[06][07][08][09][10]
[11][12][13][14][15]

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.

[01][04][07][10][13]
[02][05][08][11][14]
[03][06][09][12][15]

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

[01][02][03]
[04][05][06]
[07][08][09]
[10][11][12]
[13][14][15]
VISHOWNTAR
Newbie Poster
8 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

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[0];

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

  return Maximum;
}

int main() {
  int a[][5]= {
    {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;
}
Hamrick
Posting Whiz
325 posts since Jun 2007
Reputation Points: 180
Solved Threads: 34
 

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

VISHOWNTAR
Newbie Poster
8 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

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.

vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 

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.
VISHOWNTAR
Newbie Poster
8 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 
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
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
 

Thanks Vijayan, appreciated.

VISHOWNTAR
Newbie Poster
8 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

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


using namespace std;
int b[6][3];
int temp;
int max(const int *array, const int num){
int maximum = array[0];
for(int i =0; i

VISHOWNTAR
Newbie Poster
8 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You