简体   繁体   中英

How to calculate the grade point average(GPA) of students

I want to write a method for a University class that takes a discipline as a parameter and returns the grade point average(GPA) of the students in that discipline, but I don't know how to do it, this is the signature of the method:

public double AverageOfDiscipline(discipline D)
    {
        double sum = 0;
        for (int i = 0; i < students.Length; i++)
        {
            //???
        }
        return //????
    }

and this is my project:

public enum discipline { Computer, Civil, Mechanical, Electrical}
public enum educationType { Undergraduate, Postgraduate}
class Student
{
    public string nameAndLastName;
    public double GPA;
    public discipline discipline;
    public educationType education;
    public Student(string nameAndLastName, double GPA, discipline discipline, educationType education)
    {
        this.nameAndLastName = nameAndLastName;
        this. GPA = GPA;
        this.discipline = discipline;
        this.education = education;
    }
    public string ToString()
    {
        return nameAndLastName + ": "+ education + ", "+ discipline+ "; "+"GPA: "+ GPA ;
    }
}

class University
{
    public string uniName;
    public Student[] students;
    public double AverageOfDiscipline(discipline D)
    {
        double sum = 0;
        for (int i = 0; i < students.Length; i++)
        {
            //???
        }
        return //????
    }
}

Thanks for your help.

For the university class you need to iterate through the students array, check if they are pursuing the specified displine, sum then calculare average as below

  public double AverageOfDiscipline(discipline D)
        {
            double sum = 0;
            int totalStudentsForDispline = 0;
            for (int i = 0; i < students.Length; i++)
            {
                if (students[i].discipline == D)
                {
                    sum += students[i].GPA;
                    totalStudentsForDispline += 1;
                }
            }

            return sum > 0 ? sum / totalStudentsForDispline : 0;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM