T K
T K

Reputation: 27

Calculating with randomly generated numbers in C++

So I'm supposed to be generating one hundred random numbers between 1-100. 20 of them are supposed to be generated within the range of 1-100, and the other 80 are supposed to be within the range of 40-70. I'm also supposed to create functions to calculate the average and standard deviations of the numbers.

Keep in mind this is supposed to be done without the use of arrays.

My issue is that I'm not sure how to get my program to remember the numbers being generated so that I can use them for the calculation function. All I can really do right now is get them all printed out through the use of loops. I don't know how to make the generated numbers usable values for calculation.

Any tips, and am I approaching this the right way?


#include <iostream>
#include <cstdlib>
#include <cmath>
#include <time.h>
using namespace std;


int getOutlierScore();
int getNormalScore();


int main()
{
    srand (time(NULL));
    cout << "The scores are as follows:" <<endl;

    for (int i = 1; i <= 20; i++)
    {
        cout << i << ". " << getOutlierScore() <<endl;
    }

    for (int i = 21; i <= 100; i++)
    {
        cout << i << ". " << getNormalScore() <<endl;
    }

    return 0;
}

int getOutlierScore()
{
    int outlier;
    outlier = rand() % 100 + 1;
    return outlier;
}

int getNormalScore()
{
    int max = 70, min = 40;
    int normalScore;
    normalScore = rand() % (max - min) + min;
    return normalScore;
}

Upvotes: 1

Views: 542

Answers (3)

muXXmit2X
muXXmit2X

Reputation: 2765

Well I totally forgot that you can actually calculate the standard deviation without actually storing the numbers. However if you want to store them for e.g prompting them you can use a linked list. This isn't an array and therefore should be allowed.

A simple linked list (there are more elegant solutions out there but this one is fairly simple) could look like this:

struct Node
{
    int val;
    Node* next;

    Node(int val)
    {
        this->val = val;
        next = NULL;
    }

    void add(int val)
    {
        Node* cur = this;
        while (cur->next)
        {
            cur = cur->next;
        }
        cur->next = new Node(val);
    }

    void deleteFromHere() // delete all Nodes after this one
    {
        while (next)
        {
            Node *cur = this;
            Node * last;
            while (cur->next)
            {
                last = cur;
                cur = cur->next;
            }
            delete last->next;
            last->next = NULL;
        }
    }
};

And use it like so:

int main(void)
{
    Node head(getOutlierScore());

    for (int i = 0; i < 19; i++)
    {
        head.add(getOutlierScore());
    }

    for (int i = 0; i < 80; i++)
    {
        head.add(getNormalScore());
    }

    Node* cur = &head;
    while (cur->next)
    {
        cout << cur->val << ". ";
        cur = cur->next;
    }
    cout << endl;

    // calculate standard deviation and average here

    head.deleteFromHere();
}

By the way C++11 came with the <random> header, which is prefered over std::rand. It's usage is pretty simple. Have a look at this:

int GetRandom(int min, int max)
{
    static random_device rd; // create random device only once per program run
    mt19937 mt(rd()); // create number generator

    uniform_int_distribution<int> dist(min, max); // define range for the values [min;max]
    return dist(mt); // create a random value in range
}

Upvotes: 0

Kishore Bandi
Kishore Bandi

Reputation: 5701

Well, I don't want to post the answer to your home work. I'm just guiding you on how to achieve this.

You're doing good so far. Firstly, To calculate standard deviation you need to find mean first. That means you're going to have to calculate average.

Now, You don't need an array to calculate the average for a set of numbers. Keep track of the total sum of these random values in a variable say sum.

You'll have to adds all the values from functions getOutlierScore() and getNormalScore() into this variable.

So at the end of loop you'll get the total sum with the provided conditions of "20 of them are supposed to be generated within the range of 1-100, and the other 80 are supposed to be within the range of 40-70."

Now all you've to do is find average and then Standard deviation. Let me know if you're stuck. (Practicing will help you learn, Good luck!!)

Edit: As @Doug pointed out, you need sum of squares as well. So keep track of that in another Variable.

Upvotes: 1

doug
doug

Reputation: 4289

class Statistics {
private:
    int count;
    double sum;
    double sum2;
public:
    Statistics():count(0), sum(0),sum2(0){}
    void clk(float f);          // clock in data value
    float ave();                // get mean
    float std();                // get standard deviation
};

inline void Statistics::clk(float f)
{
    count++;
    sum += f;
    sum2 += f*f;
}

inline float Statistics::ave()
{
    return float(sum / count);
}

inline float Statistics::std()
{
    return (float)sqrt((double)((sum2 - sum*(sum / count)) / (count - 1)));
}

This should give you a start. You don't need to store anything. Just clock in the 100 random values using a Statisics object with clk(r). After you are done you can call the ave() and std() to get the results.

Upvotes: 0

Related Questions