seeding rand and arrays help!

Hey guys I really need some help. I am suppose to generate the array of numbers using rand( ), instead of prompting the user for the values. Add a function to calculate the average value of the array. Finally, add code to seed the RNG (random number generator) from the system clock, and run you program several times to verify that you get a different sequence each time

Here is my code without rand
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
#include<iostream>
#include<iomanip>
#include <ctime>

using namespace std;

int min_element(int array[], int size);

int max_element(int array[], int size);

int average_element(int array[], int size);


int main(void)
{
    int x = 0;
    int myarray[100];
    int val = 0;
    int mymin, mymax;

    cout << "Enter the numbers of elements in the array: " << endl;
    cin >> x;

    if (x >= 100)
    {
        cout << "sorry.  too large.  100 max. "<<endl;
        return 1;
    }

    for(int i = 0;i < x;i++)
    {
        cout << "Enter the element No. " << i + 1  << "of the array ";
        cin >> val;
        myarray[i] = val;
    }

    mymin = min_element(myarray, x);
    mymax = max_element(myarray, x);
    cout << "The minimum element of the array is " << mymin << endl;

    cout << "The maximum element of the array is " << mymax << endl;

#ifdef WIN32
    system("pause");
#endif
    return 0;
}

int min_element(int array[], int size)
{
	// returns the minimum value of array
    int i=0;
    int minimum = array[0];
    for (i = 1; i < size; ++i) {
        if (minimum > array[i]) {
            minimum = array[i];
        }
    }
    return minimum;
}

int max_element(int array[], int size)
{
	int i=0;
	int maximum = array[0];
    for (int i = 1; i < size; ++i) {
        if (maximum < array[i]) {
            maximum = array[i];
        }
    }
    return maximum;
}

int average_element(int array[], int size)
{
	int sum = array[0];
    for (int i = 1; i < size; ++i) {
        sum += array[i];
    }
    return sum/size;
}


And here it is when a try to seed the random number. But the min and max elements always end up as -858993460 no matter what. Any help is greatly appreciated. Here is my code with me trying to rand

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

using namespace std;

int min_element(int array[], int size);

int max_element(int array[], int size);

int average_element(int array[], int size);


int main(void)
{
    int x = 0;
    int myarray[6];
    int val = 0;
    int mymin, mymax;

    int n = 0;
	unsigned int my_seed = time(0);     // get a seed from the system clock
    srand(my_seed);
    
    for (int i=0;i<6;i++)
        cout << setw(10) << rand() << " ";
    cout << endl;
    myarray[6] = my_seed;
    


    mymin = min_element(myarray, x);
    mymax = max_element(myarray, x);
    cout << "The minimum element of the array is " << mymin << endl;

    cout << "The maximum element of the array is " << mymax << endl;

#ifdef WIN32
    system("pause");
#endif
    return 0;
}

int min_element(int array[], int size)
{
	// returns the minimum value of array
    int i=0;
    int minimum = array[0];
    for (i = 1; i < size; ++i) {
        if (minimum > array[i]) {
            minimum = array[i];
        }
    }
    return minimum;
}

int max_element(int array[], int size)
{
	int i=0;
	int maximum = array[0];
    for (int i = 1; i < size; ++i) {
        if (maximum < array[i]) {
            maximum = array[i];
        }
    }
    return maximum;
}

int average_element(int array[], int size)
{
	int sum = array[0];
    for (int i = 1; i < size; ++i) {
        sum += array[i];
    }
    return sum/size;
}
Last edited on
srand(time(0));
I switched srand(my_seed); to srand(time(0)); but it still doesn't work
I prefer

srand(static_cast<unsigned int>(time(0)));

but either one will work.
I switched it but still doesn't work

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

using namespace std;

int min_element(int array[], int size);

int max_element(int array[], int size);

int average_element(int array[], int size);


int main(void)
{
    int x = 0;
    int myarray[6];
    int val = 0;
    int mymin, mymax;

    int n = 0;
	unsigned int my_seed = time(0);     // get a seed from the system clock
    srand(static_cast<unsigned int>(time(0)));
    
    for (int i=0;i<6;i++)
        cout << setw(10) << rand() << " ";
    cout << endl;
    myarray[6] = my_seed;
    


    mymin = min_element(myarray, x);
    mymax = max_element(myarray, x);
    cout << "The minimum element of the array is " << mymin << endl;

    cout << "The maximum element of the array is " << mymax << endl;

#ifdef WIN32
    system("pause");
#endif
    return 0;
}

int min_element(int array[], int size)
{
	// returns the minimum value of array
    int i=0;
    int minimum = array[0];
    for (i = 1; i < size; ++i) {
        if (minimum > array[i]) {
            minimum = array[i];
        }
    }
    return minimum;
}

int max_element(int array[], int size)
{
	int i=0;
	int maximum = array[0];
    for (int i = 1; i < size; ++i) {
        if (maximum < array[i]) {
            maximum = array[i];
        }
    }
    return maximum;
}

int average_element(int array[], int size)
{
	int sum = array[0];
    for (int i = 1; i < size; ++i) {
        sum += array[i];
    }
    return sum/size;
}
Last edited on
1
2
3
4
    for (int i=0;i<6;i++)
        cout << setw(10) << rand() << " ";
    cout << endl;
    myarray[6] = my_seed; //┬┐what do you think this line does? 



1
2
3
#ifdef WIN32
    system("pause");
#endif 
thanks for the laugh.
closed account (z05DSL3A)
Totes my goat,

Your problem is nothing to do with srand(), you are not assigning any numbers to the array (other than a single my_seed).
Last edited on
So i do have the right code right? i just need to assign the numbers to the array somehow?
At no point do you ever assign values to the myarray[] elements.
Also, line 28 is out-of-bounds.
Last edited on
> i just need to assign the numbers to the array somehow?

Not somehow; assign the random numbers that you generate to the elements of the array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    int myarray[6];
    unsigned int my_seed = time(0);
    //srand(static_cast<unsigned int>(time(0)));
    srand(my_seed) ;

    for (int i=0; i<6; i++)
    {
        int r = rand() ; // random number
        // cout << setw(10) << rand() << " "; 
        cout << setw(10) << r << " "; // print it
        myarray[i] = r ; // assign to element at position i; i in [0,5] 
    }
    cout << endl;

    // myarray[6] = my_seed; // **** error; out of bounds 
Thanks JLBorges I got it to work. Your my fuckin nigga man!
Heres the code in case anyone else is going to need 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
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
#include<iostream>
#include<iomanip>
#include <ctime>

using namespace std;

int min_element(int array[], int size);

int max_element(int array[], int size);

int average_element(int array[], int size);


int main(void)
{
    int x = 0;
	int myarray[100];
    int val = 0;
    int mymin, mymax, myavg;

	cout << "Enter the numbers of elements in the array: " << endl;
    cin >> x;
	
    unsigned int my_seed = time(0);
    
    srand(my_seed) ;

    for (int i=0; i<x; i++)
    {
        int r = rand() ; 
        cout << setw(10) << r << " "; 
        myarray[i] = r ; 
    }
    cout << endl;
    
    mymin = min_element(myarray, x);
    mymax = max_element(myarray, x);
	myavg = average_element(myarray, x);
    cout << "The minimum element of the array is " << mymin << endl;

    cout << "The maximum element of the array is " << mymax << endl;

	cout << "The average value of the array is " << myavg << endl;

#ifdef WIN32
    system("pause");
#endif
    return 0;
}

int min_element(int array[], int size)
{
	// returns the minimum value of array
    int i=0;
    int minimum = array[0];
    for (i = 1; i < size; ++i) {
        if (minimum > array[i]) {
            minimum = array[i];
        }
    }
    return minimum;
}

int max_element(int array[], int size)
{
	int i=0;
	int maximum = array[0];
    for (int i = 1; i < size; ++i) {
        if (maximum < array[i]) {
            maximum = array[i];
        }
    }
    return maximum;
}

int average_element(int array[], int size)
{
	int sum = array[0];
    for (int i = 1; i < size; ++i) {
        sum += array[i];
    }
    return sum/size;
}
Topic archived. No new replies allowed.