Printing a 2D array

This is a discussion on Printing a 2D array within the C++ Programming forums, part of the General Programming Boards category; I want to display a 2D array, but my output is: 22 34 5 648 798 2477 0 0 0 ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Kansas City
    Posts
    76

    Printing a 2D array

    I want to display a 2D array, but my output is:
    22 34 5 648 798 2477 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

    What is wrong with my code since only the last column is displayed ?
    Code:
        int num_array[6][5] = 
        { 
            ( 82,3,36,8,22 ) ,
            ( 6,2,88,9,34 ) ,
            ( 65,26,42,1,5 ) ,
            ( 46,1521,51,7,648 ) ,
            ( 14,39,5,792,798 ) ,
            ( 47,11,348,547,2477 ) 
        };
        
        int nNumRows = 6;
        int nNumCols = 5;
        
    for (int nRow = 0; nRow < nNumRows; nRow++)
        for (int nCol = 0; nCol < nNumCols; nCol++)
            cout << num_array[nRow][nCol] << " ";

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    You've used parentheses and not braces in your array initialiser.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Why did it compile? In using parentheses, you've inadvertently used the comma operator.
    Code:
    ( 82,3,36,8,22 )
    That is an expression. The comma operator evaluates its left expression, then its right, and the value of the expression has the value of the right-hand side. When you combine multiple commas, you get something like
    Code:
    ( (((82,3),36),8),22 )
    which evaluates to 22 in the end.

    The comma operator is one of the least-used operators. Perhaps the most useful place for it is in for loops:
    Code:
    for(x = 0, y = 0; x < MAX; x ++, y ++)
    In array initializers, any elements not specified are set to zero. That's why the remaining elements in your array became zero.

    One other bit of knowledge is required to understand what happened: you can leave out nested curly braces. For example, this
    Code:
    int array[2][2] = {{1, 2}, {3, 4}};
    can be written as
    Code:
    int array[2][2] = {1, 2, 3, 4};
    Many people consider this bad programming practice, and your compiler will probably warn you about it if you turn up the warning level. But it's part of C, at least C89.

    Anyway, just some useless information. As Salem said, you want to use
    Code:
    {}
    instead of ().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Treating a 2D array as a 1D array....
    By John_L in forum C Programming
    Replies: 6
    Last Post: 10-18-2007, 03:38 PM
  2. malloced 2d array passed to qsort
    By jamie85 in forum C Programming
    Replies: 7
    Last Post: 11-25-2005, 01:55 PM
  3. Dynamic 2d array
    By Mithoric in forum C++ Programming
    Replies: 8
    Last Post: 12-29-2003, 09:19 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM
  5. how to pass 2D array into function..?
    By IngramGc in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 09:41 AM

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