Random amount of array elements with random numbers

How could you make an array with 20 to 30 random elements. Then, I want each element to have a random number between 100 to 200. I have written the random number for each element, but how do you make a random amount of those elements as well? I know I need to use functions and loops. I want something like this:

Making 22 random numbers and storring them in ARRAY1
ARRAY1[ 0 ]= 167
ARRAY1[ 1 ]= 158
ARRAY1[ 2 ]= 188
ARRAY1[ 3 ]= 110
ARRAY1[ 4 ]= 152
ARRAY1[ 5 ]= 187
ARRAY1[ 6 ]= 190
ARRAY1[ 7 ]= 166
ARRAY1[ 8 ]= 184
ARRAY1[ 9 ]= 167
ARRAY1[ 10 ]= 154
ARRAY1[ 11 ]= 184
ARRAY1[ 12 ]= 135
ARRAY1[ 13 ]= 191
ARRAY1[ 14 ]= 180
ARRAY1[ 15 ]= 166
ARRAY1[ 16 ]= 117
ARRAY1[ 17 ]= 185
ARRAY1[ 18 ]= 166
ARRAY1[ 19 ]= 177
ARRAY1[ 20 ]= 135
ARRAY1[ 21 ]= 162


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;




int main()
{
    int iseed = (int)time(NULL);
    int n;

    srand(iseed);
    n = 100 + rand ()% 100; // 100 to 200 random number

    

    return 0;
}
Hi,
Firstly :
1
2
int numArray[50];
int count = 20 + (rand() % 10 + 1);
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
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;




int main()
{
    int iseed = (int)time(NULL);
    int n;
    int numArray[50];
    int count = 20 + (rand() % 10 + 1);


    srand(iseed);
    n = 100 + rand ()% 100; // 100 to 200 random number

    

    return 0;
}


Like so?
Yes.
The rest is simple.
for(int i = 0; i < count; i++) numArray[i] = 100 + (rand() % 100 + 1);

Then you print out the array.
You srand() first before you use rand().

==>
1
2
3
srand(iseed);
int count = 20 + (rand() % 10 + 1);
 
Does that help? :)
I need to use functions though for both.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;




int main()
{
    int iseed = (int)time(NULL);
    int n;

    srand(iseed);
    n = 100 + rand ()% 100; // 100 to 200 random number

    for(int i = 0; i < count; i++) numArray[i] = 100 + (rand() % 100 + 1);
        cout << "Array1" <<[i]= n;


    return 0;
}
Make an ARRAY1 of X random ELEMENTS.

Each array ELEMENT has a different random value N.

X is a random number between 20 to 30. (This means you have from 20 to 30 array Elements).
Must use your randint(min, max) function to return value into X!
N is a random number between 100 to 200. (This means that each Element is a random number between 100 to 200).
Must use your randint(min, max) function to return value into each N!
That's not how you print it.

1
2
3
4
5
6
cout << "Array1" << [i] = n;

cout << "Making " << count << " random numbers and storring them in ARRAY1" << endl;

for(int i = 0; i < count; i++)
cout << "ARRAY1[ " << i << " ] = " << numArray[i] << endl;
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
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;

int randint(int v1, int v2)
{
    return (v1 + (rand() % (v2 - v1) + 1));
}

int main()
{
    int numArray[50];
    int iseed = (int)time(NULL);
    srand(iseed);
    int count = 20 + (rand() % 10 + 1);
    for(int i = 0; i < count; i++) numArray[i] = randint(100, 200);
        
    cout << "Making " << count << " random numbers and storing them in ARRAY1" << endl;

    for(int i = 0; i < count; i++)
    cout << "ARRAY1[ " << i << " ] = " << numArray[i] << endl;
    return 0;
}
(Does that help you?) :)
How would I take that array and make another but reverse it and then double the values? That first one works great though! I love you!
> How would I take that array and make another but reverse it and then double the values?
Do you also need to print out that array too?
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
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;

int randint(int v1, int v2)
{
    return (v1 + (rand() % (v2 - v1) + 1));
}

int main()
{
    int numArray[50];
    int revArray[50]; // reverse array
    int iseed = (int)time(NULL);
    srand(iseed);
    int count = 20 + (rand() % 10 + 1);
    for(int i = 0; i < count; i++) numArray[i] = randint(100, 200);
    for(int i = count - 1, j = 0; i >= 0; i--, j++) revArray[j] = numArray[i] * 2;
        
    cout << "Making " << count << " random numbers and storing them in ARRAY1" << endl;

    for(int i = 0; i < count; i++)
    cout << "ARRAY1[ " << i << " ] = " << numArray[i] << endl;

     cout << endl;
     cout << "Reverse " << count << " random numbers and storing them in ARRAY2" << endl;

    for(int i = 0; i < count; i++)
    cout << "ARRAY2[ " << i << " ] = " << revArray[i] << endl;
    return 0;
}
Last edited on
Does that help you? :)
yes, thank you! :D
Glad it helped :)
Ya, you have helped me sooo much lately LOL
You are welcome :)
Topic archived. No new replies allowed.