2D array with uniqqe random numbers...

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 4
Reputation: brettw02 is an unknown quantity at this point 
Solved Threads: 0
brettw02 brettw02 is offline Offline
Newbie Poster

Re: Help Me!! New To C++!! Arrays!!

 
0
  #1
Feb 14th, 2008
Love the idea of the program. Im quite interested in seeing how to get something like this to work. Is there any chance someone could add a working version to the code snippets section or possibly send me a working snippet in PM? Im trying to educate my self and self-learn c++... I dont at the moment have any books.

Thanks in advance.....
BrettW02
Last edited by brettw02; Feb 14th, 2008 at 6:53 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Help Me!! New To C++!! Arrays!!

 
0
  #2
Feb 14th, 2008
The best way to learn actually is to try to do it yourself.

A good reference is always a very useful help. I refer to these two often:
C/C++ Reference (short and sweet)
cplusplus.com (all the gory details)

Start small, add on details as you go, and you'll be able to do much more complicated things in short order.

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 4
Reputation: brettw02 is an unknown quantity at this point 
Solved Threads: 0
brettw02 brettw02 is offline Offline
Newbie Poster

Re: Help Me!! New To C++!! Arrays!!

 
0
  #3
Feb 17th, 2008
Great sites... been to them more then a few time - LOL. I love doing the prgramming myself. I have an easier time learning some chunks by taking a built program and manipulating it to see what changes and so on - this way I am able to figure out what is doing what.

Thank you for the fast reply.

BrettW02
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 4
Reputation: brettw02 is an unknown quantity at this point 
Solved Threads: 0
brettw02 brettw02 is offline Offline
Newbie Poster

2D array with uniqqe random numbers...

 
0
  #4
Feb 17th, 2008
OK... at attempting this program I cant even get a two dimensional array to print a 4 x 4 grid of random numbers that are all original (I know I know, I only need 8 different numbers - but I am trying to get no duplicates first b4 I attempt to get a duplicate of only 2 of each... ) Please help... thank you

@Duoas
Maybe - U could throw me a test chunk of a snippet I could play with which could help me develop this game idea....

bw02

here is what I was playing with
<---
  1. #include <iostream>
  2. #include <time.h>
  3. #include <windows.h>
  4.  
  5. #define MAXRAND 25
  6. #define SIZE 5
  7.  
  8. using namespace std;
  9.  
  10. int main(){
  11.  
  12. int row=0, rowCheck=0;
  13. int col=0, colCheck=0;
  14.  
  15.  
  16. int cardHidden [5][5];
  17. int cardShow [5][5];
  18. srand(time(NULL));
  19.  
  20. //cardHidden [5][5]= (rand() % 10) + 1;
  21.  
  22. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
  23.  
  24. for(row=1; row<SIZE; row++)
  25. {
  26. for(col=1; col<SIZE; col++)
  27. {
  28. cardShow [row][col]= (rand() % MAXRAND) + 1;
  29. }
  30.  
  31. }
  32.  
  33.  
  34. for(rowCheck=1; row<SIZE; row++)
  35. {
  36. for(colCheck=1; col<SIZE; col++)
  37. {
  38.  
  39. for(row=1; row<SIZE; row++)
  40. {
  41. for(col=2; col<SIZE; col++)
  42. {
  43. if(cardShow[rowCheck][colCheck] == cardShow[row][col])
  44. {
  45. cardShow[row][col] = (rand() % MAXRAND) + 1;
  46. rowCheck=1;
  47. colCheck=2;
  48. row=1;
  49. col=2;
  50.  
  51.  
  52.  
  53. }
  54. }
  55. }
  56. }
  57. }
  58.  
  59.  
  60.  
  61.  
  62. for(row=1; row<SIZE; row++)
  63. {
  64. for(col=1; col<SIZE; col++)
  65. {
  66. cout << cardShow[row][col] << "\t";
  67.  
  68. }
  69.  
  70.  
  71. cout << "\n\n";
  72.  
  73. }
  74.  
  75.  
  76.  
  77. system("PAUSE");
  78.  
  79. return 0;
  80. }
--->Thanks

Im having one heck of a time here....

Problem I am attempting to solve. I have created a 2 dimensional array - each position in the array is assigned a random number. I am trying to create a 4 x 4 grid (given the 2d array). I have set the array[5][5] for an extra row and column to print a location...

IE:

----1---2---3---4
A | X | X | X | X

B | X | X | X | X

C | X | X | X | X

D | X | X | X | X

(Where X is the random number)

I need 8 pairs of random values as well as the 8 values to be in different locations every time the program is run....

to get the random numbers in the array here is what I did...

  1.  
  2. #include <iostream>
  3. #include <time.h>
  4. #include <windows.h>
  5.  
  6.  
  7. #define MAXRAND 25
  8. #define SIZE 5
  9.  
  10. using namespace std;
  11.  
  12. int main(){
  13.  
  14. int row=0;
  15. int col=0;
  16.  
  17.  
  18. int cardShow [5][5]; // Set of cards (4x4 grid to store unique random values)
  19. srand(time(NULL));
  20.  
  21.  
  22. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12); // Font color
  23.  
  24.  
  25. //Set each position in the card array a random value
  26. for(row=1; row<SIZE; row++)
  27. {
  28. for(col=1; col<SIZE; col++)
  29. {
  30. cardShow [row][col]= (rand() % MAXRAND) + 1;
  31. }
  32.  
  33. }
  34.  
  35.  
  36. //Display each value of card to screen
  37. for(row=1; row<SIZE; row++)
  38. {
  39. for(col=1; col<SIZE; col++)
  40. {
  41. cout << cardShow[row][col] << "\t";
  42.  
  43. }
  44.  
  45. cout << "\n\n";
  46. }
  47.  
  48.  
  49. cout << "\n\nProgram complete.. press any key to exit.";
  50. cin.get();
  51.  
  52. return 0;
  53. }

I have tried so many things to get the random numbers to be unique and to show in 2 random positions in the array but cant figure it out.... Please help!!!!

I know I have to take baby steps but maybe someone could help me with a "step-by-step"
mini guide to help me achieve my goal.

Thank you in advance - -

If I understand correctly, you want to check your random numbers as you are creating them to make sure that the same number was not assigned to an earlier card? If that is the case, one solution could involve setting up a boolean array of MAXRAND elements and initializing them to false. As a random number is generated, that array is checked to see whether that number has been selected before. If so, generate another random number. If not, assign it to cardShow[row][col]. After assigning it, flag that number as taken by assigning the array for that number index to be true now. If at a later time that number is generated again, when the array is checked it will show up as already having been assigned and thus discarded. Taking your code and adding code/pseudo-code,
  1. boolean numberTakenAlready[MAXRAND];
  2. // code to initiaize this entire array to false
  3. for(row=1; row<SIZE; row++)
  4. {
  5. for(col=1; col<SIZE; col++)
  6. {
  7. bool numberPicked = false;
  8. int number;
  9. while (!numberPicked)
  10. {
  11. number = rand() % MAXRAND + 1;
  12. // check numberTakenAlready[number - 1]
  13. // if it's false, set numberPicked to true to exit loop
  14. }
  15. cardShow[row][col] = number;
  16. numberTakenAlready[number - 1] = true;
  17. }
  18. }

Just commented on the other thread.
http://www.daniweb.com/forums/thread96644.html
Last edited by Ancient Dragon; Feb 18th, 2008 at 8:34 am. Reason: poster hijacked another thread
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 4
Reputation: brettw02 is an unknown quantity at this point 
Solved Threads: 0
brettw02 brettw02 is offline Offline
Newbie Poster

Re: 2D array with uniqqe random numbers...

 
0
  #5
Feb 18th, 2008
Originally Posted by brettw02 View Post
  1. boolean numberTakenAlready[MAXRAND];
  2. // code to initiaize this entire array to false
  3. for(row=1; row<SIZE; row++)
  4. {
  5. for(col=1; col<SIZE; col++)
  6. {
  7. bool numberPicked = false;
  8. int number;
  9. while (!numberPicked)
  10. {
  11. number = rand() % MAXRAND + 1;
  12. // check numberTakenAlready[number - 1]
  13. // if it's false, set numberPicked to true to exit loop
  14. }
  15. cardShow[row][col] = number;
  16. numberTakenAlready[number - 1] = true;
  17. }
  18. }

Just commented on the other thread.
http://www.daniweb.com/forums/thread96644.html
Ok this is great... I am only unsure of one thing here --» I am a little unsure of the reason for [number - 1] -- Im sure im just overlooking something rather simple...
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: 2D array with uniqqe random numbers...

 
0
  #6
Feb 18th, 2008
Another way to look at this is to create an array of 16 consecutive numbers:

  1. {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}

Then use a shuffle algo to mix them up. Which can be achieved by picking two numbers then swapping them. Repeat until happy.
Last edited by iamthwee; Feb 18th, 2008 at 4:49 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,842
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: 2D array with uniqqe random numbers...

 
0
  #7
Feb 18th, 2008
Originally Posted by brettw02 View Post
Ok this is great... I am only unsure of one thing here --» I am a little unsure of the reason for [number - 1] -- Im sure im just overlooking something rather simple...
The 1 is subtracted because in C++ array indexes start at 0 and your loops:
  1. for(row=1; row<SIZE; row++)
  2. {
  3. for(col=1; col<SIZE; col++)
start at 1. Your cardShow array is not utilizing the 0 indexes of the array. The numberTakenAlready array is. You're generating random numbers from 1 to MAXRAND and the array indexes are from 0 to MAXRAND - 1. Everything's one off since array indexes always start at 0.

You could define the array to be this if you don't want to subtract the 1:
  1. boolean numberTakenAlready[MAXRAND + 1];
rather than
  1. boolean numberTakenAlready[MAXRAND];
That way numberTakenAlready[MAXRAND] would not be a segmentation fault and you'd no longer have to subtract one. The important thing is to simply be consistent so everything matches up. Either subtract 1 everywhere when accessing or changing it or do it nowhere.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 2095 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC