Thread: trying to print a 2D array..HELPP

  1. #1
    Registered User
    Join Date
    Jan 2013
    Location
    San Jose, CA
    Posts
    53

    Exclamation trying to print a 2D array..HELPP

    Hey everyone,

    so I have a very simple program here, and somehow I can't get it to print out a two-dimensional array. Any ideas on what I might be doing wrong?

    here's my code:
    Code:
    #include <stdio.h>
    
    #define MAX_PEOPLE 100
    
    
    char display_table(int table[MAX_PEOPLE][MAX_PEOPLE], int n);
    
    int main (void)
    {
        int  n = 5;
        char names[MAX_PEOPLE][4] = {"Ami", "Bob", "Cal", "Dan", "Ion"};
    
        int  table[MAX_PEOPLE][MAX_PEOPLE] =
        {
            {0,   1,   1,   1,   0},
            {0,   0,   1,   0,   0},
            {0,   0,   0,   0,   0},
            {1,   0,   1,   0,   1},
            {0,   1,   1,   1,   0}
        };
    
    display_table(table[MAX_PEOPLE][MAX_PEOPLE], n);
    
        return 0;
    }
    
    
    char display_table(int table[MAX_PEOPLE][MAX_PEOPLE], int n)
    {
        int row;
        for (n=0; n<MAX_PEOPLE; n++)
        {
            for(row=0; row<MAX_PEOPLE; row++)
            printf("%d", table[n][row]);
        }
    }
    and here's how the printed table should look like when i run the program:
    Code:
          |Ami Bob Cal Dan Ion
       --- --- --- --- --- ---
       Ami| 0   1   1   1   0 
       Bob| 0   0   1   0   0
       Cal| 0   0   0   0   0
       Dan| 1   0   1   0   1
       Ion| 0   1   1   1   0
    Thanks everyone!!
    Last edited by kal123456; 02-01-2013 at 08:20 PM.

  2. #2
    Registered User
    Join Date
    Jan 2013
    Location
    UK
    Posts
    19
    If you use an & symbol, you'll pass the address of table in memory.

    Code:
    #include <stdio.h>
    Code:
    void display_table(int*, int);
    
    #define MAX_PEOPLE 100
    
    
    int main (void)
    {
        int  n = 5;
        char names[MAX_PEOPLE][4] = {"Ami", "Bob", "Cal", "Dan", "Ion"};
    
        int  table[MAX_PEOPLE][MAX_PEOPLE] =
        {
            {0,   1,   1,   1,   0},
            {0,   0,   1,   0,   0},
            {0,   0,   0,   0,   0},
            {1,   0,   1,   0,   1},
            {0,   1,   1,   1,   0}
        };
    
    display_table(&table, n);
    
        return 0;
    }
    
    
    void display_table(int *table, int n)
    {
        int row;
        for (n=0; n<MAX_PEOPLE; n++)
        {
            for(row=0; row<MAX_PEOPLE; row++)
            printf("%d", table[n][row]);
        }
    }
    
    Last edited by 0x47617279; 02-01-2013 at 08:53 PM.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Isn't this essentially the same problem you've been looking for help on in 2 previous threads?

    1. For starters, you say you want your output to include the data from the names array, for both rows and columns, but your function only receives the int array.

    2. Your function receives 'int n' which you toss away and use it as a loop counter. What is n and why do you hard code it to magic-number 5?

    3. 'row' being used as a column counter belies its use.

    4. You don't need to tell your function the number of rows the array parameter has. But it does need to know the other dimensions so it can scale the indices properly. So..
    Code:
    char  display_table( char name[][MAX_NAME_LEN], int table[][MAX_PEOPLE], int rows )
    edit: By that I mean parameter of the array itself - You DO need to tell your function how many rows an array has, thus the 3rd parameter of int rows.

    5. Based on the format of you output - in order to print the table-header, you'll need to pad the output with leading spaces equal to the longest name + ' ' + '|' + ' ' .... or MAX_NAME_LEN + 3,
    THEN print the names as strings, then pad the second line and print ' --- ' MAX_PEOPLE times.
    THEN you need to print the name string from the names array for the first row + ' | ' + the first row's columnar data from the table array + '\n'. And repeat the last one until
    Last edited by Tclausex; 02-01-2013 at 09:16 PM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have some real studying to do!

    Code:
    #include <stdio.h>
     
    #define MAX_PEOPLE 5
     
    void display_table(int table[MAX_PEOPLE][MAX_PEOPLE],char names[MAX_PEOPLE][4]);
     
    int main (void)
    {
        
        char names[MAX_PEOPLE][4] = {"Ami", "Bob", "Cal", "Dan", "Ion"};
     
        int  table[MAX_PEOPLE][MAX_PEOPLE] =
        {
            {0,   1,   1,   1,   0},
            {0,   0,   1,   0,   0},
            {0,   0,   0,   0,   0},
            {1,   0,   1,   0,   1},
            {0,   1,   1,   1,   0}
        };
     
        display_table(table, names);
     
        return 0;
    }
    
    void display_table(int table[MAX_PEOPLE][MAX_PEOPLE], char names[MAX_PEOPLE][4])
    {
        int n,row;
        
       printf("     | ");                  //header row's left margin
       for(n=0;n<MAX_PEOPLE;n++)
          printf("%6s",names[n]); 
    
       printf("\n  -----------------------------------");
       for (n=0; n<MAX_PEOPLE; n++)
       {
            printf("\n%5s|",names[n]);
            for(row=0; row<MAX_PEOPLE; row++)
            printf("  %4d", table[n][row]);
       }
       printf("\n\n");
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Helpp
    By Kinshara in forum C Programming
    Replies: 32
    Last Post: 10-30-2009, 02:56 AM
  2. Helpp Do Not Know Where To Start
    By sjcc in forum C Programming
    Replies: 6
    Last Post: 03-12-2009, 08:50 AM
  3. C++ helpp
    By nooneelse in forum C++ Programming
    Replies: 6
    Last Post: 09-13-2008, 05:55 PM
  4. helpp
    By slurpy in forum Game Programming
    Replies: 5
    Last Post: 04-04-2006, 12:40 AM
  5. helpp
    By Scarvenger in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2005, 06:57 PM

Tags for this Thread