I have created a grid wich allows the user to type in a width and height and then prints to the screen with that width and height as dots. Now i need to randomly generate points in the grid to come out as "x" instead of dots. The tricky part is that the user also types in a value wich corresponds to the number of points to be randomly generated into the grid. So say for example he chooses 5, then 5 random dots in the grid will be printed as x's and the rest of the grid will be dots. I am not sure how to go about this and need some help. I have figured out how to randomly generate numbers (inefficiently though). The code for both the grid and random point generation are as follows:
Code:
# include <stdio.h>
# include <math.h>
# include <stdlib.h>
int lx;
int ly;
char grid [10] [10];
int z;
int j;
int main()
{
printf ("Insert the width (Lx) and height (Ly) of the grid \n");
scanf ("%d %d", &lx, &ly);
for (z = 0; z < lx; z++)
{
for (j=0; j < ly; j++)
{
grid [z] [j] = '.';
}
}
for (z = 0; z < lx; z++)
{
for (j=0; j < ly; j++)
{
printf ("%c", grid [z] [j]);
}
printf ("\n");
}
}
Code:
# include <stdio.h>
main()
{
int random_i(int max);
int i;
int nmax=10, nn=20;
int seed=3;
srandom(seed);
for (i=0;i<nn;i++) {
printf("%d\n",random_i(nmax));
}
}
int random_i(int max) {
return (random() % max);
}
My main problem is that first i have no idea how to go about involving the random numbers in the same code as the grid. and second how would i go about making a random distribution of x's in the grid?