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:
1
2
|<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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<iostream>
#include<string>
#include<ctime>

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<capacity;i++)
	{
		nrs[i]=rand()%999+1;
		cout<<nrs[i]<<" ";
		if(nrs[i]<500)
		{
			underCounter++;
			
		}
		else
		{
			aboveCounter++;
		}


	}

	underCap=new int[underCounter];
	aboveCap=new int[aboveCounter];

	for(int i=0;i<underCounter+aboveCounter;i++)
	{

		if(nrs[i]<500)
		{
		underCap[i]=nrs[i];
		}
		else
		aboveCap[i]=nrs[i];
	}

	cout<<"\nThese "<<underCounter<<" numbers is below 500: "; 
	for(int i=0;i<underCounter;i++)
	{
		cout<<underCap[i]<<" ";
	}

	cout<<"\nThese "<<aboveCounter<<" numbers is above 500: "; 
	for(int i=0;i<aboveCounter;i++)
	{
		cout<<aboveCap[i]<<" ";
	}
	cout<<endl;

	delete[]nrs;
	delete[]aboveCap;
	delete[]underCap;


	return 0;
}
Use an independent index instead of i for the arrays underCap and aboveCap.

1
2
3
4
5
6
7
8
9
10
11
12
13
	int ui=0; // underCap index
	int ai=0; // aboveCap index

	for(int i=0;i<underCounter+aboveCounter;i++)
	{

		if(nrs[i]<500)
		{
		underCap[ui++]=nrs[i];
		}
		else
		aboveCap[ai++]=nrs[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.