Printing a Matrix?

This is a discussion on Printing a Matrix? within the C++ Programming forums, part of the General Programming Boards category; Hello, again everyone. I was just wondering if it is possible to print an entire matrix, sorry if this is ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    41

    Printing a Matrix?

    Hello, again everyone.

    I was just wondering if it is possible to print an entire matrix, sorry if this is just a simple thing that I've missed, but anyway



    Thank you in advance

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    By the power vested in you by the invention of the for loop, yes.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    41
    I'm guessing you meant something like:
    Code:
    for (busy==1)
         {
             int matrix[5][5]={{1,2,3,4,5},{1,2,3,4,5}}
             cout<<matrix[1][0]<<"\n"<<matrix[0][1];
             /*and so on */ 
          }
    Is their and easier way to do it?

    And why I'm here, is it possible to print the names of every instance of 1 class, and allow the user to call a function from 1 of these classes?

    Thank you for your help.

    And sorry for my stupidity.

  4. #4
    *this
    Join Date
    Mar 2005
    Posts
    498
    Code:
    for (int row = 0; row < 5; row++)
       for (int col = 0; col < 5; col++)
          cout << matrix[row][col];

  5. #5
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Use a for loop and a variable or two to loop through it and make it easier to read.

    Code:
      int matrix[2][5]={{1,2,3,4,5},{1,2,3,4,5}};
      int i = 0, j = 0;
      
      for(i = 0; i < 2; ++i) {
        for(j = 0; j < 5; ++j) {
          cout << matrix[i][j];
        }
      }
    translates to:

    Code:
      cout << matrix[0][0];
    cout << matrix[0][1];
    cout << matrix[0][2];
    cout << matrix[0][3];
    cout << matrix[0][4];
    cout << matrix[0][5];
    cout << matrix[1][0];
    cout << matrix[1][1];
    cout << matrix[1][2];
    cout << matrix[1][3];
    cout << matrix[1][4];
    cout << matrix[1][5];
    The first for loop wont finish until the for loop inside it finishes. Hence why 1-5 of j is printed before i goes from 0 to 1.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

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. What is a matrix's purpose in OpenGL
    By jimboob in forum Game Programming
    Replies: 5
    Last Post: 11-13-2004, 11:19 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