Question 4 done
This commit is contained in:
9
Q4.c
9
Q4.c
@@ -1,5 +1,7 @@
|
||||
#include <stdio.h>
|
||||
//Question 4
|
||||
|
||||
// Finds average value of array
|
||||
double find_average(const int arr[], int len) {
|
||||
double sum = 0.0;
|
||||
for(int i = 0; i < len; i++) {
|
||||
@@ -7,6 +9,7 @@ double find_average(const int arr[], int len) {
|
||||
}
|
||||
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];
|
||||
@@ -22,12 +25,14 @@ int find_index_highest(const double arr[], int len) {
|
||||
}
|
||||
|
||||
int main() {
|
||||
// To test the functions
|
||||
int num_students, num_subjects;
|
||||
printf("Enter number of students: ");
|
||||
scanf("%d", &num_students);
|
||||
printf("Enter number of subjects: ");
|
||||
scanf("%d", &num_subjects);
|
||||
|
||||
// Create matrix of grades and use input to assign its values
|
||||
int grade_matrix[num_students][num_subjects];
|
||||
for(int i = 0; i < num_students; i++) {
|
||||
printf("Enter student %d's grades: ", i+1);
|
||||
@@ -37,11 +42,13 @@ int main() {
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
// Array to hold average grades of students
|
||||
double average_grades[num_students];
|
||||
// Print every students average grades
|
||||
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 %d: %0.02f\n", i+1, average_grades[i]); // Formatted to two decimal places
|
||||
}
|
||||
|
||||
printf("Student with Highest Average: Student %d\n", (find_index_highest(average_grades, num_students) +1));
|
||||
|
||||
Reference in New Issue
Block a user