Put random values into a dynamic array!

Hey guys. I am doing a program which should do following:
1. Let the user decide how many numbers should be randomized.
2. Create an dynamic array with exact the same amount of storages.
3. Then check how many of theese values were above 500 and how many were above 500.
4. Now create 2 new dynamic arrays with the exact amount of storages needed to store theese values.
5. Write theese values out on the screen.

I have done the major of the code in a way i think is right. The problem is this:
When i check if a value is below 500 and wich is above. Lets say that the first random number is <500, then my `belowCap[i]=nrs[i]`. Now lets say that the second random number is >500, Then my `aboveCap[i]=nrs[i]` will not be placed in the first storage of `aboveCap` but in the `[i]` storage.

Which means that i do not fill my arrays from 0-> and there will be gaps in my arrays and the program will get logical or run_time errors.

Visual example:
lets say that 1:value<500, 2:value>500 and so on. My arrays will be filled like this:
 ``12`` ``````|<500| | | |<500| | | |<500| <-- below 500 array | | |>500| | | |>500| | | <-- above 500 array``````

My question is simply how i solve this problem. I have been thinking for a while but my brain does not work anymore so if i could get a few hints i would be happy!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172`` ``````#include #include #include using namespace std; int main() { int *nrs=NULL; int *underCap=NULL; int *aboveCap=NULL; int capacity=0; int underCounter=0; int aboveCounter=0; cout<<"How many numbers between 0-999 would you like to randomize?: "; cin>>capacity; nrs=new int[capacity]; for(int i=0;i
Use an independent index instead of i for the arrays underCap and aboveCap.

 ``12345678910111213`` `````` int ui=0; // underCap index int ai=0; // aboveCap index for(int i=0;i

Also you forgot to seed the PRNG (pseudorandom number generator).
You do this by calling srand() once at the beginning of main().

If you don't call srand(), you will get the exact same sequence of numbers from rand() every time the program runs.

Most people feed it time() so the final line of code will look like:

`srand(time(NULL));`

http://www.cplusplus.com/reference/cstdlib/srand/

I could give more suggestions with regards to style, but those do not really have a great impact on the program itself.
Last edited on
So that was all it took -.- I'm so sick of this shit right now xD
Yeah thanks for reminding me of the srand :) I also use to set is a an `unsigned`

Thanks for the help :)
Topic archived. No new replies allowed.