C ++找到最高价值

时间:2014-11-17 17:49:01

标签: c++ arrays function

我似乎无法弄清楚我的代码如何返回并打印成绩最高的学生。我这个学期(单声道)病得很重,所以我错过了很多讲座。我想知道是否有人可以帮我打印最高等级的学生姓名,成绩和平均分。 谢谢!

嗨,我似乎无法让我的代码打印出平均分最高的学生。它打印出一堆数字,平均值和lettergrade不正确。如果有人能向我解释发生了什么事就会很棒!

#include <iostream>
#include <iomanip>
#include <string.h>
#include <fstream>
#include <stdio.h>
using namespace std;

struct Student {
    string fName;
    string lName;
    int score1;
    int score2;
    int score3;
    int score4;
    int score5;
    char letterGrade;
    int averageTest;
};

void avgAndassignGrade(Student array[], int size);
void sortAlpha(Student array[], int size);
void PrintStudents(Student arr[], int size);
int findHighest(Student arr[], int size);
void PrintStudentsHighest(Student arr[], int grade, int size);

int main()
{
    int counter=0;
    Student class120[50];

    ifstream file( "grades.txt" );
    string line;

    if ( !file )
        cout<<"No file found.";
    if ( file.is_open() )
    {
        while ( getline (file,line) )
        {
            file >> class120[counter].fName
                 >> class120[counter].lName 
                 >> class120[counter].score1
                 >> class120[counter].score2
                 >> class120[counter].score3
                 >> class120[counter].score4
                 >> class120[counter].score5;
            counter++;
        }
        file.close();
    }
    avgAndassignGrade( class120, counter );
    sortAlpha( class120, counter );
    int grade = findHighest( class120, counter );
    PrintStudents( class120, counter );
    PrintStudentsHighest( class120, grade, counter );
}

void avgAndassignGrade(Student array[], int size)
{
    for (int i = 0; i < size; i++ )
    {
        int total = array[i].score1 + array[i].score2 + array[i].score3 + array[i].score4
                    + array[i].score5;
        array[i].averageTest = total / 5;
        if ( array[i].averageTest > 89 && array[i].averageTest < 100 )
            array[i].letterGrade = 'A';
        if ( array[i].averageTest > 79 && array[i].averageTest < 90 )
            array[i].letterGrade= 'B';
        if ( array[i].averageTest > 69 && array[i].averageTest < 80 )
            array[i].letterGrade= 'C';
        if ( array[i].averageTest > 59 && array[i].averageTest < 70 )
            array[i].letterGrade = 'D';
        if ( array[i].averageTest < 60 )
            array[i].letterGrade = 'E';
    }
}

void sortAlpha(Student array[],int size)
{
    Student temp;
    for ( int j = 0; j < size - 1; j++)
    {
        for (int l = 0; l < size - 1; l++)
        {
            if ( array[l].lName > array[l+1].lName )
            {
                temp = array[l];
                array[l] = array[l+1];
                array[l+1] = temp;
            }
        }
    }
}

int findHighest(Student arr[], int size)
{
    int large = arr[0].averageTest;
    for ( int i = 0; i < size; i++ )
    {
        if ( large < arr[i].averageTest ) {
            large = arr[i].averageTest;
        }
    }
    return large;
}

void PrintStudents(Student arr[], int size) {
    cout << "Name" << "\t\t" << "Average" << "\t\t" << "Grade" << "\n";
    for ( int i = 1; i < size; i++ )
    {
        cout << arr[i].lName + "," + arr[i].fName << "\t\t" << arr[i].averageTest << "\t\t" 
             << arr[i].letterGrade <<"\n";
    }
}

void PrintStudentsHighest( Student arr[], int grade, int size ) {
    Student high;

    for ( int i = 0; i < size; i++ )
    {
        if( arr[i].averageTest == grade)
            high=arr[i];
    }
    cout << "The student(s) with the highest grade."<< '\n'
         << "Name"<<"\t\t"<<"Average"<<"\t\t"<<"Grade"<<"\n"
         << high.lName + "," + high.fName << "\t\t" << high.averageTest << "\t\t"
         << high.letterGrade << endl;
}

编辑:我更改了代码。现在它打印出最高等级的东西,但它只是乱七八糟的数字。我正在尝试找到具有最高平均值的学生,并打印他/她的名字平均值和字母等级。

1 个答案:

答案 0 :(得分:0)

我并不完全知道你要做什么...但如果你想要找到许多数字之间的最高值,你必须做这样的事情:

int max = 0;
if (grade > max)
   max = grade; 

你可以创建数组来取代score1,score2 ....所以就像那样:

int scores[5]; //it's so easier to deal with :)

然后添加分数将与for循环(5循环)!!!

如需更多帮助......您可以详细了解您的计划,以帮助您更多! 我希望它对你有所帮助!!