Student Grades Struct
Create a program that takes student grades as input and stores them in a struct before outputting the average grade of the student.Write a struct called student_grades with the variables math, english, geography, and science. Write a function float get_average_grade that takes a student_grades struct and returns the average of the grades.
Write a struct called student_grades with the variables math, english, geography, and science. Write a function float get_average_grade that takes a student_grades struct and returns the average of the grades.
Answer
#include <iostream>
struct student_grades {
float math;
float english;
float geography;
float science;
};
float get_average_grade(student_grades grades) {
float total = grades.math + grades.english + grades.geography + grades.science;
return total / 4.0f; // Dividing by the number of subjects
}
int main() {
student_grades student;
std::cout << "Enter the math grade: ";
std::cin >> student.math;
std::cout << "Enter the english grade: ";
std::cin >> student.english;
std::cout << "Enter the geography grade: ";
std::cin >> student.geography;
std::cout << "Enter the science grade: ";
std::cin >> student.science;
float average = get_average_grade(student);
std::cout << "The average grade is: " << average << std::endl;
return 0;
}