944,214 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 4465
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 17th, 2007
0

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

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
VISHOWNTAR is offline Offline
8 posts
since Jul 2007
Jul 17th, 2007
0

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

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jul 17th, 2007
0

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

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.
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
VISHOWNTAR is offline Offline
8 posts
since Jul 2007
Jul 17th, 2007
0

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

Compare
C++ Syntax (Toggle Plain Text)
  1. for ( row = 0 ; row < 3 ; row++ )
  2. for ( col = 0 ; col < 5; col++ )
for say averaging each row...

Vs.
C++ Syntax (Toggle Plain Text)
  1. for ( col = 0 ; col < 5; col++ )
  2. for ( row = 0 ; row < 3 ; row++ )
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jul 18th, 2007
0

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

Click to Expand / Collapse  Quote originally posted by Salem ...
Compare
C++ Syntax (Toggle Plain Text)
  1. for ( row = 0 ; row < 3 ; row++ )
  2. for ( col = 0 ; col < 5; col++ )
for say averaging each row...

Vs.
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
VISHOWNTAR is offline Offline
8 posts
since Jul 2007
Jul 18th, 2007
0

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

That's as close as I can get without giving you the answer on a plate.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jul 18th, 2007
0

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

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

Quote ...
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.
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  1. [01][02][03]
  2. [04][05][06]
  3. [07][08][09]
  4. [10][11][12]
  5. [13][14][15]
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Jul 18th, 2007
0

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

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,


Click to Expand / Collapse  Quote originally posted by Hamrick ...
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.
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  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.
C++ Syntax (Toggle Plain Text)
  1. [01][02][03]
  2. [04][05][06]
  3. [07][08][09]
  4. [10][11][12]
  5. [13][14][15]
Reputation Points: 10
Solved Threads: 0
Newbie Poster
VISHOWNTAR is offline Offline
8 posts
since Jul 2007
Jul 18th, 2007
0

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

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?
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Jul 18th, 2007
0

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

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

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Maximum possible nodes in any link list
Next Thread in C++ Forum Timeline: Redundancy





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC