首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用一组学生和他们各自的GPA计算GPA的各种方法

用一组学生和他们各自的GPA计算GPA的各种方法
EN

Stack Overflow用户
提问于 2020-03-27 22:45:14
回答 1查看 615关注 0票数 0

关于我正在研究的问题,我要给出更多的细节,具体的任务是:

编写一个计算所有学生平均、最低、最高和平均绩点的程序。首先,程序将读取学生记录(姓名和GPA)并确定文件中学生记录的数量。在获得所有的名称和GPA之后,您的程序将按GPA的升序对学生GPA和名称进行排序。最后,您的程序将: 1)显示所有的名称和GPA;2)确定和显示GPA的最小和最大值(有相应的学生名称);3)计算和显示平均GPA。

这里是我作为成品拥有的东西:

代码语言:javascript
复制
//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 ()‘中:

-WaddressSolved`

  • 32:23:警告:语句没有效果,-Wunused-valueSolved`

  • 35:12:警告:语句是引用,而不是调用,而不是调用,若要运行“-WaddressSolved`

  • 35:12:”-Wunused-valueSolved`

  • 43:17:警告:语句没有影响-Wunused-valueSolved`

  • 43:17:错误:“名称”未在此作用域中声明,则

  • 43:32: error: GPA:'GPA‘未在此作用域中声明,

  • 48:21:警告:语句是引用,而不是调用,-WaddressSolved`

  • 48:21:警告:语句不影响-Wunused-valueSolved`

  • 51:12:错误:无法在assignmentSolved

中将'double(const *,int)‘转换为'displayMinMaxGPA’

将非常感谢您的任何帮助,谢谢提前

"`“错误通过调用函数并将参数放入

"“错误通过调用calcAvgGPA函数后在括号中放置参数来解决。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-28 02:49:24

你在基本理解上有几个大错误。让我们仔细研究一下,看看我们能帮助我们了解些什么。另外,您使用的是std::string,但不使用std::vector (这将使整个分配更容易)。

此外,(2)免责声明,我还没有看过任何函数中的逻辑,看它们是否正确工作.一旦这为您构建,您可以工作,以使逻辑正确,如果它是错误的。

修正了scope. CoryKramer提到的错误(您已经编辑了问题以表明您有),

  • 您没有在主函数
  • 中声明GPA或name对象,因为您没有使用类,并且没有任何成员数据,您需要将这些信息存储在main
  • 中。

  1. 您的函数不接受对其参数的引用,而是通过复制

获取它们。

不声明变量

代码语言:javascript
复制
    //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的作用域中声明这些变量,如果在可以编辑这些变量的作用域中没有这些变量,则不同的函数将无法使用相同的变量。

以复制方式获取项目与以参考方式获取

代码语言:javascript
复制
//Function prototypes
void obtainNamesAndGPAs(double GPA[], string name[], int &students);

在这个原型中,通过引用参数students,这意味着可以更新main作用域中的变量。但是你不是通过引用其他任何参数,而是通过复制。注意到丢失的&了吗?即使在main作用域中有这些变量,它们也不会被更新,因为函数在其作用域中复制和使用这些副本,而不是在更高的作用域中修改变量。您的所有函数本质上都存在此错误。考虑哪些函数需要输入,哪些变量需要修改,哪些变量不需要修改。

也是

我有一种感觉,你会遇到更大的问题与动态数组大小。我可以看到这一点,因为我没有看到new实例用于GPAname变量。这是一个std::vector将真正闪耀的地方,但我将由你来完成(因为这是一项家庭作业)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60895095

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档