Write a menu based program to maintain student records. Your program should take
ID: 3692516 • Letter: W
Question
Write a menu based 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
4. Zip code (may be a string)
You have to create a structure Student with above information. Then create a linked list w here each list node contains a Student . Your program should be able to take records of a minimum of 5 students. After taking the records, you should provide 7 functionalities to the user.
1. Print records – prints records of all students
2. Add a new record – take a new record from the us er for a new student. Create a node for the new student and add it to the end of the list.
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 have to delete corresponding list nodes).
4. Search by Zip code – prints records of all students with a given zip code.
5. Search by score range – take two scores max, min ; then print records of all students who have scores between [max, min] (inclusive).
6. Find the median score – compute the median score and print it. Also, print how many students are above this median score (do not forget to sort the list to compute median) .
7. 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 Zip code: code 1
First Name: firstname 2, Last Name: lastname 2, Sco re: score 2 Zip code: code 2 . . .
You should write each functionality from 1-7 in separate functions. You should provide a menu to the u ser 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 zip code
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 zip code (press 4)
Search by score range (press 5)
Find median score (press 7)
Exit the program (press 0)
After user chooses a functionality, your program pe rforms that and provides this menu again to select another functionality. This goes on until user presses 0.
Instruction: Do not forget to use
- malloc for a new node
- free to delete it.
------------------------------------------------------------------
Write a menu based program to maintain student records. Your program should take the following inputs: Student first name (max. 20 characters) 2. Student last name, (max. 20 characters) 3. Student scores (float/double), eg. 85.4 4. Zip code (may be a string) You have to create a structure Student with above information. Then create a linked list where each I node contains a Student. Your program should be able to take records of a minimum of 5 students. After taking the records, you should provide 7 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. Create a node for the new student and add it to the end of the list. 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 have to delete corresponding list nodes). 4. Search by Zip code prints records of all students with a given zip code. 5. Search by score range take two scores max, min; then print records of all students who have scores between [max, min] (inclusive). 6. Find the median score compute the median score and print it. Also, print how many students a above this median score (do not forget to sort the list to compute median). 7. 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 Zip code: code 1 First Name: firstname 2, Last Name: lastname 2, Score: score 2 Zip code: code 2Explanation / Answer
hey heres the code:
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<string.h>
struct test_struct
{
float score;
char firstName[20];
char lastName[20];
char zipcode[10];
struct test_struct *next;
};
struct test_struct *head = NULL;
struct test_struct *curr = NULL;
struct test_struct* create_list()
{
struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct));
if(NULL == ptr)
{
printf(" Node creation failed ");
return NULL;
}
printf("Enter first name: ");
scanf("%s",&ptr->firstName);
printf("Enter last name:");
scanf("%s",&ptr->lastName);
printf("Enter score : ");
scanf("%f",&ptr->score);
printf("Enter zipcode : ");
scanf("%s",&ptr->zipcode);
ptr->next = NULL;
head = curr = ptr;
return ptr;
}
struct test_struct* add_to_list()
{
if(head==NULL)
{
return (create_list());
}
struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct));
if(NULL == ptr)
{
printf(" Node creation failed ");
return NULL;
}
printf("Enter first name: ");
scanf("%s",&ptr->firstName);
printf("Enter last name:");
scanf("%s",&ptr->lastName);
printf("Enter score : ");
scanf("%f",&ptr->score);
printf("Enter zipcode : ");
scanf("%s",&ptr->zipcode);
ptr->next = NULL;
curr->next = ptr;
curr = ptr;
}
void median()
{
float a[100],temp;
int i=0,j,k,med,tot;
struct test_struct *ptr = head;
struct test_struct *tmp = NULL;
while(ptr != NULL)
{
a[i]=ptr->score;
tmp = ptr;
ptr = ptr->next;
i++;
}
for(j=0;j<i;j++)
{
for(k=j+1;k<i;k++)
{
if(a[j]>a[k])
{
temp=a[j];
a[j]=a[k];
a[k]=temp;
}
}
}
med=i/2;
printf("%f",a[med]);
tot=i-med;
printf(" There are %d who are greater than median",tot);
}
void searchlist(int i)
{
struct test_struct *ptr = head;
struct test_struct *tmp = NULL;
bool found = false;
float max,min,temp;
char zip[10];
if(i==1)
{
printf(" Enter zipcode you want to find: ");
scanf("%s",&zip);
while(ptr != NULL)
{
if (strcmp(zip,ptr->zipcode) == 0)
{
printf("zipcode found! ");
printf(" first name is %s",ptr->firstName);
printf(" last name is %s",ptr->lastName);
printf(" score is %f",ptr->score);
}
tmp = ptr;
ptr = ptr->next;
}
}
else
{
printf(" Enter range: ");
printf("enter max: ");
scanf("%f",&max);
printf("enter min: ");
scanf("%f",&min);
if(min>max)
{
temp=max;
max=min;
min=temp;
}
while(ptr != NULL)
{
if (ptr->score<=max && ptr->score>=min)
{
printf(" first name is %s",ptr->firstName);
printf(" last name is %s",ptr->lastName);
printf(" zipcode is %s",ptr->zipcode);
}
tmp = ptr;
ptr = ptr->next;
}
}
}
struct test_struct* search_in_list(char last[], struct test_struct **prev)
{
struct test_struct *ptr = head;
struct test_struct *tmp = NULL;
bool found = false;
while(ptr != NULL)
{
if (strcmp(last,ptr->lastName) == 0)
{
found = true;
break;
}
else
{
tmp = ptr;
ptr = ptr->next;
}
}
if(true == found)
{
if(prev)
*prev = tmp;
return ptr;
}
else
{
return NULL;
}
}
int delete_from_list()
{
struct test_struct *prev = NULL;
struct test_struct *del = NULL;
char last[20];
printf(" Enter last name of the student you want to delete: ");
scanf("%s",&last);
del = search_in_list(last,&prev);
if(del == NULL)
{
return -1;
}
else
{
if(prev != NULL)
prev->next = del->next;
if(del == curr)
{
curr = prev;
}
else if(del == head)
{
head = del->next;
}
}
free(del);
del = NULL;
return 0;
}
void print_list(void)
{
struct test_struct *ptr = head;
printf(" -------Printing list ------- ");
while(ptr != NULL)
{
printf(" ");
printf("first name: %s ",ptr->firstName);
printf("last name: %s ",ptr->lastName);
printf("score : %f ",ptr->score);
printf("zipcode :%s",ptr->zipcode);
ptr = ptr->next;
}
return;
}
int main(void)
{
int op,n=1;
while(n!=0)
{
printf(" 1.Print records 2.Add a new record 3.Delete record(s) 4.Search by Zip code 5.Search by score range 6.Find the median score 7.Exit the program");
printf(" Enter your option: ");
scanf("%d",&op);
printf(" ");
switch(op)
{
case 1:print_list();
break;
case 2:
add_to_list();
break;
case 3:
delete_from_list();
break;
case 4:
searchlist(1);
break;
case 5:
searchlist(2);
break;
case 6:
median();
break;
case 7:
n=0;
break;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.