The file datatxt holds the names and grades for the students

The file data.txt holds the names and grades for the students in a math class. Your program will calculate and display the average for each student, as well as their highest grade, and calculate and display the average for the whole class, as well as the highest grade.

• The number of grades for each student is unknown.

• The last ‘grade’ for each student will be -1, which is not an actual grade.

• The number of students is unknown. You must keep reading in student data until there is no more

• The class average is the average of the student’s averages.

• If a student has no grades (except for the -1), their average is 0.

• If there are no students in the class, the class average is 0.

Hint: You should not try to read in all the data from the file before doing any calculations or output. Much of it can be done one student at a time. Also, you will need to use a ‘nested’ loop, that is, a loop within a loop.

For example, if the data file contains the following data:

Sue Storm 95 76 89.3 99.2 91 -1

Reed Richards 76 85.5 89.25 -1

Bruce Banner 54.5 67.8 75 88.3 -1

The output will look like this:

The average for Sue Storm is: 90.1 and the high grade is: 99.2

The average for Reed Richards is: 83.6 and the high grade is: 89.3

The average for Bruce Banner is: 71.4 and the high grade is: 88.3

The average for the class is: 81.7 and the highest grade for the class is: 99.2

Solution

#include<stdio.h>

#define Size n

struct student {

char name[30];

   int rollno;

   int sub[3];

};

void main() {

   int i, j, max, count, avg, n, a[SIZE], ni;

   struct student st[SIZE];

   clrscr();

   printf(\"Enter how many students: \");

   scanf(\"%d\", &n);

   /* for loop to read the names and roll numbers*/

   for (i = 0; i < n; i++) {

      printf(\"\ Enter name and roll number for student %d : \", i);

      scanf(\"%s\", &st[i].name);

      scanf(\"%d\", &st[i].rollno);

   }

   /* for loop to read ith student\'s jth subject*/

   for (i = 0; i < n; i++) {

      for (j = 0; j <= 2; j++) {

         printf(\"\ Enter marks of student %d for subject %d : \", i, j);

         scanf(\"%d\", &st[i].sub[j]);

      }

   }

   /* (i) for loop to calculate avg marks obtained by each student*/

   for (i = 0; i < n; i++) {

      total = 0;

      for (j = 0; j < 3; j++) {

avg = avg + st[i].sub[j];

      }

      printf(\"\ avg marks obtained by student %s are %dn\", st[i].name,avg);

      a[i] = avg;

   }

   /* (ii) for loop to list out the student\'s roll numbers who

    have secured the highest marks in each subject */

   /* roll number who secured the highest marks */

   for (j = 0; j < 3; j++) {

      max = 0;

      for (i = 0; i < n; i++) {

         if (max < st[i].sub[j]) {

            max = st[i].sub[j];

            ni = i;

         }

      }

      printf(\"\ Student %s got maximum marks = %d in Subject : %d\",st[ni].name, max, j);

   }

   max = 0;

   for (i = 0; i < n; i++) {

      if (max < a[i]) {

         max = a[i];

         ni = i;

      }

   }

   printf(\"\ %s obtained the total highest marks.\", st[ni].name);

   getch();

}

char name[30];

   int rollno;

   int sub[3];

};

void main() {

   int i, j, max, count, avg, n, a[SIZE], ni;

   struct student st[SIZE];

   clrscr();

   printf(\"Enter how many students: \");

   scanf(\"%d\", &n);

   /* for loop to read the names and roll numbers*/

   for (i = 0; i < n; i++) {

      printf(\"\ Enter name and roll number for student %d : \", i);

      scanf(\"%s\", &st[i].name);

      scanf(\"%d\", &st[i].rollno);

   }

   /* for loop to read ith student\'s jth subject*/

   for (i = 0; i < n; i++) {

      for (j = 0; j <= 2; j++) {

         printf(\"\ Enter marks of student %d for subject %d : \", i, j);

         scanf(\"%d\", &st[i].sub[j]);

      }

   }

   /* (i) for loop to calculate avg marks obtained by each student*/

   for (i = 0; i < n; i++) {

      total = 0;

      for (j = 0; j < 3; j++) {

avg = avg + st[i].sub[j];

      }

      printf(\"\ avg marks obtained by student %s are %dn\", st[i].name,avg);

      a[i] = avg;

   }

   /* (ii) for loop to list out the student\'s roll numbers who

    have secured the highest marks in each subject */

   /* roll number who secured the highest marks */

   for (j = 0; j < 3; j++) {

      max = 0;

      for (i = 0; i < n; i++) {

         if (max < st[i].sub[j]) {

            max = st[i].sub[j];

            ni = i;

         }

      }

      printf(\"\ Student %s got maximum marks = %d in Subject : %d\",st[ni].name, max, j);

   }

   max = 0;

   for (i = 0; i < n; i++) {

      if (max < a[i]) {

         max = a[i];

         ni = i;

      }

   }

   printf(\"\ %s obtained the total highest marks.\", st[ni].name);

   getch();

}

The file data.txt holds the names and grades for the students in a math class. Your program will calculate and display the average for each student, as well as
The file data.txt holds the names and grades for the students in a math class. Your program will calculate and display the average for each student, as well as
The file data.txt holds the names and grades for the students in a math class. Your program will calculate and display the average for each student, as well as
The file data.txt holds the names and grades for the students in a math class. Your program will calculate and display the average for each student, as well as
The file data.txt holds the names and grades for the students in a math class. Your program will calculate and display the average for each student, as well as

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site