Link to home
Start Free TrialLog in
Avatar of Jammy0339
Jammy0339

asked on

C++ Array with functions

I am having trouble making the entry of diving scores accept decimals. How do I code the ability to remove the lowest & highest scores before I compute the averages?
Thanks in advance

#include <iostream>
#include <cmath>

using namespace std;

    const int NUM_JUDGES = 7; // number of judges
    const double SCORE_FACTOR = .6; // scoring factor from book
    const double DEGREE_DIFFICULTY = 1.6; // selected degree of difficulty

void fillArray(int a[], int size, int& numberUsed);

double computeAverage(const int a[], int numberUsed);

void sort (int a[], int numberUsed);

void swapValues( int& v1, int& v2);

int indexOfSmallest(const int a[], int startIndex, int numberUsed);

int main()
{
    int scoreArray[NUM_JUDGES], numberUsed, max;
    double totalScore;

    cout << "This program reads diving scores and shows \n"
         << "the diver's total score.\n";
         cout << "Enter the diving scores: \n"
              << "Each score must be between 0 and 10.\n";
          // cin  >> scoreArray[0];
           //max = scoreArray[10];//max is thelargest value of an individual score

       fillArray(scoreArray, NUM_JUDGES, numberUsed);
       sort(scoreArray, numberUsed);
       cout << "In sorted order the numbers are: \n";


       for(int index = 0; index < numberUsed; index++)
            cout << scoreArray[index] << "";
        cout << endl;

        return 0;


}

void fillArray(int a[], int size, int& numberUsed)
{
    cout << "Enter up to " << size << " nonnegative scores.\n"
         << "Mark the end of the list with a negative number.\n";
    int next, index = 0;
    cin >> next;
    while ((next >=0) && (index < size))
    {
        a[index] = next;
        index++;
        cin >> next;
    }
    numberUsed = index;

}
double computeAverage(const int a[], int numberUsed)
{
    // check for bad input
    if(numberUsed < 1)
    {
    cout << "Error: number is 0 in the computeAverage. \n"
            << "computerAverage return0. \n";
    return 0;
    }
    //valid input received OK to calculate
    double total = 0;
    for (int index = 0; index < numberUsed; index++)
    {
        total = total + a[index];
    }
    return (total/numberUsed);

 }

 void sort(int a[], int numberUsed)
{
    int indexOfNextSmallest;
    double average = computeAverage(a, numberUsed);
    cout << "Average of the " << numberUsed
         << " scores = " << average << endl
         << "Final diver's score is: " << average *  SCORE_FACTOR * DEGREE_DIFFICULTY << endl;
    for(int index = 0; index < numberUsed -1; index++)
    {
        indexOfNextSmallest = indexOfSmallest(a, index, numberUsed);
        swapValues(a[index], a[indexOfNextSmallest]);

    }
}
 void swapValues (int& v1, int& v2)
 {
     int temp;
     temp = v1;
     v1 = v2;
     v2 = temp;
 }
int indexOfSmallest (const int a[], int startIndex, int numberUsed)
{
    int min = a[startIndex], indexOfMin = startIndex;
       for (int index = startIndex+ 1; index < numberUsed; index++)
        if (a[index] < min)
        {
            min = a[index];
            indexOfMin = index;

        }

    return indexOfMin;

}

Open in new window

Avatar of Infinity08
Infinity08
Flag of Belgium image

>>     int next, index = 0;
>>     cin >> next;

Instead of taking an int as input, take a double as input :

        double next = 0.0;
        cin >> next;

and make sure to adapt the rest of the code accordingly.
>> How do I code the ability to remove the lowest & highest scores before I compute the averages?

After you sort the values given by the user, you can simply ignore the first and the last. Or you can actually remove them from the array.
Avatar of Jammy0339
Jammy0339

ASKER

Does the sequence of the parts of the program need to be changed to accomplish dropping of the lowest & highest scores?
Thanks
I took just a high level overview of your OP. For small arrays, then performance is not an issue. For large arrays, then to get an average after removing the lowest and highest score, then there really isn't any need to sort (which is an expensive operation). Just find the lowest and highest elements by scanning the array one time (this can be done efficiently in a single function); and then remove them from the list (this can be done either in an array shuffle or just keep track of the min and max indexes).

After removing the min and max elements, then just do a straight average on what is left. Or, if just keeping track of the min/max indexes, then as you create the sum, just skip over the min and max index.

For the future, in case you are not forced to write all the detailed code at this time, you can use C++ standard library algorithms to simplify the coding. See, for example:

min
   http://www.cplusplus.com/reference/algorithm/min/

max
   http://www.cplusplus.com/reference/algorithm/max/

or,

min_element
    http://www.cplusplus.com/reference/algorithm/min_element/

max_element
    http://www.cplusplus.com/reference/algorithm/max_element/

FYI - there is a remove function, but it removes all the matching elements in the range; so be careful:
   http://www.cplusplus.com/reference/algorithm/remove/
SOLUTION
Avatar of phoffric
phoffric

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> Does the sequence of the parts of the program need to be changed to accomplish dropping of the lowest & highest scores?

No. Right now, you are reading the input, sorting it, and then displaying it. What you want to do, fits nicely after the sort step.
Infinity08:
=======================================================================
>>     int next, index = 0;
>>     cin >> next;
Instead of taking an int as input, take a double as input :
        double next = 0.0;
        cin >> next;
and make sure to adapt the rest of the code accordingly.
===========================================================================
In above new snippet, does index = 0 need to be included. I am having making the changes.
Thanks
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial