Ghost_Programmer
Ghost_Programmer

Reputation: 45

Object members not receiving values in C++

#include <algorithm>
#include <ctime>
#include <iostream>
#include <string>
using namespace std;

class stats
{
    int arr_size, median1, median2, occurrences;
    string data;
    int* arr;

    void setValues()
    {
        srand(time(NULL));
        for (int i = 0; i < arr_size; i++) { arr[i] = rand() % 99 + 1; }

        sortArray();
    }
    void sortArray()
    {
        int min_index;
        for (int i = 0; i < arr_size - 1; i++)
        {
            min_index = i;
            for (int j = i + 1; j < arr_size; j++) { if (arr[j] < arr[i]) { min_index = j; } }
            swap(this->arr[i], this->arr[min_index]);
        }
    }
    void swap(int& x, int& y)
    {
        int temp;
        temp = x;
        x = y;
        y = temp;
    }

    double getMean()
    {
        double total = 0;
        for (int i = 0; i < arr_size; i++) { total += arr[i]; }
        return total / arr_size;
    }
    int getMedian()
    {
        if (arr_size % 2 == 0) { median1 = arr[(arr_size / 2) - 1]; this->median2 = arr[arr_size / 2]; }
        else { median1 = arr[arr_size / 2]; }
        return median1;
    }
    int getMode()
    {
        int count = 1, max_count = 1, mode;
        mode = arr[0];

        for (int i = 0; i < arr_size; i++)
        {
            if (arr[i - 1] == arr[i]) { count += 1; mode = arr[i]; }
            else if (arr[i - 1] != arr[i])
            {
                if (count > max_count) { max_count = count; mode = arr[i - 1]; }
                count = 1;
            }
        }
        this->occurrences = max_count;
        return mode;
    }
public:
    stats(string str = "", int size = 1) : data{ str }, arr_size{ size } { arr = new int[size]; setValues(); }
    ~stats() { delete arr; arr = nullptr; }

    void printValues()
    {
        cout << this->data << endl << endl;
        cout << "Mean: " << getMean() << endl;

        this->median1 = getMedian();
        if (arr_size % 2 == 0) { cout << "Median 1: " << this->median1 << endl << "Median 2: " << this->median2 << endl; }
        else { cout << "Median: " << this->median1 << endl; }

        int mode = getMode();
        cout << "Mode: " << mode << " with " << this->occurrences << " occurrences." << endl;

        // Testing purposes
        cout << endl;
        for (int i = 0; i < arr_size; i++) { cout << "arr[" << i << "]: " << arr[i] << endl; }
    }
};

So this is my code, and basically all I have to do is get the mean, median(s), and mode, along with its occurrences. It wasn't hard to build at all, but if you run it, median2 andoccurrneces don't receive the values. Why is this?

Upvotes: 0

Views: 67

Answers (2)

molbdnilo
molbdnilo

Reputation: 66371

This happens because the order of evaluation of parameters is unspecified, but your code is relying on them to be evaluated left-to-right.

// getMode() and this->occurrences may be evaluated in any order...
cout << "Mode: " << getMode() << " with " << this->occurrences << " occurrences." << endl;

You could store the results of your functions in variables:

int median1 = getMedian();
if (arr_size % 2 == 0) { cout << "Median 1: " << median1 << endl << "Median 2: " << this->median2 << endl; }
else { cout << "Median: " << median1 << endl; }

int mode = getMode();
cout << "Mode: " << mode << " with " << this->occurrences << " occurrences." << endl;

but the better solution is to restructure your code so it doesn't rely on the ordering of side effects.

Upvotes: 1

emvee
emvee

Reputation: 4449

median2 only gets a value if the size of the array is even (re-layed out for clarity):

if (arr_size % 2 == 0) {
    median1 = arr[(arr_size / 2) - 1];
    this->median2 = arr[arr_size / 2];
}
else { 
    median1 = arr[arr_size / 2];
}
return median1;

Also you should read up on statistics; in order to get any median of a distribution the samples need to be sorted, which you don't seem to do.

And this is also the reason why occurrences doesn't get the right value (it does get a value, only not the one you expect).

Upvotes: 2

Related Questions