A Little Help

This is a discussion on A Little Help within the C++ Programming forums, part of the General Programming Boards category; Hey Everybody I am a student at a University in St. Louis and was woundering if someone coudl take a ...

  1. #1
    Dante42
    Guest

    Question A Little Help

    Hey Everybody I am a student at a University in St. Louis and was woundering if someone coudl take a look at this program and tell me what is wrong. The program ask you to enter a number which becomes the size of the array. You then enter that many numbers. The program sorts and averages the numbers and is suppossed to find the median number. It all works except the median function. I have pasted a copy of my program if someone has some advise would really appreciate it. Thanks
    Chris

    #include<iostream.h>
    #include<iomanip.h>

    float sortarray(float array[], int elems); //Decleares sort function
    float median(float *a, int count); //Declares Median Function
    float average(float[], int scores); //Decleares average function

    void main(void)
    {
    float *test; //Holds amount of each test scores (array)
    int scores; //Holds number of test scores

    cout << "How many test scores do you wish to enter: " << endl;
    cin >> scores; //Enters test scores
    test = new float[scores];
    if (test == NULL) //Tests for memeorey allocation
    {
    cout << "Error allocating memory!\n";
    return;
    }
    else
    {
    cout << "Memory Allocated!" << endl;
    cout << "The beginning address is: " << &test << endl; //Shows beginning address of test
    }

    cout << "Enter the test scores below.\n";
    for (int count = 0; count < scores; count++)
    {
    cout << "Test " << (count +1) << ": ";
    cin >> test[count];
    }
    cout.precision(2);
    cout.setf(ios::fixed | ios::showpoint);
    cout << "This Program sorts the scores and drops the lowest!" <<endl;
    cout << "Here are your sorted test scores in numerical order: " << endl;
    sortarray(test, scores); //Calls function to sort test scores
    cout << endl;
    cout << "This Program also finds the median score using all scores entered!" <<endl;
    cout << "The Median number is: " << median(test, scores) << endl;
    cout << "Average Test Scores: " << average(test, scores) << endl;
    //Calls function to average all test scores
    delete [] test;
    }

    float sortarray(float array[], int elems) //Sorts arrays
    {
    int swap;
    float temp;
    do
    {
    swap = 0;
    for (int count = 0; count < (elems - 1); count++)
    {
    if (array[count] > array[count +1])
    {
    temp = array[count];

    array[count] = array[count +1];
    array[count + 1] = temp;
    swap = 1;
    }
    }
    }while (swap != 0);

    for (int count = 1; count < elems; count++) //Drops Lowest Score
    cout << array[count] << " ";
    return array[count]; //Returns sorted array.
    }

    float average(float test[], int scores) //Calculates average
    {
    float total = 0;
    for (int count = 1; count < scores; count++)
    {
    total += test[count];
    }

    float a = total / (count - 1);
    return a; //Returns average with a value of 'a'
    }

    float median(float a[], int count) //Finds Median Value
    {
    int index;
    if (count == 0)
    {
    index = count / 2;
    return index; //Returns Median Value of odd number arrays
    }
    else
    index = count % 2;
    return ((a[count - 1] + a[count])/2); //Returns Median Value of even number arrays
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    I just compiled it and got:

    warning C4244: 'return' : conversion from 'int' to 'float', possible loss of data

    That is the return from the median function. You divide into an int then return it. But the function says it should be returning a float.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    You're also dividing zero by 2 when index equals 0 (index = count/2). I don't think that's correct.

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    I don't know what you'd like to do, but your median functions
    seems to be totally off. I can see at least one fatal error and
    your comments do not match your code. I'm sure
    that we can fix it together, if you provide a plain english
    description, what exactly your median function should return.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21