Skip to main content
grammar
Source Link
Nick Udell
  • 5.2k
  • 1
  • 29
  • 68

With C++11, some code may be simplified:

double findLowest (double aaa, double bbb, double ccc, double ddd, double eee)
{
    return std::min({aaa, bbb, ccc, ddd, eee});
}

double findHighest (double aaa, double bbb, double ccc, double ddd, double eee)
{
    return std::max({aaa, bbb, ccc, ddd, eee});
}

But if you use arrayarrays and this algorithm, the code may be simplified even more:

/* calcScore calculates and return the average of 4 scores that remain after
   dropping the highest and lowest scores of the performer revieced. */
double calcScore(const double (&a)[5])
{
    auto it = std::minmax_element(std::begin(a), std::end(a));

    return (std::accumulate(std::begin(a), std::end(a), 0.)) - (*it.first + *it.second))  / 3;
}

int main ()
{
    double judges[5];

    // Ask for the score of each judge
    for (auto& judge : judges) {
        getJudgeData(judge);
        std::cout << judge << std::endl;
    }

    //Outputs the average value
    avgValue = calcScore(judges);
    std::cout << avgValue;

    return 0;
}

With C++11, some code may be simplified:

double findLowest (double aaa, double bbb, double ccc, double ddd, double eee)
{
    return std::min({aaa, bbb, ccc, ddd, eee});
}

double findHighest (double aaa, double bbb, double ccc, double ddd, double eee)
{
    return std::max({aaa, bbb, ccc, ddd, eee});
}

But if you use array and algorithm, the code may be simplified even more:

/* calcScore calculates and return the average of 4 scores that remain after
   dropping the highest and lowest scores of the performer revieced. */
double calcScore(const double (&a)[5])
{
    auto it = std::minmax_element(std::begin(a), std::end(a));

    return (std::accumulate(std::begin(a), std::end(a), 0.)) - (*it.first + *it.second))  / 3;
}

int main ()
{
    double judges[5];

    // Ask for the score of each judge
    for (auto& judge : judges) {
        getJudgeData(judge);
        std::cout << judge << std::endl;
    }

    //Outputs the average value
    avgValue = calcScore(judges);
    std::cout << avgValue;

    return 0;
}

With C++11, some code may be simplified:

double findLowest (double aaa, double bbb, double ccc, double ddd, double eee)
{
    return std::min({aaa, bbb, ccc, ddd, eee});
}

double findHighest (double aaa, double bbb, double ccc, double ddd, double eee)
{
    return std::max({aaa, bbb, ccc, ddd, eee});
}

But if you use arrays and this algorithm, the code may be simplified even more:

/* calcScore calculates and return the average of 4 scores that remain after
   dropping the highest and lowest scores of the performer revieced. */
double calcScore(const double (&a)[5])
{
    auto it = std::minmax_element(std::begin(a), std::end(a));

    return (std::accumulate(std::begin(a), std::end(a), 0.)) - (*it.first + *it.second))  / 3;
}

int main ()
{
    double judges[5];

    // Ask for the score of each judge
    for (auto& judge : judges) {
        getJudgeData(judge);
        std::cout << judge << std::endl;
    }

    //Outputs the average value
    avgValue = calcScore(judges);
    std::cout << avgValue;

    return 0;
}
Add missing const
Source Link
Jarod42
  • 566
  • 4
  • 10

With C++11, some code may be simplified:

double findLowest (double aaa, double bbb, double ccc, double ddd, double eee)
{
    return std::min({aaa, bbb, ccc, ddd, eee});
}

double findHighest (double aaa, double bbb, double ccc, double ddd, double eee)
{
    return std::max({aaa, bbb, ccc, ddd, eee});
}

But if you use array and algorithm, the code may be simplified even more:

/* calcScore calculates and return the average of 4 scores that remain after
   dropping the highest and lowest scores of the performer revieced. */
double calcScore(const double (&a)[5])
{
    auto it = std::minmax_element(std::begin(a), std::end(a));

    return (std::accumulate(std::begin(a), std::end(a), 0.)) - (*it.first + *it.second))  / 3;
}

int main ()
{
    double judges[5];

    // Ask for the score of each judge
    for (auto& judge : judges) {
        getJudgeData(judge);
        std::cout << judge << std::endl;
    }

    //Outputs the average value
    avgValue = calcScore(judges);
    std::cout << avgValue;

    return 0;
}

With C++11, some code may be simplified:

double findLowest (double aaa, double bbb, double ccc, double ddd, double eee)
{
    return std::min({aaa, bbb, ccc, ddd, eee});
}

double findHighest (double aaa, double bbb, double ccc, double ddd, double eee)
{
    return std::max({aaa, bbb, ccc, ddd, eee});
}

But if you use array and algorithm, the code may be simplified even more:

/* calcScore calculates and return the average of 4 scores that remain after
   dropping the highest and lowest scores of the performer revieced. */
double calcScore(double (&a)[5])
{
    auto it = std::minmax_element(std::begin(a), std::end(a));

    return (std::accumulate(std::begin(a), std::end(a), 0.)) - (*it.first + *it.second))  / 3;
}

int main ()
{
    double judges[5];

    // Ask for the score of each judge
    for (auto& judge : judges) {
        getJudgeData(judge);
        std::cout << judge << std::endl;
    }

    //Outputs the average value
    avgValue = calcScore(judges);
    std::cout << avgValue;

    return 0;
}

With C++11, some code may be simplified:

double findLowest (double aaa, double bbb, double ccc, double ddd, double eee)
{
    return std::min({aaa, bbb, ccc, ddd, eee});
}

double findHighest (double aaa, double bbb, double ccc, double ddd, double eee)
{
    return std::max({aaa, bbb, ccc, ddd, eee});
}

But if you use array and algorithm, the code may be simplified even more:

/* calcScore calculates and return the average of 4 scores that remain after
   dropping the highest and lowest scores of the performer revieced. */
double calcScore(const double (&a)[5])
{
    auto it = std::minmax_element(std::begin(a), std::end(a));

    return (std::accumulate(std::begin(a), std::end(a), 0.)) - (*it.first + *it.second))  / 3;
}

int main ()
{
    double judges[5];

    // Ask for the score of each judge
    for (auto& judge : judges) {
        getJudgeData(judge);
        std::cout << judge << std::endl;
    }

    //Outputs the average value
    avgValue = calcScore(judges);
    std::cout << avgValue;

    return 0;
}
added 843 characters in body
Source Link
Jarod42
  • 566
  • 4
  • 10

With C++11, some code may be simplified:

double findLowest (double aaa, double bbb, double ccc, double ddd, double eee)
{
    return std::min({aaa, bbb, ccc, ddd, eee});
}

double findHighest (double aaa, double bbb, double ccc, double ddd, double eee)
{
    return std::max({aaa, bbb, ccc, ddd, eee});
}

But if you use array and algorithm, the code may be simplified even more:

/* calcScore calculates and return the average of 4 scores that remain after
   dropping the highest and lowest scores of the performer revieced. */
double calcScore(double (&a)[5])
{
    auto it = std::minmax_element(std::begin(a), std::end(a));

    return (std::accumulate(std::begin(a), std::end(a), 0.)) - (*it.first + *it.second))  / 3;
}

int main ()
{
    double judges[5];

    // Ask for the score of each judge
    for (auto& judge : judges) {
        getJudgeData(judge);
        std::cout << judge << std::endl;
    }

    //Outputs the average value
    avgValue = calcScore(judges);
    std::cout << avgValue;

    return 0;
}

With C++11, some code may be simplified:

double findLowest (double aaa, double bbb, double ccc, double ddd, double eee)
{
    return std::min({aaa, bbb, ccc, ddd, eee});
}

double findHighest (double aaa, double bbb, double ccc, double ddd, double eee)
{
    return std::max({aaa, bbb, ccc, ddd, eee});
} 

With C++11, some code may be simplified:

double findLowest (double aaa, double bbb, double ccc, double ddd, double eee)
{
    return std::min({aaa, bbb, ccc, ddd, eee});
}

double findHighest (double aaa, double bbb, double ccc, double ddd, double eee)
{
    return std::max({aaa, bbb, ccc, ddd, eee});
}

But if you use array and algorithm, the code may be simplified even more:

/* calcScore calculates and return the average of 4 scores that remain after
   dropping the highest and lowest scores of the performer revieced. */
double calcScore(double (&a)[5])
{
    auto it = std::minmax_element(std::begin(a), std::end(a));

    return (std::accumulate(std::begin(a), std::end(a), 0.)) - (*it.first + *it.second))  / 3;
}

int main ()
{
    double judges[5];

    // Ask for the score of each judge
    for (auto& judge : judges) {
        getJudgeData(judge);
        std::cout << judge << std::endl;
    }

    //Outputs the average value
    avgValue = calcScore(judges);
    std::cout << avgValue;

    return 0;
}
Source Link
Jarod42
  • 566
  • 4
  • 10
Loading