Integers are being shown as ascii?

I'm trying to create a 2d grid using nested arrays of type int. Each element in the grid will be set to a random value between -9 and 15. However, the integers that are being placed in the grid are being shown as their ASCII equivalent. Also, each element is the same random int, instead of being completely random. How can I fix these problems? Thanks for all your help!

#define ROWS 5
#define COLS 5

int main(void)
{

    int game_board[ROWS][COLS] = {0};
    initialize_game_board(game_board);
    display_board(game_board);
}

void initialize_game_board (int board[ROWS][COLS])
{

    int rows = 0, columns = 0, rand_int = 0;
    srand( time(NULL) );
    rand_int = (rand() % 24) - 9;

    for (rows = 0; rows < ROWS; rows++)
    {
        for (columns = 0; columns < COLS; columns++)
        {
            board[rows][columns] = rand_int;
        }
    }
}


void display_board(int board[ROWS][COLS])
{

    int row = 0, column = 0;

    printf ("\n%3d", row);
    for (row = 1; row < ROWS; row++)
    {
        printf ("%2d", row);
    }
    printf ("\n");

    for (row = 0; row < ROWS; row++)
    {
        printf ("%-2d", row);
        for (column = 0; column < COLS; column++)
        {
            printf ("%-2c", board[row][column]);
        }
        printf ("\n");
    }
}

Answers


You declared game_board[ROWS][COLUMNS] as an integer array, and then when you use printf, you use %c.

You can either declare it as a character array, and keep your %c (but then you have to change your function generate the ASCII characters for each number), or you can change the printf in the innermost loop of display_board to %d.

Also, as a previous poster said, you only called random once and assigned it to all cells. Call random for each individual cell.


Need Your Help

CLOB Update Failing On Content Below Character Limit

oracle coldfusion clob

I am trying to update a CLOB field using form input in ColdFusion 8. The data being submitted is no more than 10KB in size, yet I get the ORA-01704: string literal too long error.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.