使用一组学生及其各自的 GPA 对 GPA 进行各种计算

Posted

技术标签:

【中文标题】使用一组学生及其各自的 GPA 对 GPA 进行各种计算【英文标题】:Various calculations on GPA using a collection of students and their respective GPA's 【发布时间】:2020-03-27 22:45:14 【问题描述】:

为了更详细地说明我正在处理的问题是确切的任务:

编写一个程序来计算平均值、最小值、最大值和 所有学生的平均 GPA。首先,你的程序会读入学生记录 (姓名和 GPA)并确定文件中学生记录的数量。毕竟 获得姓名和 GPA 后,您的程序将对学生 GPA 和 按 GPA 升序排列的名称。最后,您的程序将,1)显示所有名称 和 GPA,2) 确定并显示最小和最大 GPA(带有 对应的学生姓名),以及 3) 计算并显示平均 GPA。

这是我的成品:

//gpaCalc.cc                                                                                                                                                                                   
//Author: O'Brien Little                                                                                                                                                                       
//Purpose: To calculate and display the average, minimum and maximum GPA for some U of L students, where the GPA's and student names are read from
//an input file and are stored as arrays 
//Inputs: GPA's of several students, student names                                                                                                                                                          
//Outputs: Average, minimum and maximum GPA of the collection of students, along with the corresponding student names
//and display a list of all students and their respective GPA's
//Assumptions: Max 50 Students' information

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

const int arraySize=50; //Upper limit on the number of students and GPA's

//Function prototypes
void obtainNamesAndGPAs(double GPA[], string name[], int &students);
void sortGPA(double GPA[], string name[], int students);
void displayMinMaxGPA(const double GPA[], const string name[], int students);
double calcAvgGPA(const double GPA[], int students);



int main()

    //Variable declarations
    double AvgGPA;
    int index=0, students;

    //Reading the names of the students and their corresponding GPAs and storing them in an array
    obtainNamesAndGPAs;

    //Sorting the students GPAs in assending order allong with the corresponding student names
    sortGPA;

    //Displaying all the names of the students and their GPAs
    cout << "Here is a list of all the students and their GPAs that were entered into te system: " << endl;

    //While loop to display all the students and their GPAs
    while(index<students)
    
        cout << name[index] << GPA[index];
        index++;
    

    //Displaying the lowest and the highest GPAs and the students that achieved those
    displayMinMaxGPA;

    //Calculating the average GPA of the collection
    AvgGPA = calcAvgGPA;

    //Displaying the average GPA to the user
    cout << "The average GPA of the collection of students was: " << AvgGPA << endl;

    //End program
    return 0;


//****************************************************************                                                                                                                             
//Function: obtainNamesAndGPAs                                                                                                                                                                        
//Purpose: To obtain the names and GPAs of the students                                                                                                                                
//Input: GPA[], name[], &students                                                                                                                                                             
//Return Value: Void                                                                                                                                                                           
//Assumtions: None                                                                                                                                            
//****************************************************************  
void obtainNamesAndGPAs(double GPA[], string name[], int &students)

    //Array access variable
    int indexn=0, indexg=0; 

    //File stream declaration
    ifstream inFile;

    //Opening the input file and read in the first value
    inFile.open("GPA.txt");
    inFile >> name[indexn];

    //While loop to gather the GPAs from the file and insert them into their corresponding array index
    while(!inFile.eof() && indexn < arraySize)
    
        indexn++;
        inFile >> GPA[indexg];
        indexg++;
        inFile >> name[indexn];
        students++;
    
    //End of function
    return;


//****************************************************************                                                                                                                             
//Function: sortGPA                                                                                                                                                                       
//Purpose: To sort students (and thier corresponding GPAs 
//in assending order
//Input: GPA[], name[], students                                                                                                                                                            
//Return Value: Void                                                                                                                                                                           
//Assumtions: None                                                                                                                                            
//****************************************************************  
void sortGPA(double GPA[], string name[], int students)

    //Variable declarations
    double temporaryg;
    int first, second;
    string temporaryn;

    //Sorting the GPAs by asscending order
    //For loop to indicate the first value of the GPA array
    for(first=0;first<students;first++)
           
        //For loop to indicate the following value in the GPA array to check 
        for(second=first+1;second<students;second++)
        
            //If statement to make sure the GPA and name array are in asscending order and 
            //ensures the student name stays with the GPA
            if(GPA[first]>GPA[second])
            
                //Storing the bigger GPA and name for later
                temporaryg=GPA[first];
                temporaryn=name[first];
                //Making it so the smaller GPA and name comes first
                GPA[first]=GPA[second];
                name[first]=name[second];
                //Making the lower GPA and name come second
                GPA[second]=temporaryg;
                name[second]=temporaryn;
            
        
    

    //End of function
    return;


//****************************************************************                                                                                                                             
//Function: displayMinMaxGPA                                                                                                                                                                      
//Purpose: To display the Min and Max GPA and their students
//Input: GPA[], name[], students                                                                                                                                                            
//Return Value: Void                                                                                                                                                                           
//Assumtions: None                                                                                                                                            
//**************************************************************** 
void displayMinMaxGPA(const double GPA[], const string name[], int students)

    //Variable declaration
    //initialized to extreme values to ensure they will be taken by the array values
    double Maxg=0, Ming=10;
    int index;
    string Maxn, Minn;

    //For loop to find the Max and Min GPA in the array and find the corresponding 
    //students name
    for(index=0;index<students;index++)
    
        if(GPA[index]>Maxg)
        
            Maxg=GPA[index];
            Maxn=name[index];
        
        else if(GPA[index]<Ming)
        
            Ming=GPA[index];
            Minn=name[index];
        
        else
        
            break;
        
    

    //Displaying the Min and Max GPA and the corresponding students to the user
    cout << "The minimum GPA that was entered belonged to: " << Ming << " and was: " << Minn << endl;
    cout << "The maximum GPA that was entered belonged to: " << Maxg << " and was: " << Maxg << endl;

    //End of function
    return;


//****************************************************************                                                                                                                             
//Function: calcAvgGPA                                                                                                                                                                   
//Purpose: To calculate the average GPA
//Input: GPA[], students                                                                                                                                                            
//Return Value: AvgGPA                                                                                                                                                                           
//Assumtions: None                                                                                                                                            
//**************************************************************** 
double calcAvgGPA(const double GPA[], int students)

    //Variable declarations
    double sum, AvgGPA;
    //Index set to zero to make sure the array starts in the first position
    int index=0;

    //While loop to take each individual GPA out of the array and add it to sum
    while(index<students)
    
        sum = sum + GPA[index];
        index++;
    

    //Calculating the average GPA
    AvgGPA = sum/students;


    //End of function and return AvgGPA
    return AvgGPA;

但是,我收到以下错误,我无法自行解决:

在函数'int main()'中:

32:23:警告:语句是对函数的引用,而不是调用 'obtainNamesAndGPA' [-Waddress]已解决` 32:23:警告:语句无效 [-Wunused-value]已解决` 35:12:警告:语句是对函数的引用,而不是调用 'sortGPA' [-Waddress]已解决` 35:12:警告:语句无效 [-Wunused-value]已解决` 43:17:错误:“名称”未在此范围内声明 43:32:错误:未在此范围内声明“GPA” 48:21:警告:语句是对函数的引用,而不是调用 'displayMinMaxGPA' [-Waddress]已解决` 48:21:警告:语句无效 [-Wunused-value]已解决` 51:12:错误:无法将 'double(const double*, int)' 转换为 'double' 在任务中已解决``

任何帮助将不胜感激,在此先感谢

“`”错误通过调用函数并放入参数解决

"``" 错误通过在调用 calcAvgGPA 函数后将参数放在括号中解决

【问题讨论】:

您需要添加括号才能实际调用函数sortGPA()。除了实际传递该函数所需的参数sortGPA(GPA, name, students); 谢谢,帮助解决了大部分错误 【参考方案1】:

您在基本理解上存在一些重大错误。让我们通过它们,看看我们可以帮助启发什么。此外,您正在使用 std::string 但未使用 std::vector (这将使整个分配更容易)。

另外(2) 免责声明,我没有查看任何函数中的逻辑以查看它们是否可以正常工作...一旦为您构建,您就可以继续如果逻辑错了,就得到正确的逻辑。

    修复 CoryKramer 提到的错误(您已将问题编辑为建议您有) 您没有在主函数中声明 GPA 或名称对象 由于您没有使用类并且没有任何成员数据,因此您需要将此信息存储在 main 范围内。 您的函数不引用其参数,而是通过复制获取它们

不声明变量

    //Displaying all the names of the students and their GPAs
    cout << "Here is a list of all the students and their GPAs that were entered into te system: " << endl;

    //While loop to display all the students and their GPAs
    while(index<students)
    
        cout << name[index] << GPA[index];  // <--- This line
        index++;
    

上面提到的行试图引用这个范围(或全局范围)中不存在的 2 个变量。您需要在main 的范围内声明这些变量。如果这些变量在可编辑的范围内不可用,则不同的函数将无法使用相同的变量。


通过复制与参考获取项目

//Function prototypes
void obtainNamesAndGPAs(double GPA[], string name[], int &students);

在此原型中,您通过引用获取参数students,这意味着可以更新main 范围内的变量。但是您不是通过引用而是通过复制来获取任何其他参数。注意到缺少的&amp;了吗?即使您在main 范围内有这些变量,它们也不会随着函数制作副本并在其范围内使用这些副本而不是在更高范围内修改变量而得到更新。 您的所有功能本质上都有此错误。 想想函数需要输入什么,哪些变量需要修改,哪些不需要。


还有

我感觉您会遇到动态数组大小的更大问题。我可以看到这一点,因为我看不到 GPAname 变量的 new 实例。这是std::vector 真正发光的地方,但我会留给你完成(因为这是家庭作业)。

【讨论】:

谢谢你,你的建议真的很有帮助,我重写了我的代码,牢记这些并且进步了很多

以上是关于使用一组学生及其各自的 GPA 对 GPA 进行各种计算的主要内容,如果未能解决你的问题,请参考以下文章

GPA怎么算

计算学生证列表的GPA

Bailian4043 GPA排名系统水题

Bailian4043 GPA排名系统水题

什么是GPA,TOEFL,GRE,GMAT,LSAT,MCAT,SAT

如何从 python 中的用户 2 值读取并找到最高 GPA?