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

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2007
Posts: 8
Reputation: VISHOWNTAR is an unknown quantity at this point 
Solved Threads: 0
VISHOWNTAR VISHOWNTAR is offline Offline
Newbie Poster

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

 
0
  #1
Jul 17th, 2007
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
Last edited by VISHOWNTAR; Jul 17th, 2007 at 5:56 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

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

 
0
  #2
Jul 17th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 8
Reputation: VISHOWNTAR is an unknown quantity at this point 
Solved Threads: 0
VISHOWNTAR VISHOWNTAR is offline Offline
Newbie Poster

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

 
0
  #3
Jul 17th, 2007
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.
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int max(const int *array, const int count)
  7. {
  8. int Maximum = array[0];
  9.  
  10. for(int i = 0; i < count; i++)
  11. if( Maximum < array[i] )
  12. Maximum = array[i];
  13.  
  14. return Maximum;
  15.  
  16. }
  17. int main(int argc, char *argv[])
  18. {
  19. int a[]= {3,41,5,6,7};
  20. int count = sizeof(a)/sizeof(int);
  21. int Maximum = max(a, count);
  22. cout << "Maximum: " << Maximum << endl;
  23.  
  24.  
  25. system("PAUSE");
  26. return EXIT_SUCCESS;
  27. }
Last edited by Ancient Dragon; Jul 17th, 2007 at 11:40 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

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

 
0
  #4
Jul 17th, 2007
Compare
  1. for ( row = 0 ; row < 3 ; row++ )
  2. for ( col = 0 ; col < 5; col++ )
for say averaging each row...

Vs.
  1. for ( col = 0 ; col < 5; col++ )
  2. for ( row = 0 ; row < 3 ; row++ )
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 8
Reputation: VISHOWNTAR is an unknown quantity at this point 
Solved Threads: 0
VISHOWNTAR VISHOWNTAR is offline Offline
Newbie Poster

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

 
0
  #5
Jul 18th, 2007
Originally Posted by Salem View Post
Compare
  1. for ( row = 0 ; row < 3 ; row++ )
  2. for ( col = 0 ; col < 5; col++ )
for say averaging each row...

Vs.
  1. for ( col = 0 ; col < 5; col++ )
  2. 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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

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

 
0
  #6
Jul 18th, 2007
That's as close as I can get without giving you the answer on a plate.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

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

 
0
  #7
Jul 18th, 2007
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.
  1. for ( int row = 0; row < nRows; ++row ) {
  2. for ( int col = 0; col < nCols; ++col ) {
  3. // do something with array[row][col]
  4. }
  5. }
When you do that it goes over the array like this with the numbers showing the order that you visit each cell.
  1. [01][02][03][04][05]
  2. [06][07][08][09][10]
  3. [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.
  1. [01][04][07][10][13]
  2. [02][05][08][11][14]
  3. [03][06][09][12][15]
You do that with code like this.
  1. for ( int col = 0; col < nCols; ++col ) {
  2. for ( int row = 0; row < nRows; ++row ) {
  3. // do something with array[row][col]
  4. }
  5. }
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.
  1. [01][02][03]
  2. [04][05][06]
  3. [07][08][09]
  4. [10][11][12]
  5. [13][14][15]
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 8
Reputation: VISHOWNTAR is an unknown quantity at this point 
Solved Threads: 0
VISHOWNTAR VISHOWNTAR is offline Offline
Newbie Poster

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

 
0
  #8
Jul 18th, 2007
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,


Originally Posted by Hamrick View Post
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.
  1. for ( int row = 0; row < nRows; ++row ) {
  2. for ( int col = 0; col < nCols; ++col ) {
  3. // do something with array[row][col]
  4. }
  5. }
When you do that it goes over the array like this with the numbers showing the order that you visit each cell.
  1. [01][02][03][04][05]
  2. [06][07][08][09][10]
  3. [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.
  1. [01][04][07][10][13]
  2. [02][05][08][11][14]
  3. [03][06][09][12][15]
You do that with code like this.
  1. for ( int col = 0; col < nCols; ++col ) {
  2. for ( int row = 0; row < nRows; ++row ) {
  3. // do something with array[row][col]
  4. }
  5. }
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.
  1. [01][02][03]
  2. [04][05][06]
  3. [07][08][09]
  4. [10][11][12]
  5. [13][14][15]
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

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

 
0
  #9
Jul 18th, 2007
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?
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int max( const int *array, const int count ) {
  7. int Maximum = array[0];
  8.  
  9. for ( int i = 0; i < count; i++ ) {
  10. if ( Maximum < array[i] ) {
  11. Maximum = array[i];
  12. }
  13. }
  14.  
  15. return Maximum;
  16. }
  17.  
  18. int main() {
  19. int a[][5]= {
  20. {3,41,5,6,7}
  21. , {2,7,33,77,1}
  22. , {9,3,7,1,8}
  23. };
  24.  
  25. for ( int rows = 0; rows < 3; ++rows ) {
  26. cout<<"Maximum: "<< max( a[rows], 5 ) <<endl;
  27. }
  28.  
  29. return 0;
  30. }
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 8
Reputation: VISHOWNTAR is an unknown quantity at this point 
Solved Threads: 0
VISHOWNTAR VISHOWNTAR is offline Offline
Newbie Poster

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

 
0
  #10
Jul 18th, 2007
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC