# 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?

``````#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)
{
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;

}
``````
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
>>     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.
Commented:
>> 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.
Author Commented:
Does the sequence of the parts of the program need to be changed to accomplish dropping of the lowest & highest scores?
Thanks
\Commented:
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.

\Commented:
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/
\Commented:
If you change the array to a vector, and use the min_element, max_element functions, then you can use vector::erase to delete the entry from the vector list.
http://www.cplusplus.com/reference/stl/vector/erase/
Commented:
>> 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.
Author Commented:
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
Commented:
>> In above new snippet, does index = 0 need to be included.

What do you think ?

Do you need the 'index' variable ? If so, you need to define it.
If you don't need the 'index' variable, then you don't define it.

Experts Exchange Solution brought to you by