Putting random numbers into an array

I am having some trouble putting random numbers into an array. Any tips on how I can do this? My code is shown below:

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
#include <iostream>
#include <cstdlib> 
#include <ctime>
using namespace std;

int main()
{
	int number = 0;
	int count = 0;
	float average = 0;
	int largest = -1;
	int smallest = -1;
	int *array;

	cout << "Please enter a number between 20 and 100: ";
	cin >> number;

	if (number > 20 && number < 100)
	{
		array = new int[number];
		srand((unsigned)time(0));
		int random;
		for (int index = 0; index < number; index++)
		{
			random = (rand() % 1000) + 1;
			
		}
	}
	else
	{
		cout << "The number must be between 20 and 100. \n";
		cout << "Please enter a number between 20 and 100: " << endl;
		cin >> number;
	}
	system("Pause");
	return 0;
}
How would you put anything into an array ?

Edit: Maybe you should read the other 2 post you made and comment there.
Last edited on
My code was updated so I made another post. Thanks for the help.
1
2
3
4
5
6
7
8
9
int k = 0;
array = new int[number];
srand((unsigned)time(0));
int random;
for (int index = 0; index < number; index++)
{
	random = (rand() % 1000) + 1;	
	array[k] = random; k++;
}


You need to use the array now, correct?

Krulcifer Einfolk
Last edited on
Thank you. Yes I will need to be pulling some data from it
My code is compiling but when it runs I'm getting an infinite loop of 168. Anything obvious I am doing wrong? I tried adding code so it can display the contents of the array in lines of 10.

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
#include <iostream>
#include <cstdlib> 
#include <ctime>
using namespace std;

int main()
{
	int number = 0;
	int count = 0;
	float average = 0;
	int largest = -1;
	int smallest = -1;
	int *array;
	

	cout << "Please enter a number between 20 and 100: ";
	cin >> number;

	if (number >= 20 && number <= 100)
	{
		int k = 0;
		array = new int[number];
		srand((unsigned)time(0));
		int random;
		for (int index = 0; index < number; index++)
		{
			random = (rand() % 1000) + 1;
			array[k] = random; k++;
		}
		for (int i = 0; i < 1000;)
		{
			cout << array[i] << "";
			if ((i + 1) % 10 == 0)
			{
				cout << endl;
			}
		}
	}
	else
	{
		cout << "The number must be between 20 and 100. \n";
		cout << "Please enter a number between 20 and 100: " << endl;
		cin >> number;
	}
	system("Pause");
	return 0;
}
Last edited on
You never update the loop counter (i) on line 30.

Try this:
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
#include <iostream>
#include <cstdlib> 
#include <ctime>
#include <cassert>

using namespace std;

int main( /* int argc, char** argv */ ) {
	int
                number = 0,
		count = 0,
		largest = -1,
		smallest = -1,
		i = 0,
		random  = 0;
	float average = 0.0;
	int*  array = nullptr;
	
        // do...while() is REALLY good for verifying input
	do {
		cout << "Please enter a number between 20 and 100: ";
		cin >> number;
	} while(number < 20 || number > 100);

	array = new int[number];
        assert(array); // Allocation check
	srand( (unsigned)time(0) );

        // Why did you have two loop counters for this? Only one is needed
	for(i = 0; i < number; i++) {
		random = ( rand() % 1000 ) + 1;
		array[i] = random;
	} // END for(i)

	for (i = 0; i < number; i++) {
		cout << array[i] << ' ';
		if( (i + 1) % 10 == 0) {
			cout << '\n';
		} // END if
	} // END for(i)

return 0;
} // END main 
Last edited on
Topic archived. No new replies allowed.