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 21:30
The salespeople at hyperactive media sales all use laptop computers so they can take data with them on the road. you are a salesperson for superduper lightspeed computers talking to hyperactive media sales about upgrading the laptops to windows 10. explain how network location awareness in windows 10 would make the laptops more secure.
Answers: 3
question
Computers and Technology, 22.06.2019 23:30
Select all that apply. which of the following are proofreading options included in microsoft word? spell check find replace grammar check formatting check
Answers: 1
question
Computers and Technology, 23.06.2019 08:00
The managing director of a company sends a christmas greeting to all his employees through the company email. which type of network does he use? he uses an .
Answers: 3
question
Computers and Technology, 23.06.2019 11:00
How should you specify box sizes on a web page if you want the boxes to vary according to the font size of the text they contain? a. in pixels b. in inches c. as percentages d. in em units
Answers: 2
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, 18.12.2019 22:31
question
Mathematics, 18.12.2019 22:31
question
Mathematics, 18.12.2019 22:31
Questions on the website: 13722363