Galya
Galya

Reputation: 11

2D array and how to print the array in C

I am trying to print a 2D array that should look like this

*************
            *
            *
*************

like a frame, but I have hard time because it is not printing the stars instead it prints numbers and even those columns I specified with 42 ('*') are not showing. I don't know if the problem is the way I am printing the array. I hope someone can help me. Thank you! here's my code: int i,j;

  int star=42;
  char starchar=(char)star;
  //colMax=10; 
  int frame[rowMax][colMax];
     for(row=0;row<rowMax;row++)
        {
           for(column=0;column<colMax;column++)
                {
                        frame[0][column]=starchar;
                        frame[9][column]=starchar;
                        frame[row][9]=starchar;


                        printf("%d%d",row,column);
                }

Upvotes: 0

Views: 596

Answers (2)

chux
chux

Reputation: 153498

printf("%d%d",row,column); prints the row and column, not the value in frame[row][column].

#include <string.h>
#include <stdio.h>
int main(void) {
  // Not clear if rowMax is the maximum index or the width. Assume maximum index.
  size_t rowMax = 10;
  size_t colMax = 5;
  int star = '*';

  // Much easier to use char array
  char frame[rowMax + 1][colMax + 1];  // +1 the dimensions

  // fill
  memset(&frame[0], star, colMax + 1);  // fill first row
  for (size_t row = 1; row < rowMax; row++) {
    memset(&frame[row], ' ', colMax);  // fill with spaces
    frame[row][colMax] = star;
  }
  memset(&frame[rowMax], star, colMax + 1);  // fill last row

  // print
  for (size_t row = 0; row <= rowMax; row++) {
    for (size_t col = 0; col <= colMax; col++) {
      fputc(frame[row][col], stdout);
    }
    fputc('\n', stdout);
  }

  return 0;
}

Upvotes: 0

Dronz
Dronz

Reputation: 2097

It is printing numbers because you are passing %d%d to the printf function, which stands for decimal number. Replace %d with %c to get the character representation. See the manual for printf, e.g. at http://www.cplusplus.com/reference/cstdio/printf/

Also, you have another problem which is you are passing values other than the ones you intend into printf. You are passing row and column, which are not the values at that location, but the numbers of the index. You want something like:

printf("%c",frame[row][column]);

As Paul Roub points out in comments, you are also doing some odd things with the way you are putting values into the array during your display loop, which is probably also going to confuse you if you develop this code further. First, you don't ever specify what is in the non-star parts of the array. Those should be set to something like the space character before, or at the same time, as adding stars. As written, the values will default to zero, which is unprintable null in ASCII, not space (which is 32).

Also, I assume you mean to be setting up the values first, and then displaying them. The way your loop works, it is setting the values as it draws them, in an interesting way that I would assume is not what you intend. Your example will currently end up showing what you expect, but usually I would expect you to fill in the values first and then display them later.

Upvotes: 1

Related Questions