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

Time:05-28

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.

CodePudding user response:

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;
        }
  •  Tags:  
  • c#
  • Related