User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 392,038 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,328 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 754 | Replies: 6
Reply
Join Date: Feb 2008
Posts: 4
Reputation: brettw02 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
brettw02 brettw02 is offline Offline
Newbie Poster

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

  #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 5:53 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,809
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 184
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

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

  #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  
Join Date: Feb 2008
Posts: 4
Reputation: brettw02 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
brettw02 brettw02 is offline Offline
Newbie Poster

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

  #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  
Join Date: Feb 2008
Posts: 4
Reputation: brettw02 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
brettw02 brettw02 is offline Offline
Newbie Poster

Help 2D array with uniqqe random numbers...

  #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. }
  54.  

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 7:34 am. Reason: poster hijacked another thread
Reply With Quote  
Join Date: Feb 2008
Posts: 4
Reputation: brettw02 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
brettw02 brettw02 is offline Offline
Newbie Poster

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

  #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  
Join Date: Aug 2005
Posts: 4,667
Reputation: iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice iamthwee is just really nice 
Rep Power: 17
Solved Threads: 298
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

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

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

 {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 3:49 pm.
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Join Date: Jan 2008
Posts: 1,405
Reputation: VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough 
Rep Power: 6
Solved Threads: 179
VernonDozier VernonDozier is offline Offline
Nearly a Posting Virtuoso

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

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Other Threads in the C++ Forum

All times are GMT -4. The time now is 11:05 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC