# Thread: How do i get this program to print random numbers?

1. ## How do i get this program to print random numbers?

okay, so this is the program so far, it can print one number that I set it to. how can I edit this so it will print a random number which will be printed on the front of the two squares??

Code:
```#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
// print the image
char roll_num = '2,3';
image[0][4][4] = roll_num;
// 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;
}```

2. ok take that code I gave you, and think about it
Code:
```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;

// you are going to need to remember, or think
//for X and O to print out, and how you're
//going to get the both of them to have
// a number printed in each of them
//3D arrays
//I can figure it out, so can you.
//because you wrote it,
//that is only if you want both of them
//to show a number, of course.

char numbers[6] = {'1','2','3','4','5','6'};
image[1][4][4] = numbers[(rand() % 6)];

// 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;
}```
You need to print a char 1 - 6?? or whatever , but you need it to be random and you need to slip it in at run time. Rand give int .. How could you do to get a char 1 thru x to be random using rand() that returns an int and an char array that is filled with the numbers 1 thru x then call rand() to return to get the the element number to get the the char at a random 1 thru x the assign it to your X O char array?

sorry I didn't catch your post to me in your other post on this.

3. right okay got you! I've sorted that thank you!, how do I do it so the random number appears in both squares? because at the moment it only appears in one of them
also that's fine you have been very helpful!

4. Originally Posted by Santac
right okay got you! I've sorted that thank you!, how do I do it so the random number appears in both squares? because at the moment it only appears in one of them
also that's fine you have been very helpful!
come on, use that brain God gave you, think about it, look closely at how it is getting that one in one of your print outs. You wrote this 3D array what part of it separates the two ?

5. okay I've tried and tried but no success. it just wont print a random number on the other square??

6. Originally Posted by Santac
okay I've tried and tried but no success. it just wont print a random number on the other square??
You cannot see how it is actually putting that one number into your 3d array?

did you write that 3D array? you got 3 element "holders"
Code:
`image[2][ROW][COL]`
you wrote a bunch of complected code to get that to overlay into another 3d array. How did you figure that out? Now you're telling me you cannot figure out how to add the other number into the other part of the 3d array? what is the first [ ] for? what is the second [ ] for, what is the third [ ] for in a 3d array?

Because it can be done
Code:
```+-------+  +-------+
|       |  |       |
|  ***  |  | *   * |
| *   * |  |  * *  |
| * 5 * |  |   3   |
| *   * |  |  * *  |
|  ***  |  | *   * |
|       |  |       |
+-------+  +-------+```

7. okay ive done it! great thank you!
but when ever the number 1 comes up it shifts over to one side?

8. Originally Posted by Santac

okay ive done it! great thank you!
but when ever the number 1 comes up it shifts over to one side?
yep looks like you got some more work ahead of you to make it do what you want it to do. maybe re think how you're implementing it even. I do not know, because I do not know what you're trying to ultimately accomplish. nor do I even know how to deal with a 3d array. per se'

EDIT:
Code:
```\$ ./x_and_o_3darray
+-------+  +-------+
|       |  |       |
| *   * |  |  ***  |
|  * *  |  | *   * |
|   1   |  | * 4 * |
|  * *  |  | *   * |
| *   * |  |  ***  |
|       |  |       |
+-------+  +-------+

\$ ./x_and_o_3darray
+-------+  +-------+
|       |  |       |
|  ***  |  |  ***  |
| *   * |  | *   * |
| * 6 * |  | * 1 * |
| *   * |  | *   * |
|  ***  |  |  ***  |
|       |  |       |
+-------+  +-------+```

9. this is the code. how have you done that so the 1 doesn't shift everything??

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

char image[1][ROW][COL];
//       COL
//     +----+
//     |    |    ROW
//     |    |
//     +----+
//
// 5x5 O 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 O 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]= O_mark;
//image[1][4][4] = roll_num;
char numers[6] = {'1','2','3','4','5','6'};
image[0][4][4] = numers[(rand() % 6 + 1)];
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;
}```

10. The numers array has valid indexes between 0 and 5, but (rand() % 6 + 1) returns a value between 1 and 6.

11. look at post 10 and then read this, and you should then know why I wrote it like that
Code:
```  char numbers[6] = {'1','2','3','4','5','6'};
image[1][4][4] = numbers[(rand() % 6)];```
at least you got the other part right (finally) you just forgot you're now dealing with the element numbers 0 - 5 and no longer the numbers in the array 1 - 6. plus I redid it and added the b to numbers.

12. done it! thank you!

13. yeay! now you can move on to more complex coding.

14. not yet! I still need to make it interact with the user. haha

15. printing two O's or two X's at the same time looks problematic already.