Most is done, but idk how they want to print the name too. The whole point of Unions is that u cant have the grade and the name filled out
This commit is contained in:
2024-10-15 10:57:56 -07:00
parent b2462123db
commit c56cfeb3a7

64
Q2.c Normal file
View File

@@ -0,0 +1,64 @@
#include <stdio.h>
typedef union Student {
char name[50];
int roll_number;
int grades[3];
} Student;
double student_average_grade(Student student) {
double average_grade = 0.0;
for(int i = 0; i < 3; i++) {
average_grade += student.grades[i];
}
return average_grade/3;
}
double all_student_averages(Student arr[], int len) {
double average_grade = 0.0;
for(int i = 0; i < len; i++) {
average_grade += student_average_grade(arr[i]);
}
return average_grade/len;
}
Student highest_grade(Student arr[], int len) {
Student highest = arr[0];
for(int i = 0; i < len; i++) {
if(student_average_grade(arr[i]) > student_average_grade(highest)) {
highest = arr[i];
}
}
return highest;
}
int main() {
Student John;
John.grades[0] = 3;
John.grades[1] = 4;
John.grades[2] = 3;
Student Bob;
Bob.grades[0] = 4;
Bob.grades[1] = 4;
Bob.grades[2] = 4;
Student Bo;
Bo.grades[0] = 2;
Bo.grades[1] = 1;
Bo.grades[2] = 2;
Student arr[3];
arr[0] = John;
arr[1] = Bob;
arr[2] = Bo;
printf("Average Grade: %0.02f\n", student_average_grade(arr[0]));
printf("Average Grade: %0.02f\n", student_average_grade(arr[1]));
printf("Average Grade: %0.02f\n", student_average_grade(arr[2]));
printf("Average Grade: %0.02f", all_student_averages(arr, 3));
//("Student with highest grades: ")
return 0;
}