使用数组c ++计算三个等级的平均值

时间:2016-12-01 01:25:44

标签: c++

我目前正在开发一个c ++课程,该课程将处理并生成一个学期的学生成绩单,假设学生正在上五个课程。(输出以图表格式打印到屏幕上)。学生将为每门课程输入三个年级。课程名称,第一考试成绩,第二考试成绩和第三考试成绩应该有一个数组。

好吧,公平。好吧,我已经设法初始化上面的数组并在下面的代码中正确输出它们。这是我有点困惑的地方。接下来,我必须初始化一个数组,该数组将计算每个课程的三个考试成绩的平均值,并为每个课程的平均值初始化一个字母等级的数组。(并以与考试相同的方式将它们打印到屏幕上成绩和课程名称。)也许如果有人能够阐明如何创建一个计算三个等级的平均值的数组,我可以尝试找出如何为字母等级制作数组。谢谢。

# include iostream

using namespace std;
const int SIZE =5;

void getData(string courseName[], float examOne[], float examTwo[], float examThree[]);


int main ()
{
    // local declaration
    string courseName[SIZE];
    float examOne[SIZE];
    float examTwo[SIZE];
    float examThree[SIZE];

    getData(courseName,examOne,examTwo,examThree);


    cout<<"\n\n\n";

    for (int i = 0; i <SIZE; i++){
        cout<< courseName[i]<<"    "<<examOne[i]<<"    "<<examTwo[i]<<"     "<<examThree[i]<<"   "<<total[i]<< endl;

    }  
    return 0;
}



void getData(string courseName[],float examOne[],float examTwo[],float examThree[]){

    for (int i = 0; i <SIZE; i++){
    cout<<"Enter Course Name: "; 
    cin >> courseName[i];
    cout<<"enter First Exam Grade: ";
    cin>>examOne[i];
    cout<<"enter Second Exam Grade: ";
    cin>>examTwo[i];
    cout<<"enter Third Exam Grade: ";
    cin>>examThree[i];

    }   
}

1 个答案:

答案 0 :(得分:0)

编辑:

我添加了一个功能,将学生的平均成绩显示为字母。

using namespace std;被认为是不好的做法,有时会很危险。检查this。 将SIZE声明为全局变量也被认为是不好的做法。

#include <iostream>
#include <string>

void getData(int sizeArray, std::string courseName[], float examOne[], float examTwo[], float examThree[], float average[]);
std::string letterGrade(int grade);
void highestLowestAverageByExam(float tExamArray[], int tSize, float & tHighestGradeExam, float & tLowestGradeExam, float & tAverageGradeExam);

int main()
{
    // local declaration
    const int SIZE = 5;
    std::string courseName[SIZE];
    float examOne[SIZE];
    float examTwo[SIZE];
    float examThree[SIZE];
    float average[SIZE];

    getData(SIZE, courseName, examOne, examTwo, examThree, average);

    std::cout << "\n\n\n";

    for (int i = 0; i <SIZE; i++)
    {
        std::cout << courseName[i] << "    " << examOne[i] << "    " << examTwo[i] << "     "
            << examThree[i] << "   " << average[i] << "   " << letterGrade(average[i]) << std::endl;
    }
    std::cout << "\n";

    float highestGradeExam = 0;
    float lowestGradeExam = 0;
    float averageGradeExam = 0;

    highestLowestAverageByExam(examOne, SIZE, highestGradeExam, lowestGradeExam, averageGradeExam);
    std::cout << "Exam number 1:  Highest grade = " << highestGradeExam
        << "  Lowest grade = " << lowestGradeExam
        << "  Average grade = " << averageGradeExam << std::endl;


    return 0;
}

void getData(int sizeArray, std::string courseName[], float examOne[], float examTwo[], float examThree[], float average[])
{
    for (int i = 0; i < sizeArray; i++)
    {
        int count = 0;
        std::cout << "Enter Course Name: ";
        std::cin >> courseName[i];

        std::cout << "enter First Exam Grade: ";
        std::cin >> examOne[i];
        count++;

        std::cout << "enter Second Exam Grade: ";
        std::cin >> examTwo[i];
        count++;

        std::cout << "enter Third Exam Grade: ";
        std::cin >> examThree[i];
        count++;

        average[i] = (examOne[i] + examTwo[i] + examThree[i]) / count;
    }
}


std::string letterGrade(int averageGrade)
{
    if (averageGrade >= 0 && averageGrade <= 100)
    {
        const int NUMBER_CATEGORIES = 11;
        const std::string GRADE_LETTER[] = { "F", "D", "D+", "C-", "C", "C+", "B-", "B", "B+", "A-", "A" };
        const int LOWEST_GRADE_SCORE[] = { 0, 60, 67, 70, 73, 77, 80, 83, 87, 90, 93 };
        int category = 0;
        while (category < NUMBER_CATEGORIES && LOWEST_GRADE_SCORE[category] <= averageGrade)
        {
            category++;
        }
        return GRADE_LETTER[category - 1];
    }
    else
    {
        return "Average grade is not valid.";
    }
}

void highestLowestAverageByExam(float tExamArray[], int tSize, float & tHighestGradeExam, float & tLowestGradeExam, float & tAverageGradeExam)
{
    tHighestGradeExam = tExamArray[0];
    tLowestGradeExam = tExamArray[0];
    for (int i = 1; i < tSize; i++)
    {
        if (tExamArray[i] > tHighestGradeExam)
        {
            tHighestGradeExam = tExamArray[i];
        }
        if (tExamArray[i] < tLowestGradeExam)
        {
            tLowestGradeExam = tExamArray[i];
        }
    }
    tAverageGradeExam = (tHighestGradeExam + tLowestGradeExam) / 2;
}