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?

Recommended Answers

All 6 Replies

>> if (n<1 || n>256)

your array is only 10 rows and columns. If I enter a value of 255 then your program will crash bigtime. Instead of hard-coding a value like 256, create a define or const int to declare the array size and its limits

const int MaxSize = 10
int a[MaxSize][MaxSize];
...
...
if (n<1 || n>MaxSize)

now remember to decrement the value of n by one (range is 0 to (but not including) MaxSize) before using it as an index into the array.

I know about the size of the array. I just put 10 so that the program doesnt eat up too much memory during testing. I'm pretty sure I decremented the value of n as well. something else is wrong.

If you pre-fill all elements of the array with -1 then you will more easily see the cells that were never toched. And the reason is because the program generates random values for rowpos and colpos, and sometimes those values have already been generated. So instead of generating (5*5)-2 = 23 random values it generates something less than that.

One way to correct that would be to check the array to see of the values have been previously generated, and if they have generate new random values. If a[rowpos][colpos] has a values other than -1 then you need to generate new random values.

Probably a faster way to do it is to use two more int arrays, one for rowpos and the other for colpos, initialize each array with numbers from 0 to n, then randomly shuffle the rows. After than you don't need to generate any more random numbers, just use the arrays from row 0 to N the indices into array a[][],

Enter the size of the square (+ve integer, max 257): 5
Enter the number of holes (max 2): 2
-1      -1      8       19      11
16      -1      17      12      -1
-1      -1      -1      2       21
-1      -1      4       -1      20
10      18      22      15      23
Press any key to continue

nice idea. but how do I randomly shuffle a row? taking rowpos[]={0,1,2,3} how do I shuffle? using rand() won't work since I would have the same problem as before.

this should fix the problem

const int MaxN = 10;
...
...
	for(i = 0; i < MaxN; i++)
	{
		 for(j = 0; j < MaxN; j++)
			 a[i][j] = -1;
	}
...
...
... // code omotted
...
	for (i=1; i<=noofelements; i++)
	{
		do {
			rowpos=rand() % n;
			colpos=rand() % n;
		} while( a[rowpos][colpos] >= 0);
		a[rowpos][colpos]=i;
	}

that works. thanks a lot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.