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: 3672233 • 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

Note:

          The required method code is added in the given code.

Program:


#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;
   struct student *qtr1;
   follow=(struct student*) malloc(sizeof(struct new_student));
   if (list == NULL) // inserting into an empty list
   {  
           follow->next=list;
           list=follow;
           strcpy(follow->firstName, (*new_student).firstName);
           strcpy(follow->lastName, (*new_student).lastName);
           follow->level=(*new_student).level;
           follow->grade=(*new_student).grade;
           return;
   }
   else if(list->grade<follow->grade && list!=NULL)
   {
       follow->next=list;
       list=follow;
       strcpy(follow->firstName, (*new_student).firstName);
       strcpy(follow->lastName, (*new_student).lastName);
       follow->level=(*new_student).level;
       follow->grade=(*new_student).grade;      
       return;
   }  
   else if(list->grade>follow->grade && follow->next !=NULL)
   {
       qtr1->next=follow;
       follow->next=qtr1->next;
       strcpy(follow->firstName, (*new_student).firstName);
       strcpy(follow->lastName, (*new_student).lastName);
       follow->level=(*new_student).level;
       follow->grade=(*new_student).grade;
       return;
   }
   else
   {
       follow->next=0;
       qtr1->next=follow;
       strcpy(follow->firstName, (*new_student).firstName);
       strcpy(follow->lastName, (*new_student).lastName);
       follow->level=(*new_student).level;
       follow->grade=(*new_student).grade;
       return;      
   }
}

// 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;
   while(temp!=NULL){
       if(strcmp(student->lastName, temp->lastName)==0){
           return temp;
       }
       else{
           temp=temp->next;
       }
   }
   return NULL;
}
// 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()
{
   int cnt=0;
   double sum,avg;
   struct student* temp = list;
   while(temp!=NULL)
   {
       sum=sum+temp->grade;
       temp=temp->next;
       cnt++;
   }
   avg=sum/cnt;
   return avg;
}

// 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)
{
   struct student *pt1;
   struct student *pu1;
       if(list==0){
       return;
   }
   pt1=search(student);
   if(pt1==0){
       pu1=list;
       list=list->next;
       free(pu1);
       return;
   }
   if(pt1->next->next==0){
       pu1=pt1->next;
       pt1->next=0;
       free(pu1);
       return;
   }
   else{
       pu1=pt1->next;
       pt1->next=pu1->next;
       free(pu1);
       return;
   }
}