# Thread: arrays.. dropping the lowest and highest then compute its average

1. ## arrays.. dropping the lowest and highest then compute its average

You were at the 2008 Olympics. You were watching Gymnastics at the National Indoor Stadium in Beijing. There were ten judges in the Stadium. Another ten judges were at a remote venue. The judges give the gymnasts a score from 1 - 10 (they can give scores such as 9.3, 8.5 as well).

You were not satisfied with the displaying of the scores of the gymnasts in the stadium because no average is displayed. Since you brought with you your Apple, you decided to make your own program for the average score.Store the scores of the judges in Stadium in one array. The judges' score from the remote venue should be stored in a separate array.

Create a third array. Copy the judges' (the 20 judges) scores in this third array. Compute the average of the scores given by the judges. In computing for the average, the minimum and the maximum scores should not be included. Display this average. Also display the judges who gave the gymnast the lowest and highest scores.
-----------we are not yet allowed to used the functions..pls..!!!!,, help me to finish this code.. i don't know what will i do next..i don't know how to drop the lowest and highest score then compute its average.. pls.. kindly check at my codes..

--i'm just new in programming language.

----►here is my codes:
Code:
```#include <iostream.h>
void main()

{
float arr[10], sum=0,arr2[10], sum2=0;

cout<<"Beijing, Gymnast Competition Olympics year of 2008."<<endl;

cout<<"scores of the judges in the stadium:"<<endl;

for(int i=0; i<10; i++){
cin>>arr[i];

for(i=0; i<10; i++)
sum = arr[0] + arr[1] + arr[2] + arr[3] + arr[4] +arr[5] + arr[6] + arr[7] + arr[8] + arr[9];

float ave = (float)sum/10;

cout<<"The average of the judges in the stadium is "<<ave<<"."<<endl;

}

cout<<"scores of the judges in the remote venue:"<<endl;

for(int g=0; g<10; g++){
cin>>arr2[g];

for(g=0; g<10; g++)
sum2 = arr2[0] + arr2[1] + arr2[2] + arr2[3] + arr2[4] +arr2[5] + arr2[6] + arr2[7] + arr2[8] + arr2[9];

float sum_of_ave = (float)sum2/10;

cout<<"The average of the judges in the remote venue is "<<sum_of_ave<<"."<<endl;
}

cout<<"scores from judges in the stadium and in the remote venue in the gymnastics:"<<endl;

float arr3[20], sum3=0 ,low=0, high=0;

arr3[20]=arr[0] + arr[1] + arr[2] + arr[3] + arr[4] +arr[5] + arr[6] + arr[7] + arr[8] + arr[9]+arr2[0] + arr2[1] + arr2[2] + arr2[3] + arr2[4] +arr2[5] + arr2[6] + arr2[7] + arr2[8] + arr2[9];

for(int k=0; k<20; k++)
cin>>arr3[k];

{

for(int k=0; k<20||k>20; k++)
if (arr3[k]<low)
low=arr3[k];

if (arr3[k]>high)
high= arr3[k];
}

float	total=(float)sum3/18;

cout<<"The average of the gymnast is"<<total<<endl;
cout<<"The lowest score is"<<low<<endl;
cout<<"The highest score is"<<high<<endl;
}```

2. Guessing from "#include <iostream.h>", this is supposed to be a C++ program, not a C program, so it has been moved to the C++ programming forum.

Now, <iostream.h> is pre-standard; you should use <iostream> instead. You should also indent your code reasonably and consistently and post it within [code][/code] bbcode tags.

3. What the heck is going on?

Check your curly braces, making sure that they are placed in logical places.

Also, make sure that your looping variables are distinct. If you are using one int in a for loop, do not change its value within the looped code unless you are doing something clever. There are 26 letters in the English alphabet, and many others besides.
Pretend you were a computer with no knowledge of the task at hand. What does this line do?
Code:
`arr3[20]=arr[0] + arr[1] + arr[2] + arr[3] + arr[4] +arr[5] + arr[6] + arr[7] + arr[8] + arr[9]+arr2[0] + arr2[1] + arr2[2] + arr2[3] + arr2[4] +arr2[5] + arr2[6] + arr2[7] + arr2[8] + arr2[9];`

4. How do you exclude the min and max from the average?
Well, notice that the prompt wants you to say which judges gave min and max scores. This tells you where those scores are in the arrays. So when you're lopping through the array, check each time to ensure that the value you're looking at is not one of the extrema. If it is, skip it. This is essentially against the board rules, but I'm procrastinating so I wrote an example for you.

Please note how much explicit, redundant code there is. Have you learned about functions? Have you learned about pointers? These two things would shrink this code maybe three or four times, and make it more readable. But this gets a crude method across.

Code:
```
#include <cassert>  //for a "sanity check"
#include <iostream> //standard C++ input/output

//main() is an int!
int main()
{
using namespace std; //so we don't need 'std::' before everything
const unsigned njudges = 10; //save ourselves rewiting code when one of the judges dies.
const float score_limit = 10.0;
float local_scores[njudges];
float remote_scores[njudges];

for(unsigned i = 0; i < njudges; ++i)
{
cout << "Local judge number " << (i+1) << "'s score: ";
cin >> local_scores[i];
}
for(unsigned i = 0; i < njudges; ++i)
{
cout << "Remote judge number " << (i+1) << "'s score: ";
cin >> remote_scores[i];
}

float local_sum = 0;
float remote_sum = 0;
for(unsigned i = 0; i < njudges; ++i)
{
local_sum += local_scores[i];
remote_sum += remote_scores[i];
}
cout << "Average score given by the local judges: " << (local_sum / njudges) << '\n'
<< "Average score given by the remote judges: " << (remote_sum / njudges) << endl;

float local_max = 0;
unsigned local_max_index;
for(unsigned i = 0; i < njudges; ++i)
{
if(local_scores[i] > local_max)
{
local_max = local_scores[i];
local_max_index = i;
}
}
cout << "Local judge number " << (local_max_index+1) << " gave the high score of " << local_max << " points." << endl;

float remote_max = 0;
unsigned remote_max_index;
for(unsigned i = 0; i < njudges; ++i)
{
if(remote_scores[i] > remote_max)
{
remote_max = remote_scores[i];
remote_max_index = i;
}
}
cout << "Remote judge number " << (remote_max_index+1) << " gave the high score of " << remote_max << " points." << endl;

float local_min = score_limit;
unsigned local_min_index;
for(unsigned i = 0; i < njudges; ++i)
{
if(local_scores[i] < local_min)
{
local_min = local_scores[i];
local_min_index = i;
}
}
cout << "Local judge number " << (local_min_index+1) << " gave the low score of " << local_min << " points." << endl;

float remote_min = score_limit;
unsigned remote_min_index;
for(unsigned i = 0; i < njudges; ++i)
{
if(remote_scores[i] < remote_min)
{
remote_min = remote_scores[i];
remote_min_index = i;
}
}
cout << "Remote judge number " << (remote_min_index+1) << " gave the low score of " << remote_min << " points." << endl;

const unsigned nscores = 2*(njudges-2);
float all_scores[nscores];

unsigned all = 0;
unsigned local = 0;
for(/*see above*/; local < njudges; ++all, ++local)
{
if(local != local_max_index && local != local_min_index)
all_scores[all] = local_scores[local];
else
--all; //if we DO skip this judge, then we make sure we don't leave a hole in all_scores[].
}
assert(all == njudges-2);
unsigned remote = 0;
for(/*see above*/; remote < njudges; ++all, ++remote)
{
if(remote != remote_max_index && remote != remote_min_index)
all_scores[all] = remote_scores[remote];
else
--all; //same deal here.
}

float total_sum = 0;
for(unsigned i = 0; i < nscores; ++i)
total_sum += all_scores[i];

cout << "The average score among all of the judges, excluding the min and max from each set, is "
<< (total_sum / nscores) << endl;

//functions declared as 'int' should return a value,
//but main() is special and returns 0 when you say nothing.
}```
Note also, that if a user "cheats" and puts in scores all greater than 10, the minimum will come out as 10, even though it should be higher. How would you fix that?

Do you see how the bolded code works? I didn't write it too clearly, but if you get how those loops work then you're doing well.