0

My program is designed to take 3-30 values from the user and have it run through a handful of functions to compute average, median, and Standard Deviation. However, once I put a set of test case values, my output results in a jumbled mess of what I assume to be an infinite loop.

I am completely lost on what exactly I did wrong. I would greatly appreciate any help.

#include <iostream>
#include <iomanip> // for Setprecision
#include <cmath> // for pow and sqrt

using namespace std;

int main()
{
    // Declare Local Variables
    const int SIZE=30.0;
    double array[SIZE];
    int count = 0;
}
//Module: inputArrayValues
//Description: Ask the user to input values between 3 and 30 elements for the array to hold. Also validates that the user inputs a total of values between 3 and 30.
void inputArrayValues (double array[], int SIZE, int &count)
{
    double number = 0;
    const double SENTINEL = -1.0;
    // Basic information about what the user can input. Does not repeat.
    cout << "Please enter values one at a time." <<endl;
    cout << "Up to a maximum of 30 values and a minimum of 3 values." << endl;
    cout << "Only positive values are accepted or the program will not work." << endl;
    cout << "With the exception, please enter the value -1.0 to stop entering values." << endl;

    // While Loop
    // Variable count is counting how many values the user inputs for later calculation
    for (int i = 0; i < SIZE; i++)
    {
        while (number!= SENTINEL)
        {
            cout << "Please enter a value or enter -1.0 to stop entering values" << endl;
            cin >> number;
            array[i] = number;
            if (number != SENTINEL)
            {
                count++;
            }

        }
    }

    if (count < 3 || count > SIZE)
    {
        cout << "Invalid total number of values." << endl;
        cout << "The total number of values must between 3 and 30 values." <<endl;
        cout << "This program will now close..." << endl;
        cout << "Thank you for using this program." << endl;
    }
}

//Function: comupteAverage
//Description: Computes the average of the given inputs.
double computeAverage (double array[], int count)
{
    double sum = 0.0;
    double resultA;
    for (int i =0; i < count; i++)
    {
        sum = sum + array[i];
    }
    resultA = sum / count;
    return resultA;
}
//Function: computeMedian
//Description: Computes the Median of the given inputs.
double computeMedian (double array[], int count)
{
    double resultM;
    if ((count % 2) == 0)
    {
        resultM = (array[count/2] + (array[count/2] -1.0) /2.0);
    }
    else
        resultM = array[count/2];
    return resultM;
}
//Function: computeSTD
//Description: Computes the Standard Deviation of the given inputs.
double computeSTD (double array[], int count, double average)
{
    double temp;
    double sum = 0;
    double resultV;
    for(int i = 0; i < count; i++)
    {
        temp = pow((array[i] - average), 2);
        sum = sum + temp;
    }
    //Account for Sample Standard Deviation N-1
    resultV = sqrt(sum/(count -1));
    return resultV;
}

Test Case numbers I used.

73.3
83.4
58
11.9
25.1
69.9
45.7
95.0
44.4
-1.0 // To stop entering values

Thank you in advance for your time and advice!

I performed a shorter test case with only 5 values.

Please enter a value or enter -1.0 to stop entering values
4

Please enter a value or enter -1.0 to stop entering values
3

Please enter a value or enter -1.0 to stop entering values
2

Please enter a value or enter -1.0 to stop entering values
-1.0

-1.0 -92559631349317830000000000000000000000000000000000000000000000.0 -92559631
349317830000000000000000000000000000000000000000000000.0 -9255963134931783000000
0000000000000000000000000000000000000000.0 -1.0 -9255963134931783000000000000000
0000000000000000000000000000000.0 The average is: -61706420899545223000000000000
000000000000000000000000000000000.0
The median is: -1.0
The Standard Deviation is: 53439328075621178000000000000000000000000000000000000
000000000.0
Press any key to continue . . .

However, I am still unclear on what's causing the program to behave in this way.

Heavy
  • 1,741
  • 13
  • 23
Chipchip
  • 1
  • 1
  • 5
  • This is not a short code example. Most of your code is unrelated to the problem. See http://sscce.org/ – mrks Nov 10 '14 at 06:32
  • 1
    My apologies! I posted the entire code as I did not have an idea what was causing it. Allow me to narrow it down. Thank you for keeping me in line. – Chipchip Nov 10 '14 at 06:37

1 Answers1

0

First:

for (int i = 0; i < SIZE; i++)
{
    while (number!= SENTINEL)
    {
        cout << "Please enter a value or enter -1.0 to stop entering values" << endl;
        cin >> number;
        array[i] = number;       // <-----
        if (number != SENTINEL)
        {
            count++;
        }

    }
}

you're filling only first element of array, because you do not increment i. The right code should be like this:

count = 0;
do
{
    cout << "Please enter a value or enter -1.0 to stop entering values" << endl;
    cin >> number;
    if ( number != SENTINEL )
    {
        array[ count ] = number;
        count++;
    }
}
while ( number != SENTINEL && count < SIZE );

Second: Where is the code, that calls your functions ???

Third: Your computeAverage and computeSTD are looking good, but what the hell are you doing in computeMedian ???

Update: The right median calculation should be like this:

double computeMedian (double array[], int count)
{
    double resultM;
    double * sorted_array = new double[ count ];
    for ( int i = 0; i < count; i++ )
    {
        sorted_array[ i ] = array[ i ];
    }
    std::sort( sorted_array, sorted_array + count );
    if ((count % 2) == 0)
    {
        //resultM = (sorted_array[count/2] + (sorted_array[count/2] -1.0) /2.0); // <-- compare this line
        resultM = ( sorted_array[count/2] + sorted_array[count/2 - 1] ) / 2.0;  // <-- and this
    }
    else
        resultM = sorted_array[count/2];
    delete []sorted_array;
    return resultM;
}

And the last: I'm strongly recommending you to use std::vector instead of "raw" arrays.

borisbn
  • 4,704
  • 20
  • 38
  • Hello! Thank you so much for your advice. In regards to your questions. I had to shorten the amount of code shown to make it less of an eyesore. (The module calls are working fine) And for computeMedian, I sadly do not know exactly how to do so and the best I could come up with were from seeing examples on the internet. If you can assist me in this section as well I would be grateful. – Chipchip Nov 10 '14 at 08:57
  • Look at Max Shawabkeh's answer on this SO question - http://stackoverflow.com/questions/2114797/compute-median-of-values-stored-in-vector-c – borisbn Nov 10 '14 at 09:03
  • This is the example I was basing my median function on. However, I have not learned size_t to find the exact length of the array. Instead I used a count variable to keep track of the number of times the user inputs a value. I am not sure I can use this method as I was not taught this method in my class. Would there be any simpler way for me to represent this example? Thank you so much for your help. – Chipchip Nov 10 '14 at 09:13
  • Thank you so much for your hard work. Sadly, I am not sure if I can use this example as it contains many aspects, such as std::, that I have not learned in class. Effectively, if I were to use this example to learn how to calculate for median, my professor would most likely mark me off. However, I do greatly appreciate the amount of hard work you put into helping me. – Chipchip Nov 10 '14 at 09:42
  • Ok. Function `std::sort` (as you can guest from it's name))) is sorting given array from the lowest to the largest element. You can write it yourself by reading [this link from wiki](https://en.wikipedia.org/wiki/Quicksort) – borisbn Nov 10 '14 at 09:51
  • Ah! I understand now. Unfortunately, I must wait until tomorrow to learn from your example as I must sleep. However, I will try to post my result here tomorrow. Thank you so much for your advice. Please have a fantastic day or night! – Chipchip Nov 10 '14 at 10:11