Formatting two dimensional arrarys

This is a discussion on Formatting two dimensional arrarys within the C++ Programming forums, part of the General Programming Boards category; Hey guys i'm having a problem with my formatting of 2d array output. I'm building this for a .pgm image--say ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    20

    Formatting two dimensional arrarys

    Hey guys i'm having a problem with my formatting of 2d array output. I'm building this for a .pgm image--say the code is

    Code:
    for(int row = 0; row < numrows; row++)
         for(int col = 0; col < numcols; col++)
              bwarray[row][col] = 255;
    
    for(int r2 = 0; r2 < numrows; r2++)
         for(int c2 = 0; c2 < numcols; c2++)
              cout << bwarray[r2][c2] << ' ';
    where numrows is 3 and numcols is 5..so the ouput would be

    255 255 255 255 255 255 255 255 255 255 255 255 255 255 255

    I need the output to be

    255 255 255 255 255
    255 255 255 255 255
    255 255 255 255 255

    I've tried working with modulus with no luck (i've never been good with loops)..anyone have any ideas? Any help is appreciated.

  2. #2
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    Code:
    for(int r2 = 0; r2 < numrows; r2++)
         for(int c2 = 0; c2 < numcols; c2++)
              cout << bwarray[r2][c2] << ' ';
    Add a newline char to the end of any row iteration. Implement this loop like this:
    Code:
    for (int r2 = 0; r2 < numrows; r2++) {
            for (int c2 = 0; c2 < numcols; c2++) {
                    cout << bwarray[r2][c2] << ' ';
            }
            cout << "\n";
    }
    I guess this ought to do it.

  3. #3
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    You should use curley braces '{' indeed.
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi dimensional array
    By $l4xklynx in forum C Programming
    Replies: 7
    Last Post: 01-03-2009, 03:56 AM
  2. Two dimensional array
    By George2 in forum C Programming
    Replies: 3
    Last Post: 11-10-2007, 05:27 AM
  3. two dimensional string array question
    By Hoser83 in forum C Programming
    Replies: 8
    Last Post: 02-07-2006, 08:15 PM
  4. dos game help
    By kwm32 in forum Game Programming
    Replies: 7
    Last Post: 03-28-2004, 06:28 PM
  5. Have you ever had a four dimensional experience
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 35
    Last Post: 08-18-2003, 07:13 PM

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