首页 > 解决方案 > 我在使用数组/函数的 C++ 程序时遇到问题

问题描述

对于我的课程,我需要使用数组而不是包含分数的单个变量来编写程序。为这一章而奋斗。我将在下面留下我的代码。任何帮助将不胜感激。需要找到输入的所有分数之间的总和,减去最高和最低,然后找到平均值。


#include <iostream>
#include <iomanip>

using namespace std;

// Function prototypes

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

int main()
{
// Declare variables

    const int SIZE = 7;
    double scores[SIZE];
    double sum;
    
    for (int x = 0; x < SIZE; x++)
        double getJudgeData(scores[x]);
    
    
    return 0;
}

// Function getJudgeData

void getJudgeData(double &judgeScore, double scores[])
{
    static int judge = 1;
    cout << "Enter score " << judge << ": ";
    cin >> judgeScore;
    while (judgeScore < 0 || judgeScore > 10)
    {
        cout << "Invalid score, please enter a score between 0 and 10";
        cout << "Enter score " << judge << ": ";
        cin >> judgeScore;
    }
    judge++;

    return;
}

// Function calcScore

double calcScore(double scores[])
{
    double highest = findHighest(scores);
    double lowest = findLowest(scores);
    double result = 0;
    for (int x = 0; x < 7; x++)
    {
        result += scores[x];
    }       

    
    result - findHighest(scores) - findLowest(scores);



    cout << fixed << showpoint << setprecision(2);
    cout << "The average after the dropping the highest and lowest scores: " << result / 5 << endl;

    return result;
}

// Function findLowest

double findLowest(double scores[])
{
    double result = 10;
    for (int x = 0; x < 7; x++)
    {
        if (scores[x] < result)
            result = scores[x];
    }
    return result;
}

// Function findHighest

double findHighest(double scores[])
{
    double result = 0;
    for (int x = 0; x < 7; x++)
    {
        if (scores[x] > result)
            result = scores[x];
    }
    return result;
}


标签: c++arraysfunction

解决方案


推荐阅读