Matrix

This is a discussion on Matrix within the C++ Programming forums, part of the General Programming Boards category; Hi there guys, I am trying to write a C++ program which takes in a float from a user, between ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    6

    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

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>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.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    6
    Apologies - edited first part of reply...

    Also any suggestion for limiting the numbers entered to be between 0 and 3??

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    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 );
        }
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    6
    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?

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    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.

  7. #7
    Registered User
    Join Date
    Nov 2004
    Posts
    6
    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...

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    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.

    [edit]Changed wording of "highest value" to "highest sum" to make things more clear[/edit]
    Last edited by hk_mp5kpdw; 11-29-2004 at 02:27 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  9. #9
    Registered User
    Join Date
    Nov 2004
    Posts
    6
    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?

  10. #10
    Registered User
    Join Date
    Nov 2004
    Posts
    6
    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. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    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
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 04:57 PM
  3. Gauss-Jordan Matrix Inversion in C++
    By Max_Power82 in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2006, 07:31 PM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21