| | |
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:
Solved Threads: 0
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.
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)
#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; }
Last edited by Ancient Dragon; Jul 17th, 2007 at 11:40 pm. Reason: add code tags
Compare
for say averaging each row...
Vs.
C++ Syntax (Toggle Plain Text)
for ( row = 0 ; row < 3 ; row++ ) for ( col = 0 ; col < 5; col++ )
Vs.
C++ Syntax (Toggle Plain Text)
for ( col = 0 ; col < 5; col++ ) for ( row = 0 ; row < 3 ; row++ )
•
•
Join Date: Jul 2007
Posts: 8
Reputation:
Solved Threads: 0
•
•
•
•
Compare
for say averaging each row...C++ Syntax (Toggle Plain Text)
for ( row = 0 ; row < 3 ; row++ ) for ( col = 0 ; col < 5; col++ )
Vs.
C++ Syntax (Toggle Plain Text)
for ( col = 0 ; col < 5; col++ ) for ( row = 0 ; row < 3 ; row++ )
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.
•
•
•
•
Will you please give me little detail hint?
C++ Syntax (Toggle Plain Text)
for ( int row = 0; row < nRows; ++row ) { for ( int col = 0; col < nCols; ++col ) { // do something with array[row][col] } }
C++ Syntax (Toggle Plain Text)
[01][02][03][04][05] [06][07][08][09][10] [11][12][13][14][15]
C++ Syntax (Toggle Plain Text)
[01][04][07][10][13] [02][05][08][11][14] [03][06][09][12][15]
C++ Syntax (Toggle Plain Text)
for ( int col = 0; col < nCols; ++col ) { for ( int row = 0; row < nRows; ++row ) { // do something with array[row][col] } }

C++ Syntax (Toggle Plain Text)
[01][02][03] [04][05][06] [07][08][09] [10][11][12] [13][14][15]
The truth does not change according to our ability to stomach it.
•
•
Join Date: Jul 2007
Posts: 8
Reputation:
Solved Threads: 0
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,
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.
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)
for ( int row = 0; row < nRows; ++row ) { for ( int col = 0; col < nCols; ++col ) { // do something with array[row][col] } }
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)
[01][02][03][04][05] [06][07][08][09][10] [11][12][13][14][15]
You do that with code like this.C++ Syntax (Toggle Plain Text)
[01][04][07][10][13] [02][05][08][11][14] [03][06][09][12][15]
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)
for ( int col = 0; col < nCols; ++col ) { for ( int row = 0; row < nRows; ++row ) { // do something with array[row][col] } }
C++ Syntax (Toggle Plain Text)
[01][02][03] [04][05][06] [07][08][09] [10][11][12] [13][14][15]
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)
#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; }
The truth does not change according to our ability to stomach it.
![]() |
Similar Threads
- find duplicates from a spreadsheet file (Computer Science)
- Output Windows Explorer column details to a file (Windows NT / 2000 / XP)
- query to find the 10th maximum salary (MS SQL)
- substitue of comparison operators (C++)
- Multimap confusion; how does it work? (C)
- plz help (Visual Basic 4 / 5 / 6)
- array function operation (C++)
- computer slow, especially on startup (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: Maximum possible nodes in any link list
- Next Thread: Redundancy
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






