Please assist in correcting the code below per the feedback

Please assist in correcting the code below per the feedback of -The data struct is backward: rather than having a struct that consists of arrays, you need to have an array of structs, where each struct holds the ID number and score. This will also make it possible to keep each grade associated with the correct ID when the program sorts the entries by ID number. You have not implemented this sort in your code. Remember that you are sorting data in an array of structs rather than simple values in an array.

ORIGINAL INSTRUCTIONS

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.

#include <stdio.h>

#include <stdlib.h>

//Creating the GradeBook Structure

struct gradeBook {

   //Declaring the student\'s Id array and Grade array

int id[100];

int grade[100];

} gradbk;

int main( )

{

//initialize variables

int i=0, j=0;

int id;

//infinite loop which will stop when user enters -1 as id value

while(id != -1)

{

   //Getting the id entered by the user

   printf(\"Enter student id number (Enter -1 to stop):\");

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

//if user entered id is not -1

if(id != -1)

{

   //Populating the id into the id array

       gradbk.id[i]=id;

       //Getting the grade entered by the user

   printf(\"Enter grade percentage (0-100) \");

//read grade

scanf(\"%d\",&gradbk.grade[i]);

i++;

continue;

}

//if user entered -1, then exit this loop

else

{

break;

}

}

//Displaying all the students id\'s and grades

printf(\"\ \ Displaying the Students ID\'s and Grades : \ \");

//loop which will iterate till no:of user entered grades

for(j=0; j<i; j++)

{

//print the students id\'s and grade

printf(\"\ Student id : %d \",gradbk.id[j]);

printf(\"\ Grade is : %d \",gradbk.grade[j]);

}

return 0;

}

Solution

#include <stdio.h>

#include <stdlib.h>

//Creating the GradeBook Structure

struct gradeBook {

   //Declaring the student\'s Id array and Grade array

int id;

int grade;

} gradbk[100]; //array of structures

int main( )

{

//initialize variables

int i=0, j=0;

int id,n;
struct gradeBook temp;

//infinite loop which will stop when user enters -1 as id value

while(id != -1)

{

   //Getting the id entered by the user

   printf(\"\ Enter student id number (Enter -1 to stop):\");

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

//if user entered id is not -1

if(id != -1)

{

   //Populating the id into the id array

       gradbk[i].id=id;

       //Getting the grade entered by the user

   printf(\"\ Enter grade percentage (0-100) \");

//read grade

scanf(\"%d\",&gradbk[i].grade);

i++;

continue;

}

//if user entered -1, then exit this loop

else

{

break;

}

}


n= i;

//Displaying all the students id\'s and grades

printf(\"\ \ Displaying the Students ID\'s and Grades : \ \");

//loop which will iterate till no:of user entered grades

for(j=0; j<n; j++)

{

//print the students id\'s and grade

printf(\"\ Student id : %d \",gradbk[j].id);

printf(\"\ Grade is : %d \",gradbk[j].grade);

}

// sorting the structure on student id using bubble sort

for(i=0;i<n-1;i++) //n-1 passes
{
    for(j=0;j<n-1-i;j++)
    {
        if(gradbk[j].id>gradbk[j+1].id)   // compare consective values
        {
            temp = gradbk[j];
            gradbk[j] = gradbk[j+1];              //swap values using temp structure variable
            gradbk[j+1] = temp;
        }
    }
}
printf(\"\ Sorted student records\");
//print out the ID numbers and grades entered by the user, sorted by ID number.
for(j=0; j<n; j++)

{

//print the students id\'s and grade

printf(\"\ Student id : %d \",gradbk[j].id);

printf(\"\ Grade is : %d \",gradbk[j].grade);

}

return 0;

}

output:

Please assist in correcting the code below per the feedback of -The data struct is backward: rather than having a struct that consists of arrays, you need to ha
Please assist in correcting the code below per the feedback of -The data struct is backward: rather than having a struct that consists of arrays, you need to ha
Please assist in correcting the code below per the feedback of -The data struct is backward: rather than having a struct that consists of arrays, you need to ha
Please assist in correcting the code below per the feedback of -The data struct is backward: rather than having a struct that consists of arrays, you need to ha

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site