Write a menu based program to maintain student records. Your program should take
ID: 658184 • 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 where 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
Explanation / Answer
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>
#include<string.h>
#include<stdlib.h>
void menu();
void print_records(int size,
char firstname[][20],
char lstname[][20],
float score[]);
void srch_by_firstname(int size,
char firstname[][20],
char lstname[][20],
float score[]);
void srch_by_lstname(int size,
char firstname[][20],
char lstname[][20],
float score[]);
void sort_by_score(int size,
char firstname[][20],
char lstname[][20],
float score[]);
void sort_by_lstname(int size,
char firstname[][20],
char lstname[][20],
float score[]);
int main(void)
{
char fn[15][20];
char ln[15][20];
float sc[15];
int n = 0;
int i = 0;
int x = -1;
char nametofind[20];
printf("Please indicate the number of records you want to enter (min 5, max 15): ");
scanf("%d", &n);
printf("Please input records of students (enter a new line after each record), with the following format first name last name score ");
for (i = 0; i<n; i++)
{
scanf("%s %s %f", &fn[i][0], &ln[i][0], &sc[i]);
}
do
{
printf(" -you can access records by- ");
printf("Print data (press 1) ");
printf("Srch from f name (press 2)");
printf("Srch from l name (press 3)");
printf("Sort from score (press 4)");
printf("Sort from last name (press 5)");
printf("Exit (press 0) ");
printf("-------------------------- ");
printf("Please function through entering value 0 to 5. ");
scanf_s("%d", &x);
printf("You have selected option: %d ", x);
switch (x)
{
case 1:
print_records(n, fn, ln, sc);
break;
case 2:
srch_by_firstname(n, fn, ln, sc);
break;
case 3: srch_by_lastname(n, fn, ln, sc);
break;
case 4: sort_by_score(n, fn, ln, sc);
break;
case 5: sort_by_lastname(n, fn, ln, sc);
break;
default: printf("Please only enter a number from 0 to 5 ");
}
} while (x != 0);
return 0;
}
void print_records(int size, char firstname[][20], char lastname[][20], float score[])
{
int i = 0;
for (i = 0; i < size; i++)
{
printf("First Name: %s, Last Name: %s, Score: %.2f ", firstname[i], lastname[i], score[i]);
}
}
void srch_by_firstname(int size, char firstname[][20], char lastname[][20], float score[])
{
int i = 0;
char nametofind[20];
printf("Please enter the first name of student record you wish to print: ");
gets(nametofind);
for (i = 0; i<size; i++)
{
if (strcmp(nametofind, firstname[i]) == 0)
{
printf("First Name: %s, Last Name: %s, Score: %.2f ", firstname[i], lastname[i], score[i]);
}
}
}
void srch_by_lastname(int size, char firstname[][20], char lastname[][20], float score[])
{
int i = 0;
char nametofind[20];
printf("Please enter last name of student record you wish to print: ");
gets(nametofind);
for (i = 0; i<size; i++)
{
if (strcmp(nametofind, lastname[i]) == 0)
{
printf("First Name: %s, Last Name: %s, Score: %.2f ", firstname[i], lastname[i], score[i]);
}
}
}
void sort_by_score(int size, char firstname[][20], char lastname[][20], float score[])
{
int i = 0;
int j = 0;
float tempscore;
char tempfirstname[20];
char templastname[20];
for (i = 0; i<size - 1; i++)
{
for (j = 0; j<size - i - 1; j++)
{
if (score[j]>score[j + 1])
{
tempscore = score[j];
score[j] = score[j + 1];
score[j + 1] = tempscore;
strcpy(tempfirstname, firstname[j]);
strcpy(firstname[j], firstname[j + 1]);
strcpy(firstname[j + 1], tempfirstname);
strcpy(templastname, lastname[j]);
strcpy(lastname[j], lastname[j + 1]);
strcpy(lastname[j + 1], templastname);
}
}
}
print_records(size, firstname, lastname, score);
}
void sort_by_lastname(int size, char firstname[][20], char lastname[][20], float score[])
{
int i = 0;
int j = 0;
float tempscore;
char tempfirstname[20];
char templastname[20];
for (i = 0; i<size - 1; i++)
{
for (j = 0; j<size - i - 1; j++)
{
if (strcmp(lastname[j], lastname[j + 1])>0)
{
strcpy(tempfirstname, firstname[j]);
strcpy(firstname[j], firstname[j + 1]);
strcpy(firstname[j + 1], tempfirstname);
strcpy(templastname, lastname[j]);
strcpy(lastname[j], lastname[j + 1]);
strcpy(lastname[j + 1], templastname);
tempscore = score[j];
score[j] = score[j + 1];
score[j + 1] = tempscore;
}
}
}
print_records(size, firstname, lastname, score);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.