0

for my project, I need to generate an n*n matrix (n is user input) with numbers from 1 to x (x<n) positioned randomly. I tried implementing it with this code but it doesnt seem to work. x = n*n - number of holes.

the number of holes is user input with max value as n/2.

here's what I tried:

```
int a[10][10]={0};
int n, holes, noofelements, randomno, rowpos, colpos;
int i,j;
srand(time(NULL));
do
{
printf("Enter the size of the square (+ve integer, max 257): ");
scanf("%d", &n);
if (n<1 || n>256)
printf("Invalid size.");
}
while (n<1 || n>256);
do
{
printf("Enter the number of holes (max %d): ", n/2);
scanf("%d", &holes);
}
while (holes>(n/2));
noofelements=n*n-holes;
for (i=1; i<=noofelements; i++)
{
rowpos=rand()%n;
colpos=rand()%n;
a[rowpos][colpos]=i;
}
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
printf("%d\t", a[i][j]);
printf("\n");
}
```

the generated matrix should have only 2 0's but it always has more than 2. any ideas?