Question

I am new to 'C' and I am trying to make this for a class project. I have been trying to print out a 2d array, but when I get to my print function, it only prints out a new line character. I apologize if the code is hideous, I am still trying to get all of the tendencies of 'C'. Right now i am only trying to figure out how to print the array, i am not worried about the other function yet (so don't worry about evalboard or evaluate_cell.) Many Thanks!

signed char board [10][10] = {5};
int evaltheboard(void){
    int i,a;
    signed char cell_to_check [1][1] = {0};
    for (a=0;a<=9;a++){
        for (i=0;i<=9;i++){
            cell_to_check[0][0] = board[i][a];
            if (cell_to_check[0][0] != 0){
                evaluate_cell(i,a);
            }
        }
    }
    return 0;
}

int evaluate_cell(int i,int a){
    signed char live_n_cells = 0, empty_x_mod, empty_y_mod;
    signed char x_inc [8] = {0,1,1,1,0,-1,-1,-1}, y_inc [8]={-1,-1,0,1,1,1,0,-1};
    int z, x, y;
    z=x=y=0;

    for (;z<=7;z++){
        if (board[(i+(x_inc[x]))][(a+(y_inc[y]))] != 0){
        ++live_n_cells;
        ++x;
        ++y;
    }
        else(board[(i+(x_inc[x]))][(a+(y_inc[y]))] == 0);
            empty_x_mod = i-(x_inc[x]) ;
            empty_y_mod = a-(y_inc[y]);
            ++x;
        ++y;
    }

    if (live_n_cells >= 3){
        board[i][a] = 0;
    }
    else if (live_n_cells = 0){
    board[i][a] = 0;
    }
    else(live_n_cells = 1 || 2);
        board[empty_x_mod][empty_y_mod] = 1;
    return 0;

}

int print_array(void){\
    int i,a;
    for (a=0;a<=9;a++){
        for (i=0;i<=9;i++){
            printf("%c", board[i][a]);
        }
        printf("%c", board[0][0]);
        printf("\n");
    }
    return 0;
}

int main(void){

    print_array();
    return 0;

}
Was it helpful?

Solution

You are assigning non-printable character values to the board, but you are using printf in with the character code %c. If you would print the decimal value using %d, then you would get the 5's that you initialized the array to.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top