0

Hey guys, Now that the previous problem was fixed I was finally able to run my program but now I just don't know what is wrong. Everytime I would enter in a few numbers and let it run, it would print out a series of the same number. Can someone tell me what is wrong this time? I cannot seem to get the right minimum, maximum, range, mean and median of the numbers i entered.

#include "stdafx.h"
#include <iostream>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    //Constant for the array size
    const int SIZE = 100;

    //Declare variables
    int values[SIZE];
    int count = 0;
    int index;
    int number;
    double lowest, highest, range, average, median, even, odd;
    double total = 0;

    //Information for the user
    cout << "For this program, you will be asked to enter values (up to 100 values) that will be calculated." << endl;
    cout << "for the minimum, maximum, range, mean, and median of the values." << endl;

    //Get values from user and have sentinel value for when user would like to quit.
    cout << "Enter a value or -1 to quit." << endl;
    cin >> number;


    //If sentinel is not given continue with input.
    while (number != -1 && count < SIZE )
    {
            values[count] = number;
            count = count + 1;
            //ask for next
            cout << "Enter a value or -1 to quit." << endl;
            cin >> number;
    }

    //Find the minimum value from the set
    lowest = values[0];
    //the for loop
    for (index = 1; index < SIZE; ++index)
    {
            if (values[index] < lowest)
            {
                    lowest = values[index];
            }
    }

    //Find the maximum value from the set
    highest = values[0];
    //the for loop
    for (index = 1; index < SIZE; ++index)
    {
            if (values[index] > highest)
            {
                    highest = values[index];
            }
    }

    //Find range of set value
    range = highest - lowest;

    //Find mean
    for (index = 0; index < SIZE; ++index)
    {
        total = total + values[index];
    }
    average = total / SIZE;

    //find median
    //Put values in ascending order
    for (index = 0; index < SIZE; ++index)
    {
        cout << values[index] << endl;
    }

    if (index % 2 == 0)
    {
        even = index / 2;
        median = ( ( even + 1 ) + even ) / 2;
    }
    else
    {
        odd = ( index / 2 );
        median = odd + 1;
    }

    //Display outputs
    cout << "The value set is: "<< endl;
        for (index = 0; index < SIZE; ++index)
            {
                cout << values[index] << endl;
            }
    cout << "The minimum set is: "<< lowest << endl;
    cout << "The maximum set is: "<< highest << endl;
    cout << "The range set is: "<< range << endl;
    cout << "The mean set is: "<< average << endl;
    cout << "The median set is: "<< median << endl;

    return 0;
}

Edited by pritaeas: Moved to C++

3
Contributors
3
Replies
31
Views
3 Years
Discussion Span
Last Post by whatthebobo
0

On all of your loopps you are running to SIZE. If the user does not enter 100 elemtents then you will be using elements that have not been used. you need to change all of the for loops to run to count and that should fix it.

Edited by NathanOliver

0

Another thing the median returns the index not the value. something like this should work:

//find median
//Put values in ascending order
//Not the best sorting algorithm but useable
for (int i = 1; i < count; i++)
{
    if(values[i] < values[i-1])
    {
        int temp = values[i];
        values[i] = values[i-1];
        values[i-1] = temp;
        i=0;
    }
}
if((count%2)==0)
{
    median = (double)(values[(count/2) - 1] + values[count/2]) / 2;
}
else
{
    median = values[((int)(count/2))];
}

Edited by tinstaafl

0

ok thanks guys I fixed the mistakes everyone told me and now it runs perfectly! thanks so much!

This question has already been answered. Start a new discussion instead.
Be sure to adhere to our posting rules.