Thread: tables?

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    77

    Question tables?

    is there anyway i can print arrays in a form of a table?

    if so how can i do it?
    Hooked On Phonics Didn't Work For Me!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Print each row of the array and then print a newline.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    77
    i know how to print out arrays in two columns but how can i make it so there is an exact number of spaces between each column
    Hooked On Phonics Didn't Work For Me!

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    get a string for the line. figure out how many spaces each number will take up, insert as many space characters as needed. output string.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    harryp
    Guest
    Code:
    int table[5][5] = 
    { 1, 2, 3, 4, 5,
       6, 7, 8, 9, 10,
       11, 12, 13, 14, 15,
       16, 17, 18, 19, 20,
       21, 22, 23, 24, 25 };
    
    for (int i = 0; i<5; i++)
    {
         for (int j = 0; j<5; j++)
         {
              if (j == 0) // a beginning '|'
              cout << ' table[j][i]  << "| ";
         cout << "\n";
    }
    That would get the following output:
    Code:
    | 1 | 2 | 3 | 4 | 5 |
    | 6 | 7 | 8 | 9 | 10 |
    | 11 | 12 | 13 | 14 | 15 |
    | 16 | 17 | 18 | 19 | 20 |
    | 21 | 22 | 23 | 24 | 25 |
    That looks a tad unprofessional. You could improve it by checking to see if 'j' is a single digit, and if it is, print out an extra n number of spaces to even up all the lines. Hope that helps!

    Brendan

  6. #6
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    You can experiement with using
    printf("%4d", i) right justifiy by 4 and
    printf("%-4d", i) left justify by 4.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's why you use width specifiers. I believe it's something simple like:

    cout << width( 10 ) << 120;

    Although, I rarely use C++ so I don't recall off the top of my head.

    I'm on a call (imagine that) or I'd look it up.

    [edit]
    Actally, it's 'setw( SIZE )'.
    [/edit]

    Quzah.
    Last edited by quzah; 09-04-2002 at 09:29 PM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ style tables
    By MarkZWEERS in forum C++ Programming
    Replies: 4
    Last Post: 05-18-2009, 08:41 AM
  2. Iterable hash tables
    By OnionKnight in forum Tech Board
    Replies: 5
    Last Post: 07-21-2008, 02:02 AM
  3. converting struct to tables in turbo c++
    By geft in forum C++ Programming
    Replies: 4
    Last Post: 02-28-2008, 08:44 AM
  4. String tables in console apps
    By Sentral in forum C++ Programming
    Replies: 3
    Last Post: 10-16-2006, 04:08 AM
  5. Help with beginning hash tables, please.
    By Will in forum C++ Programming
    Replies: 8
    Last Post: 11-17-2002, 09:51 PM