Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming Assignment (50 points) You are given a partially completed program h

ID: 3671883 • Letter: P

Question

Programming Assignment (50 points)

You are given a partially completed program hw06.c. Your job is to follow the instructions given in the program to complete the functions so that the program executes properly. A user is given the option to input a new student, delete a student, search for the grade of a student, or determine the class average of the students on the list. A good portion of the code is already implemented for you. The best way to start this assignment would be to start in the main and trace through the helper function and understand how the code works.

Add

When inputting a new student, the user is prompted for the student’s first name, last name, grade in the class (an integer between 0 and 100), and education level. The add() function is worth 15 points and has specific instructions on how the list is to be sorted. For simplicity, in this assignment you are to assume that no students on the list will have the same last name. However, you still should not allow for a student to be added to the list twice. This means that you will only need to check the last names of the students on the list and compare them to the last name of the student being added to determine if the student is already on the list. Please follow these instructions very carefully while writing this function and notice the return type.

Search

When searching for a student’s grade, you will be prompted for the student’s last name. You will use this last name to search for the student on the list and you will be returning a pointer to that student. Please follow these instructions very carefully.

Average

This function simply traverses your linked list and returns the average of all of the students’ grades on the list. Notice that the return type is of type double.

Delete One

When deleting a student from the list, you will be prompted for the student’s last name. You will use this last name to search for the student on the list and remove them. You will need to do so in a way that does not break your linked list and ensures no memory leaks. You will notice that the search function is called before this function, therefore, you are to assume that the student that you are looking for is on the list. Please follow these instructions very carefully.

sample input / output

The sample input / output given on the following pages tests a few of the requirements for your functions in hw06. As you can see, the function should add the names to your list in alphabetical order by last name. Make sure that you account for what is to be returned in the list is empty or if the requested student does not exist on the list. All of the pictures below contain different executions of this program and are not related. The list is not saved. All of the output is already implemented for you, the only required implementation is in the function. Please follow all directions carefully in the hw06.c file.

// READ BEFORE YOU START:

// You are given a partially completed program that creates a roster of students for a class.

// Each student has the corresponding information: grade in the class and education level.

// To begin, you should trace through the given code and understand how it works.

// Please read the instructions above each required function and follow the directions carefully.

// If you modify any of the given code, the return types, or the parameters, you risk failing the automated test cases.

//

// You are to assume that all input is valid:

// Valid first name: String containing alphabetical letters beginning with a capital letter

// Valid last name: String containing alphabetical letters beginning with a capital letter

// Valid grade input: any integer value between 1 and 100 (error handling is implemented)

// Valid education level input : f, so, j, or s. (error handling is implemented)

// All input will be a valid length and no more than the allowed amount of memory will be input

#include <stdio.h>

#include <string.h>

#include <ctype.h>

#pragma warning(disable : 4996)

typedef enum { freshman = 0, sophomore, junior, senior } education; // enumeration type education level

struct student {

char firstName[100];

char lastName[100];

   education level;

int grade;

struct student* next;

} *list = NULL;

// forward declarations

void flush();

void branching(char c);

void helper(char c);

// These have been ordered in a way of suggested completion.

//You will need to complete search() before you start delete_one()

int add(struct student*);                       // 15 points

double average();                               // 10 points

struct student* search(struct student*);       // 10 points

void delete_one(struct student*);               // 15 points

int main()

{

char ch = 'i';

   printf("Student Roster: ");

do {

       printf("Please enter your selection ");

       printf(" a: add a new student to the list ");

       printf(" d: delete a student from the list ");

       printf(" s: search for student by name ");

       printf(" r: determine the class average ");

       printf(" q: quit ");

       ch = tolower(getchar());

       flush();

       branching(ch);

   } while (ch != 'q');

return 0;

}

// consume leftover ' ' characters

void flush()

{

int c;

do c = getchar(); while (c != ' ' && c != EOF);

}

// branch to different tasks

void branching(char c)

{

switch (c) {

case 'a':

case 'd':

case 's':

case 'r':

       helper(c);

       break;

case 'q':

       break;

default:

       printf(" Invalid input! ");

   }

}

// The helper function is used to determine how much information is needed and which function to send that information to.

// It uses pointers that are returned from some functions to produce the correct ouput.

// There is no implementation needed here, but you should study this function and know how it works.

// It is always helpful to understand how the code works before implementing new features.

// Do not change anything in this function or you risk failing the automated test cases.

void helper(char c) {

char student_firstName[100];

char student_lastName[100];

char* student_level = (char*)malloc(100);

int valid_level = -1; // used to determine if the input education level is acceptable

int tempI; // temporary integer

double tempD; // temporary double

// create new temporary pointers

struct student *ptr = (struct student *)malloc(sizeof(struct student));

if (c == 'r')

   {

       tempD = average(); // compute class average

       if (tempD < 0) // tempD should equal -1 if the list is empty

       {

           printf(" There are no students on the list. ");

           return;

       }

       printf(" The class average is: %.2f ", tempD);

       return;

   }

else if (c == 'a')

   {

       printf(" Enter the student's first name: ");

       fgets(student_firstName, sizeof(student_firstName), stdin);

       printf(" Enter the student's last name: ");

       fgets(student_lastName, sizeof(student_lastName), stdin);

       // discard ' ' chars attached to input; NOTE: If you are using GCC, you may need to comment out these 2 lines

       student_firstName[strlen(student_firstName) - 1] = '';

       student_lastName[strlen(student_lastName) - 1] = '';

       // stores the first name and last name of the student into pointer

       strcpy(ptr->firstName, student_firstName);

       strcpy(ptr->lastName, student_lastName);

       printf(" Enter the student's grade: ");

       tempI = -1;

       while (tempI < 0 || tempI > 100)

       {

           scanf("%d", &tempI);

           if (tempI >= 0 && tempI <= 100)

               ptr->grade = tempI; // stores the grade of the student into pointer

           else

               printf("Please enter a grade between 0 and 100. ");

       }

       printf(" Enter the student's education level (f/so/j/s): ");

       // stores the education level of the student into pointer

       while (valid_level < 0)

       {

           scanf("%s", student_level);

           if (strcmp(student_level, "f") == 0)

           {

               valid_level++;

               ptr->level = freshman;

           }

           else if (strcmp(student_level, "so") == 0)

           {

               valid_level++;

               ptr->level = sophomore;

           }

           else if (strcmp(student_level, "j") == 0)

           {

               valid_level++;

               ptr->level = junior;

           }

           else if (strcmp(student_level, "s") == 0)

           {

               valid_level++;

               ptr->level = senior;

           }

           else printf("Please enter a valid education level (f/so/j/s). "); // error handling

       }

       // always set next equal to NULL before adding to a list

       // if it is placed at the end of the list, you know where to stop traversing

       ptr->next = NULL;

       tempI = add(ptr); // add student (should return 0 if the student is already on the list)

       if (tempI == 0) printf(" %s is already on the list. ", student_lastName);

       else printf(" %s added to the list. ", student_lastName);

       flush();

   }

else

   {

       printf(" Enter the student's last name: ");

       fgets(student_lastName, sizeof(student_lastName), stdin);

       // discard ' ' char attached to input; NOTE: If you are using GCC, you may need to comment out this line

       student_lastName[strlen(student_lastName) - 1] = '';

       // stores the last name of the student into pointer

       strcpy(ptr->lastName, student_lastName);

       struct student *temp = search(ptr); // search for student in list

       if (temp == NULL) // student not found

       {

           printf(" %s not found. ", student_lastName);

           return;

       }

       if (c == 's')

       {

           printf(" Current grade for %s: %d ", temp->lastName, temp->grade);

           return;

       }

       delete_one(ptr); // delete student

       printf(" %s deleted from the list. ", student_lastName);

       return;

   }

}

// Q1: add (15)

// Similar to hw05, you will be inserting into a list of students sorted by their last name.

// Differing from hw05, there is no limit to how many students can be in this list.

// For this assignment, you can also assume that no 2 students will have the same last name.

// NOTE: You still need to check if the input student already exists on the list,

// however, no 2 students will have the same last name and differing first names.

//

// This means that if the last name of the "new_student" matches the last name of a student on the list,

// then it is enough to assume that the student is already on the list and should not be added.

// To clarify, you will be tested to assure that a student is not added to the list twice.

//

// If the student is already on the list, return the integer value 0.

// If the student is not on the list, add the student to the list and return the integer value 1.

//

// "list" is initialized as NULL. Use this as your 'head' of the list and insert into it accordingly.

// There are 4 possibilities for inserting into the list:

//   - inserting into an empty list

//   - inserting at the beginning of the list

//   - inserting inbetween 2 nodes in the list

//   - inserting at the end of the list

int add(struct student* new_student)

{

struct student* follow = list;

}

// Q2: search (10)

// In this function, you are passed a struct student* parameter named 'student' to find their current grade.

// Stored inside this pointer, is the last name of the student that you want to find in your list (student->lastName)

// You need to return a pointer to a node in your list that contains that student's information (that will include student->grade)

// To find that pointer, you need to traverse your lise. If that student does not exist in your list, you must return NULL.

// (You must return a pointer to a node in your list. Do not create a pointer that just includes the grade, you will risk losing points)

// (Remember that it is enough search for a student by their last name since no 2 students will have the same last name)

struct student* search(struct student* student)

{

struct student* temp = list;

}

// Q3: average (10)

// In this function, you need to return the average of all of the students' grades that exist on the list.

// To do this you will need to traverse your list and access each student's grade.

// If there are no students on the list, you should return -1 (this will be tested).

double average()

{

struct student* temp = list;

}

// Q4: delete_one (15)

// In this function, you are passed a struct student* parameter named 'student' to delete the corresponding student on your list.

// Stored inside this pointer, is the last name of the student that you want to find in your list (student->lastName)

// The search function is called before this function to check if the student exists, you can assume that the student is on the list.

// You will need to find the student and delete it using proper memory management to ensure no memory leaks.

// (Remember that it is enough search for a student by their last name since no 2 students will have the same last name)

void delete_one(struct student* student)

{

}

Student Roster Please enter your selection a: add a new student to the list d: delete a student from the list s search for student by name r: determine the class average q: quit Enter the student's last name: Lennon Lennon not found Please enter your selection a: add a new student to the list d: delete a student from the list s: search for student by name r: determine the class average q: quit Enter the student's last name Lennon Lennon not found Please enter your selection a: add a new student tothe list d: delete a student fromthe list s: sea r: determine the class average q: quit rch for student by name There are no students on the list. Please enter your selection a: add a new student to the list d: delete a student from the list s search for student by name r: determine the class average q:quit

Explanation / Answer

// READ BEFORE YOU START: // You are given a partially completed program that creates a roster of students for a class. // Each student has the corresponding information: grade in the class and education level. // To begin, you should trace through the given code and understand how it works. // Please read the instructions above each required function and follow the directions carefully. // If you modify any of the given code, the return types, or the parameters, you risk failing the automated test cases. // // You are to assume that all input is valid: // Valid first name: String containing alphabetical letters beginning with a capital letter // Valid last name: String containing alphabetical letters beginning with a capital letter // Valid grade input: A+, A, A-, B+, B, B-, ... // Valid education level input : f, so, j, or s. // All input will be a valid length and no more than the allowed number of students will be added to the list #include #include #include #pragma warning(disable: 4996) typedef enum { freshman = 0, sophomore = 1, junior = 2, senior = 3 } education; // enumeration type education level struct student { char firstName[100]; char lastName[100]; char grade[30]; education level; }; int count = 0; // the amount of students currently stored in the list (initialized at 0) struct student list[30]; // initialize list of students // forward declaration of functions void flush(); void branching(char); void read(); void add(char*, char*, char*, char*, struct student*); void display(); void save(char* fileName); void load(char* fileName); int main() { load("Student_List.txt"); // load list of students from file char ch = 'i'; printf("Student Roster: "); do { printf("Please enter your selection: "); printf(" a: add a new student "); printf(" d: display students "); printf(" q: quit and save your list "); ch = tolower(getchar()); flush(); branching(ch); } while (ch != 'q'); save("Student_List.txt"); // save list of students to file return 0; } // consume leftover ' ' characters void flush() { int c; do c = getchar(); while (c != ' ' && c != EOF); } // branch to different tasks void branching(char c) { switch (c) { case 'a': read(); break; case 'd': display(); break; case 'q': break; default: printf("Invalid input! "); } } // This function is already implemented for you. It prompts for and stores a student along with their grade and education level. // It then calls the add() function (which is to be implemented) sending over those parameters (along with list). void read() { char student_firstName[100]; char student_lastName[100]; char student_grade[30]; char student_level[100]; printf(" Enter the student's first name: "); fgets(student_firstName, sizeof(student_firstName), stdin); printf(" Enter the student's last name: "); fgets(student_lastName, sizeof(student_lastName), stdin); printf(" Enter the student's grade (A+,A,A-,...): "); fgets(student_grade, sizeof(student_grade), stdin); printf(" Enter the student's education level (f/so/j/s): "); fgets(student_level, sizeof(student_level), stdin); // discard ' ' chars attached to input; NOTE: If you are using GCC, you may need to comment out these 4 lines student_firstName[strlen(student_firstName) - 1] = ''; student_lastName[strlen(student_lastName) - 1] = ''; student_grade[strlen(student_grade) - 1] = ''; student_level[strlen(student_level) - 1] = ''; add(student_firstName, student_lastName, student_grade, student_level, list); printf(" "); // newline for formatting } // Q1: add (40) // This function is used to insert a new student into the roster. // Your list should be sorted alphabetically by last name, so you need to search for the correct index to add into your list. // If a student already exists with the same last name, then you will need to sort them by their first names. // Do not allow for the same student to be added to the list multiple times. (same first name and same last name). // // NOTE: You must convert the char pointer "student_level" to an enum type and store it in the list. This will be tested. // (You must store all of the required information correctly to pass all of the test cases) // NOTE: You should not allow for the same student to be added twice, you will lose points if you do not account for this. // (That means that students on the list are allowed to have the same first names OR the same last names, but not both) // Again, you will lose points if you do not account for these cases. // // It may be helpful to print "Student added!" or "This student is already on the list!" at the correct places for debugging purposes. // // You are not required to use pointer operations for your list but you may do so if you'd like. // 'list' is passed to this function for testing purposes only, it is global void add(char* student_firstname, char* student_lastname, char* student_grade, char* student_level, struct student* list) { char *currLastName = student_lastname; //create storage for student_lastname char *currFirstName = student_firstname; //create storage for student_firstname struct student *pointer = list; //create a pointer to list for referencing education sLevel; // create an enum for education int i, j; //count increments for (i = 0; i < 30; i++) // Start of algorithm for checking for no duplicate names and adds student { if (((strcmpi((pointer + i)->lastName, currLastName)) == 0) && ((strcmpi((pointer + i)->firstName, currFirstName)) == 0)) //Checks for duplicate last name and first name { printf("Duplicate Student "); // Prints if a duplicate sudent was entered break; } if ((*(pointer + i)->lastName) == '') // If last name is not reoccurring then add student into null slot. { int dup = 0; //in order to not get it to overide sophomore with senior, i created a second check char *soph = "so"; // in order to check 'so' i needed to create a string pointer so it read more than just 's' strcpy((pointer + i)->firstName, student_firstname); //Adds first name into the structure i strcpy((pointer + i)->lastName, currLastName); //Adds last name into the structure i strcpy((pointer + i)->grade, student_grade); //Adds grade into the structure i if (*student_level == 'f') //Series of if statements to determine what to set enum as and adds to structure i { sLevel = freshman; // sets enum equal to value freshman (pointer + i)->level = sLevel; } if (strcmpi(student_level, "so") == 0) //string compare so it won't just look at the first character { sLevel = sophomore; // sets enum equal to value sophomore (pointer + i)->level = sLevel; dup++; // inc dup variable so it doesn't go into senior check } if (*student_level == 'j') // checks junior { sLevel = junior; // sets enum equal to value junior (pointer + i)->level = sLevel; } if ((*student_level == 's') && (dup == 0)) //checks senior if conditions are met { sLevel = senior; // sets enum equal to value senior (pointer + i)->level = sLevel; dup++; } count++; //increment count because a student was added to the struct break; //stop loop } } //selection sort algorithm here for (i = 0; i lastName); // last name comparison int y = strcmpi((pointer+i)->firstName, (pointer+j)->lastName); //first name comparison if (x > 0) { struct student temp; //temporary struct for swap temp = pointer[i]; // temp = spot 1 pointer[i] = pointer[j]; // spot 1 = spot 2 pointer[j] = temp; // spot 2 = spot 1 } if (x == 0) // if last name is same go inside { if (y > 0) // if first name comes before go inside { struct student temp; //temporary struct for swap temp = pointer[i]; // temp = spot 1 pointer[i] = pointer[j]; // spot 1 = spot 2 pointer[j] = temp; // spot 2 = spot 1 } } } } } // Q2: display (10) // Traverse your list and print the students the required format; see word document for output format. // Your output must match the expected output format or you risk losing points. // If the list is empty, print: "There are no students on this roster! " // (hint: Use can use your global integer value "count" to see how many students are in your list) void display() { if (count == 0) { printf(" There are no students in this roster! "); } int i; for (i = 0; i