Arrays and Random Numbers

So I'm supposed to write a program which declares an array which can hold 50 int values, and we have to use a loop to fill the array with 50 random numbers and then display the values 10 per line. Not sure why but it just keeps repeating the same number, so i'm not sure how to proceed from here. Thamks for the help!

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
	const int min_value = 1;
	const int max_value = 50;
	const int ARRAY_SIZE = 50;
	int numbers[ARRAY_SIZE];
	int count;
	int randomNum;
	
	unsigned seed = time(0);
	srand(seed);

	for (count = 0; count < ARRAY_SIZE; count++)
	{
		randomNum = (rand() % (max_value - min_value + 1)) + min_value;

		cout << numbers[count] << endl;
	}
}
I figured out how to solve the random number issue now the only issue i have is setting the numbers 10 per line.
This is what I'm getting when I run the program
/*
5786
19088
3728
27685
14148
29087
18888
7222
19881
20870
1047
32730
3653
6057
21267
15969
26603
7780
232
30800
11326
22916
23766
7136
2104
/*
In your for loop try the following

cout << numbers[count] << " ";
if(count %10){
cout << endl;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
    const int min_value = 1;
    const int max_value = 50;
    const int ARRAY_SIZE = 50;
    int numbers[ARRAY_SIZE];
    int count, i;
    int randomNum;

    unsigned int seed = time(0);
    srand(seed);

    for (count = 0; count < (ARRAY_SIZE / 10); count++)
    {
        for (i = 0; i < 10; i++)
        {
            randomNum = (rand() % (max_value - min_value + 1)) + min_value;
            cout << randomNum << "\t";
        }
        cout << endl;
    }
}

1
2
3
4
5
39      12      32      15      19      35      43      14      34      4
11      26      17      31      49      46      23      33      22      15
35      23      17      5       15      33      10      14      13      1
40      23      36      16      45      29      30      3       12      36
35      23      25      10      24      5       47      8       42      13


I'll leave it to you to work out how to put the random numbers into the array.
Don't forget these are random numbers, they can repeat, they are not unique random numbers.
You need to check they have not already been added to the array:-
http://stackoverflow.com/questions/15094834/check-if-a-value-exist-in-a-array


added
jamesfarrow (118) is a better solution, but you need to check duplicates.
Last edited on
Topic archived. No new replies allowed.