Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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’.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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);
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading