943,691 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 5228
  • C RSS
Apr 6th, 2006
0

random matrix generation

Expand Post »
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:
  1.  
  2. int a[10][10]={0};
  3. int n, holes, noofelements, randomno, rowpos, colpos;
  4. int i,j;
  5. srand(time(NULL));
  6. do
  7. {
  8. printf("Enter the size of the square (+ve integer, max 257): ");
  9. scanf("%d", &n);
  10. if (n<1 || n>256)
  11. printf("Invalid size.");
  12. }
  13. while (n<1 || n>256);
  14. do
  15. {
  16. printf("Enter the number of holes (max %d): ", n/2);
  17. scanf("%d", &holes);
  18. }
  19. while (holes>(n/2));
  20. noofelements=n*n-holes;
  21. for (i=1; i<=noofelements; i++)
  22. {
  23. rowpos=rand()%n;
  24. colpos=rand()%n;
  25. a[rowpos][colpos]=i;
  26. }
  27. for (i=0; i<n; i++)
  28. {
  29. for (j=0; j<n; j++)
  30. printf("%d\t", a[i][j]);
  31. printf("\n");
  32. }
the generated matrix should have only 2 0's but it always has more than 2. any ideas?
Similar Threads
Reputation Points: 12
Solved Threads: 0
Newbie Poster
iamboredguy is offline Offline
23 posts
since Aug 2004
Apr 6th, 2006
0

Re: random matrix generation

>> 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
  1. const int MaxSize = 10
  2. int a[MaxSize][MaxSize];
  3. ...
  4. ...
  5. 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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005
Apr 7th, 2006
0

Re: random matrix generation

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.
Reputation Points: 12
Solved Threads: 0
Newbie Poster
iamboredguy is offline Offline
23 posts
since Aug 2004
Apr 7th, 2006
0

Re: random matrix generation

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[][],

  1. Enter the size of the square (+ve integer, max 257): 5
  2. Enter the number of holes (max 2): 2
  3. -1 -1 8 19 11
  4. 16 -1 17 12 -1
  5. -1 -1 -1 2 21
  6. -1 -1 4 -1 20
  7. 10 18 22 15 23
  8. Press any key to continue
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005
Apr 7th, 2006
0

Re: random matrix generation

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.
Reputation Points: 12
Solved Threads: 0
Newbie Poster
iamboredguy is offline Offline
23 posts
since Aug 2004
Apr 7th, 2006
0

Re: random matrix generation

this should fix the problem
  1. const int MaxN = 10;
  2. ...
  3. ...
  4. for(i = 0; i < MaxN; i++)
  5. {
  6. for(j = 0; j < MaxN; j++)
  7. a[i][j] = -1;
  8. }
  9. ...
  10. ...
  11. ... // code omotted
  12. ...
  13. for (i=1; i<=noofelements; i++)
  14. {
  15. do {
  16. rowpos=rand() % n;
  17. colpos=rand() % n;
  18. } while( a[rowpos][colpos] >= 0);
  19. a[rowpos][colpos]=i;
  20. }
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,950 posts
since Aug 2005
Apr 7th, 2006
0

Re: random matrix generation

that works. thanks a lot!
Reputation Points: 12
Solved Threads: 0
Newbie Poster
iamboredguy is offline Offline
23 posts
since Aug 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Sorting structures
Next Thread in C Forum Timeline: linked list question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC