Thread: how to print the content of two dimension array on a grid

  1. #1
    Registered User
    Join Date
    Nov 2006

    how to print the content of two dimension array on a grid

    Hi everyone, i have a two-dimension array and i want to display its value on the grid like the following pattern :

    1 2 3 4 5 6 7 8 9 0
    a| | | | | | | |A| A|A|
    b | | | | | B| | | | | |
    c | | | | | B| |C| | |F|
    d | | | | | B| |C| | |F|
    e | | | | | | |C| | |F|

    Suppose i have a two-dimension array called a and i want to display its value on the grid.

    Please do not care about the line from 1 to 0 (it represents for the column of the array) and from a to e (it represents for the row of the array).For example, the value of cell with the coordinate a8 is the same with value of the element [0][7] of the array.The array will be initialized random by the user.The user just enter the value for each element and it will display on the grid.Please give me some glue to do that.Thanks in advanced

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    printf() using the %<field width><data type> modifiers.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Start with two nested for loops.

    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:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Sep 2006
    If you look at different ascii char's, you may find one's you like a lot better. Anyway:

    There's a zillion ways to do this, Kennedy's suggestion of using the width modifier in printf(), is VERY good. Just "take control" of where the cursor is on the page. You want it to print something there - you tell it. You want it to move to the next column, you tell it to print a space char (ascii 32).

    If you post up your code, we can help a lot more.

    for a 2D array, I usually use something like this, in pseudocode:
    print the Grid header
    print a string of '=' to separate it from the grid printout below
    for(row = 1 row < MaxRow r++) {
       for(col = 1 col < MaxCol col++) {
           print left margin of spaces (char 32)
           print left column of IDentifiers
           print a space
           print a row of top markers for your first row
           print newline
           print left side vertical sqr separator
           print a row of array contents, between each
           subscript value, print a sqr separator (maybe a vertical line)
           print a newline '\n'
       } /* end of for col... */    
    } /* end of for row... */
    That should get you going.

    Note: When I'm printing a grid, I frequently want it to start with the 1 row, not the zero row. You do it as seems best to you.

    Last edited by Adak; 11-07-2006 at 03:40 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. constantly print an array to the screen?
    By .exe in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2008, 10:41 PM
  2. Replies: 1
    Last Post: 04-25-2006, 12:14 AM
  3. 2 dimension array of characters
    By braddy in forum C Programming
    Replies: 9
    Last Post: 03-15-2006, 12:49 PM
  4. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM