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

I want to be clear, yes, this is a homework question, and I do not expect you to do my homework for me. I like coding and I'm just looking for advice.

Having said that, I create a Game of Life in C. Here is some code to show that I was working on it:

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 is not shown here, but it takes care of finding the number of arguments and argument strings and puts them in the coordinates with the two arrays that I create (x_array and y_array). The above method basically creates a grid in the terminal. The options for create_area are not valid yet (because I just want to check the values ​​to see if this works, so I have two int variables: xvalue and yvalue). But the idea is that create_area will take the value x and the value y and initialize the character "O" where the cell starts. Most of the code is commented out because I'm trying to figure out what is going on.

My question is . How to insert a character into my 2d array so that it displays that character in a place on the grid when viewing it in the terminal?

So far I have created my 2d array and used two loops to go through it and print spaces. But I'm trying to figure out how to print char, for example, in table [10] [10]. I tried several things, but the character will not be displayed in a specific place, only in the lower left corner of the screen. I think I missed something simple.

Any help would be appreciated.

+6
source share
5 answers

If you want to represent a character grid, you should use char data type instead of int and character constants (like '1' ) instead of integer constants (like 1 ). The main problem, however, is that the printf(" ",table[j][i]); statement printf(" ",table[j][i]); always prints a space when you miss a format specifier in a format string (for example, "%d" for integers or "%c" for a character data type). I don't know how your grid will look exactly, but maybe the following snippet 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"); } ... 
+3
source

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

You should separate the data from the presentation. Here the array is your data (it can also be a bool table[24][80] , because in the Game of Life each cell is described 1 bit). The code displaying your data should check each data item (see if it is 0 or 1) and output 1 char . You can use putchar :

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

or printf :

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

It’s good to separate the data from the presentation, because it makes it easier for you to change (improve) each of them separately. For example, if you decide to replace x with * 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) - this change is also relatively localized.

+4
source

For the parameter. In fact, you can fill the table spaces, change 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"); } 
+2
source

See this description of printf . In C, a char actor is just an integer, so you must specify the format to output it as the %c character or as the (decimal) %d integer.

  for(j=0; j<24; j++) { for(i=0; i<80; i++){ if (table[j][i] == 1) { // it 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"); } 

PS For profi, I know that this is not the best code, but it will be easy for him to understand.

+1
source

You can use the %hhu scan code to print an unsigned char . This means that there is no need to change the int table data type.

 printf("%hhu", table[j][i]); % table[j][i] contains integers 

Hint: the range is unsigned char 0 - 255 , and numbers that do not fit into this range are not displayed, as you might expect.

+1
source

Source: https://habr.com/ru/post/1014089/


All Articles