hi guys, i need help with this exercice

closed account (375jz8AR)

2. Write a program which will make some operations on a set of randomly generated numbers. First write a function generating n random numbers of type double from the range <0,1> and stores them inside an array. The array should be declared local inside main function. The size of the array should be defined by preprocessor command #define. The array and its actual size should be passed to the function as arguments. Write a function which will print all the values from the array.

3. Extend the program from previous point by adding two functions: first should calculate the average value of the array of the generated numbers and second should calculate standard variation.
average: ;
standard variation: .

4. Add a function which finds maximum from the array of random numbers and returns also the index of the maximum element.

5. Add a function reversing order of the elements inside the array. Do not use additional array during reversing operation.


*This is the exercice i must complete someone can help me? i have problems with create the function of n random numbers between <0,1> and store them into an array, thank you in advance
Please check the code below, randDouble() which generates random number given the range. In your case, you can call randDouble(0, 1).

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

double randDouble(double low, double high)
{
    double temp;

    /* swap low & high around if the user makes no sense */
    if (low > high)
    {
    temp = low;
    low = high;
    high = temp;
    }

    /* calculate the random number & return it */
    temp = (rand() / (static_cast<double>(RAND_MAX) + 1.0))
    * (high - low) + low;
    return temp;
}

int main(){
    std::srand( std::time(0) );
    std::cout << randDouble(0, 1);
    return 0;
}


I added main() function to emphasize that you need to call say for example std::srand(std::time(0));

to set seed (by calling this, you can avoid generating the same random number in every call to randDouble() ). You can call this srand function once which is for example at the start of your program.
Last edited on
Topic archived. No new replies allowed.