Help with a fun (simple) little project I'm working on.

Hello,
This is my first post on these forums. I recently got in to programming (about two weeks ago).
I'm a civil engineer major but enjoy doing a lot of other non civil, engineering related projects on my free time.
As this is my first post, I just wanted to say, this website and the forums have taught me significantly more than my (for some reason) required semester of ANSI C ever taught me, and has compelled me to delve further in to the world of programming. I thank you all for this.
I began programming in c++ in order to produce, what I still consider to be, a "simple", turned based strategy game. Here is my 5th, maybe 6th iteration of the base of the code. I am trying to refine, and learn really what it is I am doing before I start adding too much to the code.

If you skipped that intro! I will now get to the point. I am trying to randomize an array of 5 integers that for now can range from 1-10. Some of the code may not make sense at this point because of plans I have for it in the future, but currently:

I DO NOT RETURN VALUES RANGING FROM 1-10, WHICH IS WHAT I WISH FOR MY VALUES TO BE LIMITED TO. Currently, my first two integers are consistently between 1-10 and the third is in the 10^(4 or 5) range and the last two are in the hundreds.
Can you please help me figure out what is wrong with my code? I would like 5 integers between 1-10 to be assigned to the array "move_1_0"... and Thanks!
 ``12345678910111213141516171819202122232425262728293031323334`` ``````#include #include #include using namespace std; int main() { int move_1_0 [5], reloop, i, y, n; srand(time(NULL)); for(i=0;i<5;i++) { if (move_1_0[i] == 0) //move__ <1-5> { bool reloop = true; while (reloop) { move_1_0[i] = rand()%10+1; //<-Shouldn't this confine my random numbers to 1-10? for (y=0;y<5;y++) { if(move_1_0[i] != move_1_0[y]) { reloop = false; break; } /*in order to ensure no number shows up twice */ } } } cout<

(I am using Dev C++)
...and double thanks if you can help!
Yes, your rand line (line 18) will confine the random numbers to 1-10.

The problem is your rand line is not executing because the if statement on line 13 is failing.

You never initialize your move_1_0 array... so the value it contain are garbage (probably not zero).

To fix... initialize it:

 ``1234`` ``````int move_1_0 [5] = {0,0,0,0,0}; // or... if you're lazy, just give it 1 value, and then the rest will be automatically zero'd int move_1_0 [5] = {0};``````
Oh, I thought the default numbers in an array are set to zero for some reason. I should have thought to do this. Thanks for the help Disch!

So, I set"move_1_0[5]={0}", my program runs now and I return five random numbers between 1-10, as I wanted.

The purpose of my while loop, however, is to make sure no two numbers are repeated, but I DO get repeating numbers.
Can anyone help me in achieving 5 unique numbers with my code? Thank you.

New code: (nothing else has changed)
 ``123456789101112131415161718192021222324252627282930313233`` ``````#include #include #include using namespace std; int main() { int move_1_0 [5]={0}, reloop, i, y, n; srand(time(NULL)); for(i=0;i<5;i++) { if (move_1_0[i] == 0)//move__ { bool reloop = true; while (reloop) { move_1_0[i] = rand()%10+1; for (y=0;y<5;y++) { if(move_1_0[i] != move_1_0[y]) { reloop = false; break; } } } } cout<
Last edited on
 ``1234567891011121314151617181920`` ``````void randomize_array (int arr[]) { bool done = false; bool isdup; int n = 0; // number of entries so far int num; while (n < 5) { num = rand()%10+1; isdup = false; for (int i=0; i

I still don't see why my method wasn't equally as valid (aside from the whole not working thing). But thanks AA.
The logic difference I see is your if statement at line 21. As soon as you find two entries that don't match, you drop out of the for loop and stop checking. Consider the following array { 1, 2, 2 }. First time through the for loop, with i = 2 and y = 0, the not equal condition will be met and you stop checking.
To whom it may concern:
Thanks to all those who helped me. Your code worked AA, but I still don't completely understand all the logic. I actually ended up randomizing numbers for arr[] then used your code to check for dups after.
But anyways this is the code I wrote myself, and it works. Still pretty sloppy but at least I fully understand it, and it works!
 ``123456789101112131415`` ``````void position_0 (int arr[]) { for (int n=0; n<5; n++) { if (arr[n] == 0) { arr[n] = rand()%10+1; //random number generated while ((arr[n] == arr[n-1])||(arr[n] == arr[n-2])||(arr[n] == arr[n-2])||(arr[n] == arr[n-3])||(arr[n] == arr[n-4])) //check for dup { arr[n] = rand()%10+1; //replace dup and checks again in loop } } cout<
Topic archived. No new replies allowed.