All Files here
This commit is contained in:
56
Q4.c
Normal file
56
Q4.c
Normal file
@@ -0,0 +1,56 @@
|
||||
#include <stdio.h>
|
||||
//Question 4
|
||||
double find_average(const int arr[], int len) {
|
||||
double sum = 0.0;
|
||||
for(int i = 0; i < len; i++) {
|
||||
sum += arr[i];
|
||||
}
|
||||
return sum/len;
|
||||
}
|
||||
// Returns the INDEX of the highest value in array
|
||||
int find_index_highest(const double arr[], int len) {
|
||||
double highest = arr[0];
|
||||
int index_highest = 0;
|
||||
|
||||
for(int i = 0; i < len; i++) {
|
||||
if(arr[i] > highest) {
|
||||
highest = arr[i];
|
||||
index_highest = i;
|
||||
}
|
||||
}
|
||||
return index_highest;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int num_students, num_subjects;
|
||||
printf("Enter number of students: ");
|
||||
scanf("%d", &num_students);
|
||||
printf("Enter number of subjects: ");
|
||||
scanf("%d", &num_subjects);
|
||||
|
||||
int grade_matrix[num_students][num_subjects];
|
||||
for(int i = 0; i < num_students; i++) {
|
||||
printf("Enter student %d's grades: ", i+1);
|
||||
for(int j = 0; j < num_subjects; j++) {
|
||||
scanf("%d", &grade_matrix[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
double average_grades[num_students];
|
||||
printf("Average Grades:\n");
|
||||
for(int i = 0; i < num_students; i++) {
|
||||
average_grades[i] = find_average(grade_matrix[i], num_subjects);
|
||||
printf("Student %d: %0.02f\n", i+1, average_grades[i]);
|
||||
}
|
||||
|
||||
printf("Student with Highest Average: Student %d\n", (find_index_highest(average_grades, num_students) +1));
|
||||
|
||||
printf("Students with average >= 75:\n");
|
||||
for(int i = 0; i < num_students; i++) {
|
||||
if(average_grades[i] >= 75.0)
|
||||
printf("Student %d\n", i+1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user