Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a character at a certain position in a 2d array?

Tags:

c

I want to be clear, yes this is a homework related question, and no I don't expect you to do my homework for me. I love coding and I'm just looking for advice.

Having said that, I'm a creating the Game of Life in C. Here is some code to show I've been working on this:

void create_area(void){
int table[24][80];
int i,j;
int xvalue=10,yvalue=10;
table[10][10] = 2;


/*for(j=0; j<24; j++){
    for(i=0; i<80; i++){
        table[j][i] = 1;
    }
}*/

for(j=0; j<24; j++){
    for(i=0; i<80; i++){
        printf(" ",table[j][i]);
    }
    printf("\n");
}

/*if(xvalue=10){
    if(yvalue=10){
        printf("O", table[xvalue][yvalue]);
    }
}*/

/*for(j=0; j<24; j++){
    for(i=0; i<80; i++){
        if(xvalue=0){
            if(yvalue=0){
                printf("O",table[xvalue][yvalue]);
            }
        }
    }
}*/
}

My main method isn't shown here, but it takes care of finding the number of arguments and the argument strings and putting them into coordinates with two arrays that I make (an x_array and y_array). The method show above is basically create the grid in the terminal. The parameters for create_area is void for now (because I just want to test values to see if it works, which is why I have two int variables: xvalue and yvalue). But the idea is that create_area will take a x value and y value and initialize the character 'O' where the cell would begin. A lot of the code is commented out because I'm trying to figure stuff out.

My question: How do I insert a character within my 2d array so that it displays that character in a spot on the grid when looking at it in the terminal?

So far I created my 2d array, and used two for loops to go through it and print spaces. But I'm trying to figure out how to print a char for example at table[10][10]. I tried a few things, but the character won't display at the certain location, only at the bottom left of my screen. I think I'm missing something simple.

Any help would be appreciated.

like image 646
DsDude Avatar asked Jan 14 '17 19:01

DsDude


Video Answer


4 Answers

If you want to represent a grid of characters, then you should use data type char instead of int and character constants (e.g. '1') instead of integer constants (e.g. 1). The main issue, however, is that statement printf(" ",table[j][i]); always prints a space as you missed the format specifier in the format string (e.g. "%d" for integers, or "%c" for character data type). I do not know how your grid shall look like exactly, but maybe the following fragment helps:

void create_area(void){
  char table[24][80];
  int i,j;
  int xvalue=10,yvalue=10;

  for(j=0; j<24; j++){
    for(i=0; i<80; i++){
        table[j][i] = '1';
    }
  }

  table[10][10] = '0';

  for(j=0; j<24; j++){
    for(i=0; i<80; i++){
        printf("%c",table[j][i]);
    }
    printf("\n");
  }
  ...
like image 91
Stephan Lechner Avatar answered Oct 12 '22 03:10

Stephan Lechner


I agree with the other answers here, but let me provide another perspective.

You should separate data from presentation. Here the array is your data (it could also be bool table[24][80], because in the Game of Life each cell is described by 1 bit). The code that displays your data should examine each data element (see whether it's 0 or 1) and output 1 char. For that, you can use putchar:

putchar(table[j][i] ? 'x' : ' ')

or printf:

printf("%c", table[j][i] ? 'x' : ' ')

It's good to separate data from presentation because this makes it easier for you to change (improve) each of them separately. For example, if you decide to replace x by * in your visualization, the corresponding code is in one place. If you decide to optimize your storage by allocating 1 byte per cell (and not 1 int per cell) - the change is also relatively localized.

like image 36
anatolyg Avatar answered Oct 12 '22 04:10

anatolyg


For option. You actually can fill your table with spaces, modify it and then print

char table[24][80]; // char is better

memset(table, ' ', sizeof(table[0][0]) * 24 * 80); // Fill table with spaces

table[10][10] = 'J'; // Your first custom char
table[20][20] = '0'; // Another custom char

for(j=0; j<24; ++j) {
    for(i=0; i<80; ++i) {
        printf("%c",table[j][i]); // You forget write "%c" to print char
        //putchar(table[j][i]) // This will also work
    }
    printf("\n");
}
like image 2
Inline Avatar answered Oct 12 '22 04:10

Inline


See this description for printf. In C the character is just a some integer number, so you should specify a format to output it like a character %c or like an integer (decimal) %d.

   for(j=0; j<24; j++) {

        for(i=0; i<80; i++){

            if (table[j][i] == 1) { // it's a code of some character that fills your array (As I saw in commented block you used 1)
                printf(" ");
            } else {
                printf("%c",table[j][i]);
            }
        }

        printf("\n");
    }

P.S. For profi, I know it's not an optimal code but it will be easy for him to understand.

like image 1
Шах Avatar answered Oct 12 '22 03:10

Шах