Can I print elements of a simple matrix with out manual nested loop?

Discussion in 'C++' started by SUPER_SOCKO, Feb 28, 2005.

  1. SUPER_SOCKO

    SUPER_SOCKO Guest

    Suppose I have a matrix M x N dimension.
    Normally, to print all of the elements in the matrix. I write the
    following codes:

    Suppose M = 5, N = 4:

    for(int j=0; j<5; ++j) {
    for(int k=0; k<4; ++k) {
    cout << matrix[M][N] << " ";
    }
    cout << endl;
    }

    If my matrix class has method for return number of dimension and
    dimension size such as:

    numDimension(matrix); // return 2 becase the matrix has M x N = 2
    dimensions.

    sizeDimension(0); // return 5 -- size of M
    sizeDimension(1); // return 4 -- size of N

    How to write some code that print all element of the matrix with out
    code nested loops by hand?

    Thanks

    Nalerk
    SUPER_SOCKO, Feb 28, 2005
    #1
    1. Advertising

  2. SUPER_SOCKO

    Ben Hetland Guest

    Re: Can I print elements of a simple matrix with out manual nestedloop?

    SUPER_SOCKO wrote:

    > How to write some code that print all element of the matrix with out
    > code nested loops by hand?


    You can just create a double loop, where the outermost loop iterates of
    the number of dimensions, while the inner loop goes from 0 to
    "sizeDimension(current_dim)".

    However, you will need to decide yourself how to lay out a -- say --
    5-dimensional matrix in the 2-dimensional "cout space".

    You will also need to calculate your indices yourself somehow, but your
    already probably have your "matrix" class to help you out here, don't
    you? (Or how does it otherwise implement matrix[n][m] in a 2-dim.
    matrix, and say matrix[n][m][p][q][r] in a 5-dimensional one?) Along the
    way, you will need to maintain som state variables, to keep track of
    (and temporarily remember) indices you are starting at in each
    direction. Some stack- or list-like functionality probably; hint:
    std::vector.

    Another way to solve this latter is considering some kind of recursive
    implementation.


    Regards,

    -+-Ben-+-
    Ben Hetland, Feb 28, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lvcargnini

    Matrix composed by two matrix

    lvcargnini, Jul 4, 2006, in forum: VHDL
    Replies:
    3
    Views:
    2,648
    Jonathan Bromley
    Jul 5, 2006
  2. Holgerson

    Matrix*Vector and Vector*Matrix

    Holgerson, Oct 25, 2007, in forum: C++
    Replies:
    3
    Views:
    398
    Holgerson
    Oct 26, 2007
  3. Terry Reedy
    Replies:
    0
    Views:
    542
    Terry Reedy
    Apr 2, 2009
  4. Robert Kern
    Replies:
    0
    Views:
    581
    Robert Kern
    Apr 2, 2009
  5. Isaac Won
    Replies:
    9
    Views:
    350
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page