Javaturtle
Javaturtle

Reputation: 329

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;
}

Upvotes: 1

Views: 25317

Answers (2)

AdminXVII
AdminXVII

Reputation: 1329

Try this, it output a table with headers and more or less boxed:

# include <stdio.h>

int main(){

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

  printf("    | 1  | 2  | 3  | 4  | 5\n----------------------------\n");

  for(int i = 0; i < 3; ++i)
  {
     printf("%d   |",i);
     for(int j = 0; j < 5; ++j)
     {
        if(table[i][j] > 9)
        {
          printf(" %d |", table[i][j]);
        } else {
          printf(" %d  |", table[i][j]);
        }
     }
     printf("\n----------------------------\n");
  }

  getchar(); // good idea from Joe

  return 0;
}

Upvotes: 0

Joe
Joe

Reputation: 153

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;
}

Upvotes: 3

Related Questions