| | |
random matrix generation
![]() |
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:
the generated matrix should have only 2 0's but it always has more than 2. any ideas?
the number of holes is user input with max value as n/2.
here's what I tried:
C Syntax (Toggle Plain Text)
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"); }
>> 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
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.
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
C Syntax (Toggle Plain Text)
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.
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[][],
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[][],
C Syntax (Toggle Plain Text)
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
this should fix the problem
C Syntax (Toggle Plain Text)
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; }
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: how to get the last key pressed without stopping the programm ?
- Next Thread: linked list question
| Thread Tools | Search this Thread |
#include * ansi api array arrays asterisks binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile copypdffile creafecopyofanytypeoffileinc createcopyoffile createprocess() database dynamic execv fflush fgets file floatingpointvalidation fork forloop frequency function getlogicaldrivestrin givemetehcodez grade graphics gtkwinlinux histogram homework i/o inches include infiniteloop input interest intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft mysql oddnumber open opendocumentformat openwebfoundation pdf pointer posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked reversing scanf scheduling segmentationfault send sequential shape single socket socketprogramming stack standard strchr string suggestions test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






