Thread: Help with multidimensional array

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    3

    Help with multidimensional array

    I'm trying to make a multiplication table using a multidimensional array and I keep having problems with the first row and 0's.

    I need to print a table that looks like this

    * 1 2 3
    1 1 2 3
    2 2 4 6
    3 3 6 9

    I tried using (row * column) but I can't figure out how to get rid of the 0's which are throwing everything off. If I start the loops at =1 it does something crazy and throws in random numbers. I also have NO idea how to get the * symbol in the beginning.

    This is what I have right now any help would be greatly appreciated. I need to understand what I'm doing wrong not just given a correction to a part of the code. Thanks again in advance.

    Code:
    #include <stdio.h>
    
    const int num_rows = 10;
    const int num_columns = 10;
    
    int main (void)
    {
        int box[num_rows][num_columns];
        int row, column;
    
        for (row = 0; row < num_rows; row++)
            for (column = 0; column < num_columns; column++)
                box[row][column] = row * column;
    
        for (row = 0; row < num_rows; row++)
            {
                for(column = 0; column < num_columns; column++)
                {
                    printf("%4d", box[row][column]);
                }
            printf("\n");
            }
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    First, * 1 2 3 ... are really numbers, you have to print this line as characters, OK? When you are printing out your rows, that first entry is not a number, but again, a character. Think of it as printing your results inside the fence line of

    * 1 2 3 4 5 6 ...
    1
    2
    3
    4
    5
    .
    .
    .
    That will probably help you continue to solve your problem. You're not really doing anything wrong, you just had the wrong idea how to print your results.

  3. #3
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    This gives the desired output format, but does not use a 2d array. I assume this is for an assignment and as per the homework policy on this board I removed the 2d array. But you can use this as a template to see how to output formatted tables.

    Code:
    #include <stdio.h>
    
    const int num_rows = 10;
    const int num_columns = 10;
    
    int main (void)
    {
        int row, column;
    
        // print column header
        printf("%-4c", '*');
        for (column = 1; column < num_columns; column++)
            printf("%-4d", column);
        putchar('\n');
        
        for (row = 1; row < num_rows; row++)
        {
            printf("%-4d", row);
            for(column = 1; column < num_columns; column++)
            {
                printf("%-4d", row * column);
            }
            printf("\n");
        }
        getchar();
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By bennywhere in forum C Programming
    Replies: 16
    Last Post: 10-20-2009, 09:00 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. Pointer to multidimensional array
    By ejohns85 in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2009, 11:17 AM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM