Thread: standard deviation on random numbers?

  1. #1
    Unregistered
    Guest

    standard deviation on random numbers?

    hi? i was trying out this program, could someone please check this program and explain how the output generates the 10 numbers ..are these numbers stored in the compiler memory, and where did the -1.#J come from and what does it mean..

    Also, could you check if the code style is adequate, if not can you suggest any improvements.
    Thanks

    cheers,
    SAAB

    #include <iostream.h>
    #include<stdlib.h>
    #include<time.h>
    #include<stdio.h>//what is this used for
    #include<math.h>
    const int size=10;
    void fillArray(int array[size]);
    void displayArr_element(int array[size]);
    int main()
    {
    double value = 0.0;
    int number = 0;
    double sum = 0.0;
    double sumOfSquares = 0.0;
    double mean = 0.0;
    double standardDeviation = 0.0;
    int array[size];
    int element=0;
    int index=0;
    fillArray(array);
    displayArr_element(array);
    cout << "This program will calculate the mean and standard deviation\n"<< "of a series of values that you enter." << endl;
    if (standardDeviation == -1)
    cout<<" the element"<<element<<" not found"<<endl;
    else
    cout<<" the element is :"<<element<<" was found at:"
    <<"index["<<standardDeviation <<"] of the array."<<endl;
    //CALCULATE MEAN AND STANDARD DEVIATION
    mean = sum / number;
    standardDeviation = sqrt(sumOfSquares / number - mean * mean);
    cout.setf(ios::fixed);
    cout.precision(2);
    cout << "\nThe mean is: " << mean << endl;
    cout << "The standard deviation is: " << standardDeviation <<endl;
    cout<<" the element is :"<<element<<" was found at:"
    <<"index["<<standardDeviation <<"] of the array."<<endl;
    return 0;
    }
    void fillArray(int array[size])
    {
    srand(time(0));
    for(int i=0; i<size; ++i) array[i]=rand()%100;
    }
    void displayArr_element(int array[size])
    {
    cout<<"After sequencial Search the array Element with index is:"<<endl;
    for(int i=0; i<size; ++i)
    cout<<array[i]<<endl;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    It looks like you need two functions, one to calculate the mean, and one to calcuate the standard deviation. The function prototypes could be:

    double find_mean(int array[size])
    double find_standard_deviation(int array[size], double mean)

    The first function would return the mean, the second would return the standard deviation. Then in your main() function, you would call each function to do the calculation:

    Code:
    mean = find_mean(array);
    standardDeviation = find_standard_deviation(array, mean);

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    stdio contains the routines for reading from keyboard and writing to destination for C style input and output. I can't say that it is needed in this program.
    _____________________________________________

    place this line in main before the call to fillArray().

    srand(time(0));
    ________________________________________________

    I don't think that a standard deviation can ever be negative so if a function calculates a standard deviation and it returns negative it means that something went wrong.

    ____________________________________________
    As I remember statistics, standard deviation applies to the group of numbers, not a given number. A given number has a given deviation from the mean but not a standard deviation. Therefore I'm not sure what is being attempted here:

    cout<<" the element is :"<<element<<" was found at:"
    <<"index["<<standardDeviation <<"] of the array."<<endl;
    ________________________________________________-

    from what I can see element is a variable of type int initialized to a value of 0 and that value is never changed in the program. Likewise the variables value, number, sum, index and standardDeviation are initialized but the values are never changed by the program. The process of calculating the standard deviation could be made more explicit by using additonal parentheses.

    _________________________________________________
    In displayArr_element() you pass the array and use a loop to display all elements in the array, but the cout statement implies you only want to display a single element. Which is it-- display a single element or the entire array?

    You can avoid these types of problems by writing out in english what you hope to accomplish. For example: this program will develop 10 random numbers between 0 and 99 inclusive. It will then calculate a mean and standard deviation for the group of numbers. It will also allow the user to search for a given value in the array, and if it is found the deviation from expected will be displayed. This can be repeated indefinately until the user elects to stop. Alternatively the user can request that all values and their deviations from mean can be displayed. Or whatever you are trying to do. Once you have this much, break each indiviidual task into smaller and smaller tasks, using english desriptions. Then try to write pseudocode before the actual code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Generatin Random Numbers
    By dantu1985 in forum C Programming
    Replies: 15
    Last Post: 08-13-2007, 01:21 AM
  3. Random numbers
    By Mavix in forum C Programming
    Replies: 3
    Last Post: 05-13-2007, 09:01 AM
  4. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  5. random numbers
    By ballmonkey in forum C++ Programming
    Replies: 3
    Last Post: 01-18-2005, 02:16 PM