Home > Software engineering >  Attempting to find the average of 3 quiz grades from 4 students (defined by user input), and I am no
Attempting to find the average of 3 quiz grades from 4 students (defined by user input), and I am no

Time:11-05

Essentially, the goal is to take 4 students and enter 3 quiz grades a student. Once you have these grades (out of 10), you take the average grade per student and display it. Though, this code outputs something of a different nature with averages that certainly do not seem correct.

#include <stdio.h>
#define NUM_STUDENTS 4
#define NUM_QUIZZES 3

int main() {


//beginning of part 2 code
    int arr_grades[NUM_STUDENTS][NUM_QUIZZES];
    int st_val, students, quizzes;
//introduce the program
    printf("\n\nHello Professor, and welcome to the grading portal.\n\n");

    for (students = 0; students < NUM_STUDENTS; students  ) {
        //this allows the entry of three quiz grades per student to be stored
        printf("Please enter 3 quiz grades for student %d: ", students 1);
        for (quizzes = 0; quizzes < NUM_QUIZZES; quizzes  )
            scanf("%d", &arr_grades[students][quizzes]);
    }
//here we will be getting the average of our inputs
    printf("\nAverage Quiz Grades\n");
    for (students = 0; students < NUM_STUDENTS; students  ) {
        printf("Student %d's average is: ", students 1);
        //initialize
        st_val = 0;
        //this is the next look that will mimic the same for quizzes then provide       us with the final result
        for (quizzes = 0; quizzes < NUM_QUIZZES; quizzes  )
            st_val  = arr_grades[students][quizzes];
            printf("=\n", st_val / NUM_STUDENTS);
}

Input:

Please enter 3 grades for student 1:  10 10 10
Please enter 3 grades for student 2:  2 0 1
Please enter 3 grades for student 3:  8 6 9
Please enter 3 grades for student 4:  8 4 10

Wrong Output:

    Average Quiz Grades
Student 1's Average is:   7
Student 2's Average is:   0
Student 3's Average is:   5
Student 4's Average is:   5

Expected Output:

    Average Quiz Grades
Student 1's Average is:   10.0
Student 2's Average is:   1.0
Student 3's Average is:   7.7
Student 4's Average is:   7.3

CodePudding user response:

That's not how you calculate the average. Divide by the number of quizzes per student not by the number of students.

In addition, %d will display the average in a signed integer format, therefore you will lose precision (you are dividing two integers, the result will be an integer). You can use %lf to display the result as a double.

Change this line:

printf("=\n", st_val / NUM_STUDENTS);

to:

printf("%.3lf\n",(double) st_val / NUM_QUIZZES);

It's also a good idea to check the return value of scanf(), you can't be sure if it succeeded.

  • Related