Javaturtle Javaturtle - 1 month ago 9
C Question

Writing a 2-dimensional array table in C. How would I go about doing this. About neatness (such as dividing lines) as well

I need to write a program that


  1. Defines a two-dimensional array named settings with appropriate type, dimensions, and sizes large enough to hold the following table:

    0 1 2 3 4
    10 11 12 13 14
    20 21 22 23 24

  2. Initialize the array with the values in the table

  3. Print the array to standard out in neat rows and columns.



I have been taught some Java in this area, having difficulty actually outputting the numbers. I believe the everything in the array is correct and I am trying to output using if statements. Any way will work, I need to learn how to output tables in word. Also in the example he has each number "boxed off" neatly. Is this possible in word or is only the example I gave above possible?

Here is my code that I have worked on thus far.

main() {

#define column 5
#define row 3
int i = 0;
int j = 0;

int table[row][column] =
{
{0, 1, 2, 3, 4},
{10, 11, 12, 13, 14},
{20, 21, 22, 23, 24}
};
if(i<3) {
if(j<5) {

return table[i][j];
j++;
}
i++;
}

return 0;
}

Joe Joe
Answer

I have tested this and it outputs the correct table

# include <stdio.h>

int main() 
{

    static const int column = 5;
    static const int row = 3;

    int table[row][column] =
    {
        {0, 1, 2, 3, 4},
        {10, 11, 12, 13, 14},
        {20, 21, 22, 23, 24}
    };

    for(int i = 0; i < row; ++i)
    {
         for(int j = 0; j < column; ++j)
         {
              printf("%d ", table[i][j]);
         }
         printf("\n");
    }

    getchar(); // this means you have to press enter to exit the console

    return 0;
}
Comments