Modify the grade book program to use a custom struct to hold
Modify the grade book program to use a custom struct to hold the student\'s ID number and the percentage score for each item in the grade book. The program should accept the entry of ID numbers and percentage grades (0–100) until the user signals that he or she is done entering grades. The program should then print out the ID numbers and grades entered by the user, sorted by ID number. Again, be sure to format the code properly and provide comments in the C code.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char choice;
int percentArray[50];
int percentage;
int count=0,i;
for(count=0; count<50; count++)
{ //for loop to read the grades till array size
printf(\"-------------------Enter percentage---------------\ Add marks(a) \ Quit(q) \ \");
scanf(\"%c\",&choice);
if(choice == \'a\')
{ //if user choice is a, then read the grade
printf( \"Enter grade:\");
scanf(\"%d\", &percentage);
percentArray[count] = percentage; //add the grade to array
}
if(choice == \'q\') //if the user choice is q, then exit the loop
{
break;
}
}
printf(\"Grades are: \");
for(i=0; i<count; i++)
{
printf(\"%d \", percentArray[i]); //print grades
}
return 0;
}
Solution
#include <stdio.h>
#include <stdlib.h>
//creating struct for storing student id and percentage
struct student
{
int stuid;
int per;
} s[50];
//main program started
int main()
{
//declaring variables
char choice;
int count,i,j;
int n;
for(count=0; count<50; count++)
{ //for loop to read the grades till array size
printf(\"-------------------sorting ---------------\ Sorting(s) \ Quit(q) \ \");
scanf(\"%c\",&choice);
if(choice == \'s\')
{ //if user choice is s, then read the id number and percentage
printf( \"Enter id number\");
scanf(\"%d\", &s[count].stuid);
printf( \"Enter grade:\");
scanf(\"%d\", &s[count].per);
//while loop for entering valid score
while(s[count].per>=0 && s[count].per<=100)
{
printf( \"please enter a valid score between 0 to 100\");
scanf(\"%d\", &s[count].per);
}
}
if(choice == \'q\') //if the user choice is q, then exit the loop
{
break;
}
}
n=count;
//sorting struct based on student id number
for (i = 1; i < n; i++)
for (j = 0; j < n - i; j++)
{
if(s[j].stuid> s[j+1].stuid)
{
temp = s[j].stuid;
s[j].stuid = s[j+1].stuid;
s[j+1].stuid = temp;
}
printf(\"student id and average scores are: \");
for(i=0; i<count; i++)
{
printf(\"%d %d\", s[count].stuid, s[count].per);
}
return 0;
}


