![]() |
|
#1
|
|||
|
|||
weird loop problem. plz helpHey,
I've been looking at this for a while and it is probably something so simple and needs another pair of eyes to solve it. So here goes... I have an integer array of 6 integers. The point of it is that each integer holds a position between 0-5, but no position can be repeated twice. I want it to be completely random so I use srand to generate a random number, then I want to test to make sure it is not already in the array, and if it isn't place it in the array. Here is what my code looks like (I put a bunch of cout's to try and debug this thing which I'll explain the results of below) CPP / C++ / C Code:
i is intialized to 0 before the loop, so it starts as 0. I used a for loop to set all the integers in the letterOrder array to 9, that way when the loop starts, no number will match up between the random number generated between 0-5: CPP / C++ / C Code:
More cout's for my testing, and the result of this cout is 9's for every integer. However, the first time the loop runs, letterOrder[5] is equal to 5, instead of 9. I have no idea why it changes to 9, and it is the only one that changes to 9. letterOrder[0-4] are all equal to 9. This is where I'm stuck, because obviously as it loops, the final output won't be correct since 5 already occupies one of the positions in the array. Last edited by LuciWiz : 03-Jun-2005 at 00:27.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|
#2
|
|||
|
|||
|
There is the following error with your code:
Quote:
I think you actually wish it to do the following: CPP / C++ / C Code:
P.S: just for the sake of clarity, if you have something like: Code:
Last edited by LuciWiz : 03-Jun-2005 at 07:26.
Reason: Please insert your C code between [c] & [/c] tags
|
|
#3
|
|||
|
|||
|
Hey, thanks for your help, but... I think you were a little confused on what I was trying to do, most likely because I didn't explain it well enough.
I got it to work by changing my code to this: Code:
My logic is as follows: It generates a random number between 0-5. Then it tests to see whether this random number has already been generated and placed in any of the slots of the letterOrder array. If it has been used, it generates a random number again and again until it generates one that has not. If it hasn't been used, it breaks out of the loop and places it in the array in the correct spot. This seems extremely inefficient to me. For instance, if letterOrder was anything more than a 6 integer array, that do/while statement will look extremely messy and long. Is there a better way to write that while statement? Or even the whole loop for that matter. At least I got it to work tho, thanks |
|
#4
|
|||
|
|||
|
Quote:
Quote:
Your problem is actually a re-arranging problem. In principle you have numbers 1-n which you have to rearrange to a random order. Correct me if you think otherwise but please put tought to your argumentation. Then your problem becomes more simple if you initialize your array with values 1-n after which you call n times function (or loop n times through a piece of code) that will put the existing values in array to a new, random, order. like CPP / C++ / C Code:
|
|
#5
|
|||
|
|||
|
Oh, I see. I knew there was a better way to do it. Thank you for help, I was not trying to be rude or anything.
|
Recent GIDBlog
Meeting the local Iraqis by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Graphic problem in Unreal Tournament 2004 | zerox | Computer Software Forum - Games | 10 | 09-Oct-2005 12:31 |
| Array 1 dimensional help please asap | lion123 | C Programming Language | 10 | 18-Feb-2005 21:53 |
| Problem - for loop input | st8tic1 | C++ Forum | 1 | 06-Mar-2004 06:42 |
| weird search problem!! | JUNK KED | Open Discussion Forum | 3 | 11-Oct-2003 00:48 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The