•
•
•
•
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
![]() |
•
•
Join Date: Feb 2008
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
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
Thanks in advance.....
BrettW02
Last edited by brettw02 : Feb 14th, 2008 at 5:53 pm.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,809
Reputation:
Rep Power: 11
Solved Threads: 184
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.
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.
•
•
Join Date: Feb 2008
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
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
Thank you for the fast reply.
BrettW02
•
•
Join Date: Feb 2008
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
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
<---
--->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...
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,
Just commented on the other thread.
http://www.daniweb.com/forums/thread96644.html
@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
<---
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <time.h> #include <windows.h> #define MAXRAND 25 #define SIZE 5 using namespace std; int main(){ int row=0, rowCheck=0; int col=0, colCheck=0; int cardHidden [5][5]; int cardShow [5][5]; srand(time(NULL)); //cardHidden [5][5]= (rand() % 10) + 1; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12); for(row=1; row<SIZE; row++) { for(col=1; col<SIZE; col++) { cardShow [row][col]= (rand() % MAXRAND) + 1; } } for(rowCheck=1; row<SIZE; row++) { for(colCheck=1; col<SIZE; col++) { for(row=1; row<SIZE; row++) { for(col=2; col<SIZE; col++) { if(cardShow[rowCheck][colCheck] == cardShow[row][col]) { cardShow[row][col] = (rand() % MAXRAND) + 1; rowCheck=1; colCheck=2; row=1; col=2; } } } } } for(row=1; row<SIZE; row++) { for(col=1; col<SIZE; col++) { cout << cardShow[row][col] << "\t"; } cout << "\n\n"; } system("PAUSE"); return 0; }
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...
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <time.h> #include <windows.h> #define MAXRAND 25 #define SIZE 5 using namespace std; int main(){ int row=0; int col=0; int cardShow [5][5]; // Set of cards (4x4 grid to store unique random values) srand(time(NULL)); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12); // Font color //Set each position in the card array a random value for(row=1; row<SIZE; row++) { for(col=1; col<SIZE; col++) { cardShow [row][col]= (rand() % MAXRAND) + 1; } } //Display each value of card to screen for(row=1; row<SIZE; row++) { for(col=1; col<SIZE; col++) { cout << cardShow[row][col] << "\t"; } cout << "\n\n"; } cout << "\n\nProgram complete.. press any key to exit."; cin.get(); return 0; }
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,
C++ Syntax (Toggle Plain Text)
boolean numberTakenAlready[MAXRAND]; // code to initiaize this entire array to false for(row=1; row<SIZE; row++) { for(col=1; col<SIZE; col++) { bool numberPicked = false; int number; while (!numberPicked) { number = rand() % MAXRAND + 1; // check numberTakenAlready[number - 1] // if it's false, set numberPicked to true to exit loop } cardShow[row][col] = number; numberTakenAlready[number - 1] = true; } }
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
•
•
Join Date: Feb 2008
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
•
•
•
•
C++ Syntax (Toggle Plain Text)
boolean numberTakenAlready[MAXRAND]; // code to initiaize this entire array to false for(row=1; row<SIZE; row++) { for(col=1; col<SIZE; col++) { bool numberPicked = false; int number; while (!numberPicked) { number = rand() % MAXRAND + 1; // check numberTakenAlready[number - 1] // if it's false, set numberPicked to true to exit loop } cardShow[row][col] = number; numberTakenAlready[number - 1] = true; } }
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...
Another way to look at this is to create an array of 16 consecutive numbers:
Then use a shuffle algo to mix them up. Which can be achieved by picking two numbers then swapping them. Repeat until happy.
{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!
Join today don't delay!
•
•
Join Date: Jan 2008
Posts: 1,405
Reputation:
Rep Power: 6
Solved Threads: 179
•
•
•
•
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:
C++ Syntax (Toggle Plain Text)
for(row=1; row<SIZE; row++) { for(col=1; col<SIZE; col++)
You could define the array to be this if you don't want to subtract the 1:
C++ Syntax (Toggle Plain Text)
boolean numberTakenAlready[MAXRAND + 1];
C++ Syntax (Toggle Plain Text)
boolean numberTakenAlready[MAXRAND];
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
- Previous Thread: Deleting more than one file at once?
- Next Thread: Creating a Library



Linear Mode