Computers and Technology

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;
}

answer
Answers: 1

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 12:50, michaelchavez6959127
You have just been hired as an information security engineer for a large, multi-international corporation. unfortunately, your company has suffered multiple security breaches that have threatened customers' trust in the fact that their confidential data and financial assets are private and secured. credit-card information was compromised by an attack that infiltrated the network through a vulnerable wireless connection within the organization. the other breach was an inside job where personal data was stolen because of weak access-control policies within the organization that allowed an unauthorized individual access to valuable data. your job is to develop a risk-management policy that addresses the two security breaches and how to mitigate these risks. requirementswrite a brief description of the case study. it requires two to three pages, based upon the apa style of writing. use transition words; a thesis statement; an introduction, body, and conclusion; and a reference page with at least two references. use a double-spaced, arial font, size 12.
Answers: 1
image
Computers and Technology, 22.06.2019 19:00, dadonelson2109
If your accelerator suddenly gets stuck what should you do
Answers: 2
image
Computers and Technology, 23.06.2019 06:30, wwesuplexcity28
To become an audio technician, the most successful tactics might include the following. (select all that apply). learning how to persuade other people gaining different types of experience in audio technology learning as much as possible about art history establishing a reputation as a reliable professional
Answers: 1
image
Computers and Technology, 23.06.2019 10:20, chonawilson4
Suppose there is a relation r(a, b, c) with a b+-tree index with search keys (a, b).1. what is the worst-case cost of finding records satisfying 10 < a < 50 using this index, in terms of the number of records n1, retrieved and the height h of the tree? 2. what is the worst-case cost of finding records satisfying 10 < a < 50 and 5 < b < 10 using this index, in terms of the number of records n2 that satisfy this selection, as well as n1 and h defined above? 3. under what conditions on n1 and n2, would the index be an efficient way of finding records satisfying the condition from part (2)?
Answers: 1
Do you know the correct answer?
This program maintains a linked list of students, where each student contains a name (one-word only)...

Questions in other subjects: