Home > Blockchain >  Score average (Cambodian Idol Practice)
Score average (Cambodian Idol Practice)

Time:10-09

I'm having a bit of trouble with a coding project the question is as follows.

A particular talent competition has five judges, each of whom awards a score between 0 and 10 to each performer. Fractional scores, such as 8.3, are allowed. A performer’s final score is determined by dropping the highest and lowest score received, then averaging the three remaining scores. Write the program that uses this method to calculate a contestant’s score.

I have to use the following, void getJudgeData(), void calcScore(), double findLowest(), double findHighest()

My code is as follows

#include <iostream>
using namespace std;

double inputValidation(double);
void getJudgeData(double&);
void calcScore(double, double, double, double, double);
double ifLowest(double, double, double, double, double);
double findLowest(double, double, double, double, double);
double ifHighest(double, double, double, double, double);
double findHighest(double, double, double, double, double);

int main()
{
    double score1, score2, score3, score4, score5;

    getJudgeData(score1);
    getJudgeData(score2);
    getJudgeData(score3);
    getJudgeData(score4);
    getJudgeData(score5);

    calcScore(score1, score2, score3, score4, score5);

    return 0;
}

double inputValidation(double num)
{
    while (!(cin >> num) || (num < 0 || num > 10))
    {
        cout << "Incorrect Score parameters" << endl;
        exit(0);
        return 0;
    }
    return num;
}

void getJudgeData(double& num)
{
    cout << "Enter Score: ";
    num = inputValidation(num);
}

void calcScore(double num1, double num2, double num3, double num4, double num5)
{
    double average, lowest = findLowest(num1, num2, num3, num4, num5), highest = findHighest(num1, num2, num3, num4, num5);

    if ((num1 == lowest)||(num5 == highest))
        average = (num2   num3   num4) / 3;
    else if ((num2 == lowest)||(num4 == highest))
        average = (num1   num3   num5) / 3;
    else if ((num3 == lowest)||(num3 == highest))
        average = (num1   num2   num4   num5) / 3;
    else if ((num4 == lowest)||(num2 == highest))
        average = (num1   num3   num5) / 3;
    else if ((num5 == lowest)||(num1 == highest))
        average = (num2   num3   num4) / 3;

    cout << "The average is: ";
    cout << average << endl;
}

double ifLowest(double num1, double num2, double num3, double num4, double num5)
{
    double lowest{};
    if (num1 <= num2)
    {
        if (num1 <= num3)
        {
            if (num1 <= num4)
            {
                if (num1 <= num5)
                {
                    lowest = num1;
                }
            }
        }
    }
    return lowest;
}

double findLowest(double num1, double num2, double num3, double num4, double num5)
{
    double smallest;

    smallest = ifLowest(num1, num2, num3, num4, num5);
    smallest = ifLowest(num2, num3, num4, num5, num1);
    smallest = ifLowest(num3, num4, num5, num1, num2);
    smallest = ifLowest(num4, num5, num1, num2, num3);
    smallest = ifLowest(num5, num1, num2, num3, num4);

    return smallest;
}

double ifHighest(double num1, double num2, double num3, double num4, double num5)
{
    double highest{};
    if (num5 >= num2)
    {
        if (num5 >= num3)
        {
            if (num5 >= num4)
            {
                if (num5 >= num1)
                {
                    highest = num5;
                }
            }
        }
    }
    return highest;
}
double findHighest(double num1, double num2, double num3, double num4, double num5)
{
    double largest;

    largest = ifHighest(num1, num2, num3, num4, num5);
    largest = ifHighest(num2, num3, num4, num5, num1);
    largest = ifHighest(num3, num4, num5, num1, num2);
    largest = ifHighest(num4, num5, num1, num2, num3);
    largest = ifHighest(num5, num1, num2, num3, num4);

    return largest;
}

In the void calcScore, average gives me an error but only when i attempt to drop the highest score then average

CodePudding user response:

For solving this problem, you don't need to define isLowest() and isHighest() function. You can directly find the highest and lowest number by just comparing all numbers and also in your calcScore() function, you have not covered all cases. For example if num1 is lowest and num2 is highest. Here is the correct code -

#include <iostream>
using namespace std;

double inputValidation(double);
void getJudgeData(double &);
void calcScore(double, double, double, double, double);
double findLowest(double, double, double, double, double);
double findHighest(double, double, double, double, double);

int main()
{
  double score1, score2, score3, score4, score5;

  getJudgeData(score1);
  getJudgeData(score2);
  getJudgeData(score3);
  getJudgeData(score4);
  getJudgeData(score5);

  calcScore(score1, score2, score3, score4, score5);

  return 0;
}

double inputValidation(double num)
{
  while (!(cin >> num) || (num < 0 || num > 10))
  {
    cout << "Incorrect Score parameters" << endl;
    exit(0);
    return 0;
  }
  return num;
}

void getJudgeData(double &num)
{
  cout << "Enter Score: ";
  num = inputValidation(num);
}

void calcScore(double num1, double num2, double num3, double num4, double num5)
{
  double average, lowest = findLowest(num1, num2, num3, num4, num5), highest = findHighest(num1, num2, num3, num4, num5);
  average = (num1   num2   num3   num4   num5 - lowest - highest) / 3;
  cout << "The average is: ";
  cout << average << endl;
}

double findLowest(double num1, double num2, double num3, double num4, double num5)
{
  double temp[] = {num1, num2, num3, num4, num5};
  double smallest = num1;
  for (int i = 0; i < 5; i  )
  {
    if (temp[i] < smallest)
    {
      smallest = temp[i];
    }
  }

  return smallest;
}

double findHighest(double num1, double num2, double num3, double num4, double num5)
{
  double temp[] = {num1, num2, num3, num4, num5};
  double largest = num1;
  for (int i = 0; i < 5; i  )
  {
    if (temp[i] > largest)
    {
      largest = temp[i];
    }
  }

  return largest;
}
  •  Tags:  
  • c
  • Related