### 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
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081`` ``````#include #include #include 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. "<> 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

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576`` ``````#include #include #include 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

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576`` ``````#include #include #include 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(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
 ``1234`` `````` for (int i=0;i<6;i++) cout << setw(10) << rand() << " "; cout << endl; myarray[6] = my_seed; //¿what do you think this line does? ``````

 ``123`` ``````#ifdef WIN32 system("pause"); #endif ``````
thanks for the laugh.
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

 ``123456789101112131415`` `````` int myarray[6]; unsigned int my_seed = time(0); //srand(static_cast(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:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283`` ``````#include #include #include 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 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.