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

Write a menu based C program to maintain student records. Your program should ta

ID: 3686262 • Letter: W

Question

Write a menu based C program to maintain student records. Your program should take the following inputs:

1. Student first name (max. 20 characters)

2. Student last name, (max. 20 characters)

3. Student scores (float/double), eg. 85.4

Your program should be able to take records of a minimum of 5 students. After taking the records, you should provide 8 functionalities to the user.

1. Print records – prints records of all students

2. Add a new record – take a new record from the user for a new student. Be careful, you may need to allocate more memory using dynamic memory allocation.

3. Delete record(s) – to delete a record, ask for the last name of the student from the user. If there are multiple students with same last name, you must delete all of their records. You must free up the memory holding these records.

4. Search by last name – prints record of the student with a given last name. If there are multiple students with the same last name, print records for all of them.

5. Sort by score – sort the records of students according to their scores, and then print the sorted records.

6. Sort by last name – sort the records of students according to their names alphabetically, and then print the sorted records.

7. Find the median score – compute the median score and print it. Also, print how many students are above this median score.

8. Exit the program – terminate on a specific input from the user. Let that specific input be an integer of value 0.

You should print the record in the following format:

First Name: firstname 1, Last Name: lastname 1, Score: score 1

First Name: firstname 2, Last Name: lastname 2, Score: score 2

.

.

.

You should write each functionality from 1-7 in separate functions. You should provide a menu to the user as following:

For inputs:

Please indicate number of records you want to enter (min 5):

#of records

After user gives the number of records, you should inform the user how to enter the records:

Please input records of students (enter a new line after each record), with following format

first name last name score

After user gives the inputs for the records, inform the user about the functionalities:

Print records (press 1)

Add a new record (press 2)

Delete record(s) (press 3)

Search by last name (press 4)

Sort by score (press 5)

Sort by last name (press 6)

Find median score (press 7)

Exit the program (press 0)

After user chooses a functionality, your program performs that and provides this menu again to select another functionality. This goes on until user presses 0.

Explanation / Answer

Answer:

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

struct nodeStuRecord

{

char stuLastName[20];

char stuFirstName[20];

double stuScore;

struct nodeStuRecord *nextRec;

};

void addNewRecord(struct nodeStuRecord** refHead, char stuFirstName[],char stuLastName[],double stuScore)

{

struct nodeStuRecord* new_nodeStuRecord = (struct nodeStuRecord*) malloc(sizeof(struct nodeStuRecord));

strcpy(new_nodeStuRecord->stuFirstName,stuFirstName);

strcpy(new_nodeStuRecord->stuLastName,stuLastName);

new_nodeStuRecord->stuScore = stuScore;

new_nodeStuRecord->nextRec = (*refHead);

(*refHead) = new_nodeStuRecord;

}

void printStudentRecordList(struct nodeStuRecord *nodeStuRecord)

{

while (nodeStuRecord != NULL)

{

printf("stuFirstName Name: %s, stuLastName Name: %s, stuScore: %0.2lf ", nodeStuRecord->stuFirstName,nodeStuRecord->stuLastName,nodeStuRecord->stuScore);

nodeStuRecord = nodeStuRecord->nextRec;

}

}

void insertStuRecord(struct nodeStuRecord **refHead,char stuFirstName[],char stuLastName[],double stuScore)

{

addNewRecord(refHead,stuFirstName,stuLastName,stuScore);

}

void deleteStuRecord(struct nodeStuRecord **refHead,char stuLastName[])

{

struct nodeStuRecord *head1 = *refHead;

struct nodeStuRecord *nextRec = NULL;

struct nodeStuRecord *prevRec = NULL;

while(head1)

{

nextRec = head1->nextRec;

if(strcmp(head1->stuLastName,stuLastName) == 0)

{

if(nextRec)

{

strcpy(head1->stuFirstName,nextRec->stuFirstName);

strcpy(head1->stuLastName,nextRec->stuLastName);

head1->stuScore = nextRec->stuScore;

head1->nextRec = nextRec->nextRec;

free(nextRec);

}

else

{

prevRec->nextRec = NULL;

free(head1);

}

}

prevRec = head1;

head1 = head1->nextRec;

}

}

void searchStuRecord(struct nodeStuRecord **refHead,char stuLastName[])

{

struct nodeStuRecord *head1 = *refHead;

while(head1)

{

if(strcmp(head1->stuLastName,stuLastName) == 0)

{

printf("FirstName Name: %s, LastName Name: %s, stuScore: %0.2lf ", head1->stuFirstName,head1->stuLastName,head1->stuScore);

}

head1 = head1->nextRec;

}

}

int cntStuRecod(struct nodeStuRecord *nodeStuRecord)

{

int cntA = 0;

while (nodeStuRecord != NULL)

{

cntA++;

nodeStuRecord = nodeStuRecord->nextRec;

}

return cntA;

}

void sortByStudentScore(struct nodeStuRecord *listOfRecord)

{

int cntk = cntStuRecod(listOfRecord);

struct nodeStuRecord* temphead = listOfRecord;

double tempstuScore;

char stuFirstName[20],stuLastName[20];

int kk;

for (kk=0; kk<cntk; kk++)

   {

       while (temphead->nextRec)

       {

           if (temphead->stuScore > temphead->nextRec->stuScore)

           {

               tempstuScore = temphead->stuScore;

               temphead->stuScore = temphead->nextRec->stuScore;

               temphead->nextRec->stuScore = tempstuScore;

               strcpy(stuFirstName,temphead->stuFirstName);

               strcpy(temphead->stuFirstName,temphead->nextRec->stuFirstName);

               strcpy(temphead->nextRec->stuFirstName,stuFirstName);

               strcpy(stuLastName,temphead->stuLastName);

               strcpy(temphead->stuLastName,temphead->nextRec->stuLastName);

               strcpy(temphead->nextRec->stuLastName,stuLastName);

           }

           else

               temphead = temphead->nextRec;

       }

       temphead = listOfRecord;

   }

}

void sortstuLastName(struct nodeStuRecord *listOfRecord)

{

int cntkk = cntStuRecod(listOfRecord);

struct nodeStuRecord* temphead = listOfRecord;

double tempstuScore;

char stuFirstName[20],stuLastName[20];

int kk;

for (kk=0; kk<cntkk; kk++)

   {

       while (temphead->nextRec)

       {

           if (strcmp(temphead->stuLastName,temphead->nextRec->stuLastName) > 0)

           {

               tempstuScore = temphead->stuScore;

               temphead->stuScore = temphead->nextRec->stuScore;

               temphead->nextRec->stuScore = tempstuScore;

               strcpy(stuFirstName,temphead->stuFirstName);

               strcpy(temphead->stuFirstName,temphead->nextRec->stuFirstName);

               strcpy(temphead->nextRec->stuFirstName,stuFirstName);

               strcpy(stuLastName,temphead->stuLastName);

               strcpy(temphead->stuLastName,temphead->nextRec->stuLastName);

               strcpy(temphead->nextRec->stuLastName,stuLastName);

           }

           else

               temphead = temphead->nextRec;

       }

       temphead = listOfRecord;

   }

}

void getMedianScore(struct nodeStuRecord *headRec)

{

int cnt1 = cntStuRecod(headRec);

sortByStudentScore(headRec);

struct nodeStuRecord *tempRec = headRec;

int kk = 0;

double ans1 = 0;

if(cnt1 % 2 ==0)

{

while(kk+1 != cnt1/2)

{

tempRec = tempRec->nextRec;

kk++;

}

ans1 = tempRec->stuScore;

ans1 = ans1 + tempRec->nextRec->stuScore;

ans1 = ans1/2.0;

}

else

{

while(kk != cnt1/2)

{

tempRec = tempRec->nextRec;

kk++;

}

ans1 = tempRec->stuScore;

}

int ab=0;

tempRec = headRec;

while(tempRec)

{

if(tempRec->stuScore > ans1)

ab++;

tempRec = tempRec->nextRec;

}

printf("Median Score %d",ans1);

printf("no of students greater than median : %d",ab);

}

int main()

{

struct nodeStuRecord *listOfRecord = NULL;

int opCh,cnt;

addNewRecord(&listOfRecord,"Tom","Tony",8);

printStudentRecordList(listOfRecord);

char stuFirstName[20],stuLastName[20];

double stuScore;

do

{

printf("1. Print records ");

printf("2. Add a new record ");

printf("3. Delete record(s) ");

printf("4. Search by stuLastName name ");

printf("5. Sort by score ");

printf("6. Sort by stuLastName name ");

printf("7. Find the median score ");

printf("8. Exit the program ");

printf(" ");

printf("Enter ur choice ");

scanf("%d",&opCh);

switch(opCh)

{

case 1: printStudentRecordList(listOfRecord);

break;

case 2: printf("Enter firstName,lastName,score ");

scanf("%s %s %lf",&stuFirstName,&stuLastName,&stuScore);

insertStuRecord(&listOfRecord,stuFirstName,stuLastName,stuScore);

break;

case 3: printf("Enter LastName to delete ");

scanf("%s",&stuLastName);

deleteStuRecord(&listOfRecord,stuLastName);

break;

case 4: printf("enter lastname to search ");

scanf("%s",&stuLastName);

searchStuRecord(&listOfRecord,stuLastName);

break;

case 5: sortByStudentScore(listOfRecord);

printStudentRecordList(listOfRecord);

break;

case 6: sortstuLastName(listOfRecord);

printStudentRecordList(listOfRecord);

break;

case 7: getMedianScore(listOfRecord);

break;

case 8: printf("END ");

break;

default: opCh = 8;

printf("INVALID ");

break;

}

}while(opCh != 8);

system("pause");

return 0;

}

Output:

stuFirstName Name: Tom, stuLastName Name: Tony, stuScore: 8.00

1. Print records

2. Add a new record

3. Delete record(s)

4. Search by stuLastName name

5. Sort by score

6. Sort by stuLastName name

7. Find the median score

8. Exit the program

Enter ur choice sin

Enter LastName to delete

1. Print records

2. Add a new record

3. Delete record(s)

4. Search by stuLastName name

5. Sort by score

6. Sort by stuLastName name

7. Find the median score

8. Exit the program

Enter ur choice 1

stuFirstName Name: Tom, stuLastName Name: Tony, stuScore: 8.00

1. Print records

2. Add a new record

3. Delete record(s)

4. Search by stuLastName name

5. Sort by score

6. Sort by stuLastName name

7. Find the median score

8. Exit the program

Enter ur choice 2

Enter firstName,lastName,score : arlen muller 8

1. Print records

2. Add a new record

3. Delete record(s)

4. Search by stuLastName name

5. Sort by score

6. Sort by stuLastName name

7. Find the median score

8. Exit the program

Enter ur choice 1

stuFirstName Name: arlen, stuLastName Name: muller, stuScore: 3.00

stuFirstName Name: Tom, stuLastName Name: Tony, stuScore: 8.00

1. Print records

2. Add a new record

3. Delete record(s)

4. Search by stuLastName name

5. Sort by score

6. Sort by stuLastName name

7. Find the median score

8. Exit the program

Enter ur choice 8

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote