Home > database >  How to format name of students when calculating avg of marks in C
How to format name of students when calculating avg of marks in C

Time:03-25

This is the code:

#include <stdio.h>

int main(){

  int i, j;
  float average;

  int grades[5][2] = {
                 {40, 45},
                 {37, 40},
                 {43, 49},
                 {33, 39},
                 {46, 45}
                 };

  printf("Let's calculate the average of the marks obtained by the students "
         "in the two subjects.\n\n");

  for(j = 0; j < 5; j  ){
    average = 0;
    for(i = 0; i < 2; i  ){
      average  = grades[j][i];
    }
    printf("The average mark scored by the student %d is -> %.2f out of 50 \n", j, average/2);
  }
}

It gives the output as: 'The average mark scored by student (index of j) is'

How would I go about making it output names of different students? example - 'The average mark scored by student John is'.

CodePudding user response:

Assuming you have an array of strings holding your student names:

...
  for(j = 0; j < 5; j  ){
    average = 0;
    for(i = 0; i < 2; i  ){
      average  = grades[j][i];
    }
    printf("The average mark scored by the student %s is -> %.2f out of 50 \n", student_names[j], average/2);
  }
}
  •  Tags:  
  • c
  • Related