Print matrice with one loop?

This is a discussion on Print matrice with one loop? within the C Programming forums, part of the General Programming Boards category; How can I print matrix with one loop . . . Code: void printMat(const int mat[][N], int n) { int ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    15

    Print matrice with one loop?

    How can I print matrix with one loop . . .


    Code:
    void printMat(const int mat[][N], int n)		
    {
    	int i, j;
    	for(i = 0; i < n; i++)
    	{
    		for(j = 0; j < N; j++)
    		printf("%3d ", mat[i][j]);
    		puts("");
    	}
    }
    Thanx

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,820
    Why do you want to do that? It would obfuscate the clear logic, in my opinion.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Just use common sense:

    In a 3 x 1 matrix, how many elements are there? OK, how about a 3 x 2? a 3 x 3?

    Seeing the pattern yet?

    instead of
    Code:
    for(i = 0; i < rowNum; i++)
      for(j = 0; j < colNum; j++)
         printf("%3d", matrix[i][j]);
    
    //You would use:
    
    element = rowNum x colNum;
    
    for(i = 0; i < (well, you know the rest, ...

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for( x = 0; x < rows; x++ )
        printrow( matrix[ x ] );
    There you go.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-01-2008, 09:09 AM
  2. Mutiliplcation Program that uses a (do, while, and for loop)?
    By Debbie Bremer in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2008, 06:04 PM
  3. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 02:48 AM
  4. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 04:39 AM
  5. Print from 1 to 10 without using a loop
    By Guti14 in forum C++ Programming
    Replies: 11
    Last Post: 12-23-2003, 09:02 PM

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