subject

This program maintains a linked list of students, where each student contains a name (one-word only) and a GPA. The program operates as follows: Prompts the user for names and GPAs, building the list with calls to add, printing the list after each add
Prompts the user for a name, and then prints the GPA of that student (-1 if the name is not in the list)
You must complete the following:
Write the function getGPA that returns the gpa of the name passed to it, or -1.0 if the name doesn’t exist.
Re-write add so that all the students will be ordered by descending GPA. For those with the same GPA, they will be ordered ascending alphabetically.
Do not add a name if it already exists in the list and print the error message: "Student named %s exists.\n"
It is recommended to write a helper function compareStudents that compares two students and returns -1, 1 or 0, depending on the positions of the two students in the sorted linked list.
Two sample inputs for the program are shown below:
Fred 3.5
Barney 2.4
Betty 3.9
Wilma 3.5
xxx 0
Fred
Betty
xxx
Betty 3.5
Fred 2.5
Fred 2.9
Wilma 2.9
Wilma 3.0
xxx 0
Pebbles
xxx
Code :
#include
#include
#include
typedef struct _student {
char *name;
double gpa;
struct _student *next;
} Student;
Student *createStudent(char name[], double gpa, Student *next) {
Student *pNew = malloc(sizeof(Student));
pNew->name = malloc(strlen(name) + 1);
strcpy(pNew->name, name);
pNew->gpa = gpa;
pNew->next = next;
return pNew;
}
int compareStudents(Student *pNew, Student *pExisting) {
//code here
return -1;
}
Student *add(Student *head, char name[], double gpa) {
//code here
return createStudent(name, gpa, head);

}
void print(char prefix[], Student *head) {
printf("%s", prefix);
for (Student *ptr = head; ptr != NULL; ptr = ptr->next)
printf("[%s-%g] ", ptr->name, ptr->gpa);
printf("\n");
}
double getGPA(Student *head, char name[]) {
//code here
return -1.0;
}
int main(void) {
char name[100];
double gpa;
Student *head = NULL;
while (1) { // build the list
printf("\nEnter a name (one word) and gpa, or xxx and 0 to exit: ");
scanf("%s%lf", name, &gpa);
if (strcmp(name, "xxx") == 0) break;
head = add(head, name, gpa);
print("\n\tNow the list is: ", head);
}
while (1) { // perform search
printf("\nEnter a name to look up the gpa or xxx to exit: ");
scanf("%s", name);
if (strcmp(name, "xxx") == 0) break;
gpa = getGPA(head, name);
if (gpa < 0)
printf("\n\t%s does not exist\n", name);
else
printf("\n\t%s has a GPA of %g\n", name, gpa);
}
return 0;
}

ansver
Answers: 1

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 06:30
Exchanging which type of data uses the least bandwidth? music photographs video voice bandwidth- the amount of data that can be moved between two points in a set time period
Answers: 1
question
Computers and Technology, 22.06.2019 11:30
Andrina writes letters that are regularly sent to hundreds of her company’s customers. because of this, she would like for the mail merge command to be in her quick access toolbar, and she wants it to be the first button on the left. what should andrina do to place the mail merge button there?
Answers: 1
question
Computers and Technology, 24.06.2019 10:10
Scanning the road can be thought of as a
Answers: 2
question
Computers and Technology, 24.06.2019 12:30
Do you think media is stereotype ? and why?
Answers: 1
You know the right answer?
This program maintains a linked list of students, where each student contains a name (one-word only)...
Questions
question
Mathematics, 17.07.2020 18:01
question
Biology, 17.07.2020 18:01
question
Health, 17.07.2020 18:01
question
Mathematics, 17.07.2020 18:01
question
Mathematics, 17.07.2020 18:01
question
Mathematics, 17.07.2020 18:01
question
Mathematics, 17.07.2020 18:01
Questions on the website: 13722360