# random number in a sqaure in C

Page 1 of 1

## 2 Replies - 193 Views - Last Post: 01 December 2017 - 02:23 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=407960&amp;s=d20c06dbf0983b3e83dea540755a52d6&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Voltage1

• New D.I.C Head

Reputation: 0
• Posts: 1
• Joined: 01-December 17

# random number in a sqaure in C

Posted 01 December 2017 - 11:54 AM

okay, so I have to print one random number between 1 and 6 on each of the square arrays. I have done one but for some reason it wont print one the second square?

```#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 9
#define COL 9

char image[2][ROW][COL];
//       COL
//     +----+
//     |    |    ROW
//     |    |
//     +----+
//
// 5x5 X and O images
char shapes[2][5][6] = { { " *** "
, "*   *"
, "*   *"
, "*   *"
, " ***  " }
, { " *** "
, "*   *"
, "*   *"
, "*   *"
, " *** " }
};
void rectange()
{
for(int k = 0; k < 2; k++)
for(int r = 0; r < ROW; r++)
for(int c = 0; c < COL; c++)
{
image[k][r][c] = ' ';
int top    = (r == 0);
int bottom = (r == ROW - 1);
int left   = (c == 0);
int right  = (c == COL - 1);
// corner
if ((top || bottom) && (left || right))
image[k][r][c] = '+';
else
// top or bottom
if (top || bottom) image[k][r][c] = '-';
else
// left or right
if (left || right) image[k][r][c] = '|';
}
}
void overlay_X_or_O()
{
for(int k = 0; k < 2; k++)
{
int shape = rand() % 2;
for(int r = 0; r < 5; r++)
for(int c = 0; c < 5; c++)
image[k][r + 2][c + 2] = shapes[shape][r][c];
}
}
int main()
{
srand(time(NULL)); // initialise random number generator
rectange(); // initialise the image with a rectange
overlay_X_or_O(); // overlay image of X or O on top
//  int num = (rand() % 6 +1);

//    sprintf(roll_num, "%d", (rand() % 6 +1));
// printf("RANDOM = %s", roll_num);
//board[i_coord][j_coord]= x_mark;
//   char roll_num = '3';
//image[1][4][4] = roll_num;
char numers[6] = {'1','2','3','4','5','6'};
image[1][4][4] = numers[(rand() % 6 + 1)];
// print the image
for(int r = 0; r < ROW; r++)
{
for(int k = 0; k < 2; k++)
{
for(int c = 0; c < COL; c++)
printf("%c", image[k][r][c]);
printf("  ");
}
printf("\n");
}
return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: random number in a sqaure in C

### #2 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: random number in a sqaure in C

Posted 01 December 2017 - 01:46 PM

Line 19 has an extra character, is that in your program code?

Would it make more sense for the k loop (image loop) to be outside the row loop, in main?

### #3 jimblumberg

Reputation: 5444
• Posts: 16,968
• Joined: 25-December 09

## Re: random number in a sqaure in C

Posted 01 December 2017 - 02:23 PM

```    image[1][4][4] = numers[(rand() % 6 + 1)];
```

Do you realize that the above code is what is putting the random number into the second graphic? What do you think would happen if you wrote that value to the first graphic ([0])?

Jim