# Matrix

• 11-29-2004
HarryMin
Matrix
Hi there guys,

I am trying to write a C++ program which takes in a float from a user, between 0 and 3 and stores it in a matrix. I then need to compare the totals of each row, then state which row is the highest. From this highest row, I then need to find the highest value. I have been playing about with this for a few hours now... This is what I have got working so far...

Code:

```#include <iostream.h> #include <iomanip.h> #include <conio.h> #include <stdlib.h> int main (void)     {     const int ROWS = 8; // 8 columns     const int COLUMNS = 8; // 8 rows     int row;     int col;     float table[ ROWS ][ COLUMNS ];     float row_count;     float col_count;     for ( row = 0; row < ROWS; ++row)         {           for ( col = 0; col < COLUMNS; ++col )               {               cout << endl << "Enter data for row " << (row + 1)                   << ", column " << (col + 1) << " : ";               cin >> table[ row ][ col ];               }           }     cout << endl << "                Original Matrix";     cout << endl;     for (row =0; row < ROWS; ++row )         {           for (col =0; col < COLUMNS; ++col)           {               cout << setw(10) << table[ row ][ col ];           }           cout << endl;       }       system("PAUSE");       for ( row=0; row < ROWS; ++row )           {           row_count = 0;           for ( col =0; col < COLUMNS; ++col)           {               row_count = row_count + table[ row ][ col ];           }             cout << endl << "The sum of row "  << (row + 1) << " is " << row_count;           }           cout << endl;       system("PAUSE");       for ( col = 0; col < COLUMNS; ++col )       {         col_count = 0;         for ( row =0; row < ROWS; ++row)         {             col_count = col_count + table[ row ][ col ];         }         cout << endl << "The sum of the column " << (col + 1) << " is "               << col_count;         }         system("PAUSE");         return ( 0 ); }```
Any suggestions?

HarryMin
• 11-29-2004
Hunter2
>>Any suggestions?
Assuming that it works, only a few housekeeping details :) Preferably, brackets should be indented in a consistent manner. Also, you're using the old headers that are no longer standard.

Neatness is everything :)

Code:

```#include <iostream> #include <iomanip> #include <conio.h> #include <cstdlib> using namespace std; int main (void) {   const int ROWS = 8; // 8 columns   const int COLUMNS = 8; // 8 rows   int row;   int col;   float table[ ROWS ][ COLUMNS ];   float row_count;   float col_count;   for ( row = 0; row < ROWS; ++row)   {       for ( col = 0; col < COLUMNS; ++col )       {         cout << endl << "Enter data for row " << (row + 1) << ", column " << (col + 1) << " : ";         cin >> table[ row ][ col ];       }   }   cout << endl << "                Original Matrix";   cout << endl;   for (row =0; row < ROWS; ++row )   {       for (col =0; col < COLUMNS; ++col)       {         cout << setw(10) << table[ row ][ col ];       }       cout << endl;   }   system("PAUSE");   for ( row=0; row < ROWS; ++row )   {       row_count = 0;       for ( col =0; col < COLUMNS; ++col)       {         row_count = row_count + table[ row ][ col ];       }       cout << endl << "The sum of row "  << (row + 1) << " is " << row_count;   }   cout << endl;   system("PAUSE");   for ( col = 0; col < COLUMNS; ++col )   {       col_count = 0;       for ( row =0; row < ROWS; ++row)       {         col_count = col_count + table[ row ][ col ];       }       cout << endl << "The sum of the column " << (col + 1) << " is "         << col_count;   }   system("PAUSE");   return ( 0 ); }```
Also, if the body of a for loop is only 1 line, then you don't need the {} around it. It's up to you to decide if it looks better with or without them though.
• 11-29-2004
HarryMin
Apologies - edited first part of reply...

Also any suggestion for limiting the numbers entered to be between 0 and 3??
• 11-29-2004
hk_mp5kpdw
Quote:

Originally Posted by HarryMin
Also any suggestion for limiting the numbers entered to be between 0 and 3??

Code:

```for ( row = 0; row < ROWS; ++row) {     for ( col = 0; col < COLUMNS; ++col )     {         do         {             cout << endl << "Enter data for row " << (row + 1) << ", column " << (col + 1) << " : ";             cin >> table[ row ][ col ];         } while( table[row][col] < 0 || table[row][col] > 3 );     } }```
• 11-29-2004
HarryMin
Cheers :)

Now comes the hard bit. I need to compare the totals of the of the 8 rows, then display the row with the highest total. Would the best way of doing this be to sort them into an array... and if so, any suggestions as how to do it? I'm not sure how to get the values out using a table... although something tells me this should be very simple?
• 11-29-2004
PJYelton
After your loop, you know what the rowcount is right? Create a variable that stores the max rowcount and another variable that represents the corresponding row. Then each time through the loop, check to see if the new rowcount is higher than the max and if so, update the two variables.
• 11-29-2004
HarryMin
Code:

`row_count = row_count + table[ row ][ col ];`
So where exactly should I put the max row count variable - I think its the table[ row ][ col ] that throws me...
• 11-29-2004
hk_mp5kpdw
Code:

```int MaxRowCount = 0; int MaxRowIndex = 0; ... for ( row=0; row < ROWS; ++row ) {     row_count = 0;     for ( col =0; col < COLUMNS; ++col)     {         row_count = row_count + table[ row ][ col ];     }     cout << endl << "The sum of row "  << (row + 1) << " is " << row_count;     if( row_count > MaxRowCount )     {         MaxRowCount = row_count;         MaxRowIndex = row;     } }```
After this, MaxRowIndex will contain the index of the row with the highest sum.

Changed wording of "highest value" to "highest sum" to make things more clear[/edit]
• 11-29-2004
HarryMin
So in order to get the highest value within a row I would do very similar thing... but I would need to use MaxRowIndex and then search through the matrix?
• 11-29-2004
HarryMin
i.e. I would use MaxRowIndex to find the row to search through, then do a very similar thing as to searching through the totals of the rows? If that makes sense :)
• 11-29-2004
Hunter2
Quote:

So in order to get the highest value within a row I would do very similar thing... but I would need to use MaxRowIndex and then search through the matrix?
Sounds about right. Only difference is, instead of finding the highest value of (sum of elements in row), you'd be finding the highest value in row - or to rephrase it, where earlier you were looking for the highest row value, now you're looking for the highest column value in a row. It's actually a bit easier, because you don't have to calculate the sum of a bunch of values.

Your second post sums it up better than I could have ;)