Storing Random Numbers in an Arrary

Hi,

I'm new to learning c++, and I was wondering what would be the best way to store 10 random numbers to an array ranging from 1-100. My teacher did not really cover this.

Thanks,
Galen
Well first of all, you need to the declare the size of the array(in this case the size of the array is 10) ahead of time. Then you have to seed your program by using the function srand(time(NULL)). Then you create a for loop and what ever random numbers the program generates, you have to extract it back to your array. These are the basics on how to do it.
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
#include <iostream>
#include <ctime>
using namespace std;
int input (int []);
int output (int []);
int main ()
{
int rNumb[50];
input (rNumb);
output (rNumb);
}

int input (int x[])
{
	int i = 0;
	srand(time(0));
	for(int i=0; i<50; i++) 
    x[i] = rand()%100+1; 
	return x[i];
}

int output (int x[])
{
	int i = 0;
	for (int i = 0; i < 10; i++)
	{
    if (x[i] % 2 == 1)
         cout << x[i] << " ";
	}
	return x[i];
}


This is some code that I found by searching the forums. Is there anything wrong with this code? I need to change 50 to 10 so that it only stores 10 random numbers. I'm trying to find something online that goes into depth about this, but I cannot.
Try understanding this example and ask if you have any questions.

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

using namespace std;

void input(int [], int &);
void output(int [], int &);

int main ()
{
int SIZE = 0;

cout << "How many numbers would you like to generate? ";
cin >> SIZE;

int rNumb[SIZE];

srand(time(NULL));

input (rNumb, SIZE);
cout << endl;
cout << "These are the newly generated numbers: ";
output (rNumb, SIZE);
cout << endl;
}

void input (int x[], int &SIZE)
{
	int i = 0;
	
	for(i=0; i<SIZE; i++) 
    {
      x[i] = rand()%100+1;
    }
}

void output (int x[], int &SIZE)
{
	int j = 0;
	
	for (j = 0; j < SIZE; j++)
	{
        cout << x[j] << " ";
	}
	
}
Last edited on
It gives me an error message saying that srand was not declared in the scope.
I've updated it, Try it now.

By the way, what IDE are you using?
Last edited on
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
    Programmer: Galen Casstevens
    UserID: gcasstev
    Section: 001
    Assignment: Program 4
    Purpose: This program will implement pattern matching and sorting techniques. The user will be given the choice to either implement a pattern matching or sorting technique. The program will continue running until the user decides to exit.
*/

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

void input(int [], int &);
void output(int [], int &);

int main()
{
    //This is my personal information that will be displayed to the user.

    cout << "Programmer: Galen Casstevens " << endl;

    cout << "UserID: gcasstev " <<  endl;

    cout << "Section: 001 " << endl;

    cout << "Assignment: Program 4 " << endl;

    cout << "Purpose: This program will implement pattern matching and sorting techniques. The user will be given the choice to either implement a pattern matching or sorting technique. The program will continue running until the user decides to exit. " << endl;

    cout << endl;
    cout << endl;

    char patternsOrSorting;
    char exit = 'n';
    char text[5000];
    int sorting[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    int Size = 0;

    input (sorting, Size);

    cout << endl;

    cout << "These are the newly generated numbers: ";

    output (sorting, Size);

    cout << endl;

    srand(time(NULL));

    while(exit == 'n' || exit == 'N')
    {
        cout << "Would you like to implement a pattern matching technique or a sorting technique (enter 'P' for pattern matching or 'S' for sorting)?: ";
        cin >> patternsOrSorting;

        cout << endl;

        if(patternsOrSorting == 'p' || patternsOrSorting == 'P')
        {
            cout << "Enter text: ";
            cin >> text;

            cout << endl;

        }

        cout << "Would you like to exit the program?: ?";
        cin >> exit;

        cout << endl;
    }

    return 0;
}

void input (int sorting[], int Size)
{
	int i = 0;

	for(i=0; i<Size; i++)
    {
      sorting[i] = rand()%100+1;
    }
}

void output (int sorting[], int Size)
{
	int j = 0;

	for (j = 0; j < Size; j++)
	{
        cout << sorting[j] << " ";
	}

}


I'm using Code Blocks. Ignore some of the code, there are two things that I'm supposed to do with this program. I'm not getting any errors, but my program won't run now. It runs fine with main.cpp or when I try to enter any other code..

Edit: The code runs fine when I remove the parts that you gave to me.. weird
Last edited on
I have it so that the user inputs text, and the text is then stored in a string. I'm not sure what to do from here.
Topic archived. No new replies allowed.