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.
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");
}
...
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.
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");
}
See this description for printf. In C the char
acter 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With