Instruction: You must use pointers for this lab. No arrays, no structures. For e
ID: 3693656 • Letter: I
Question
Instruction: You must use pointers for this lab. No arrays, no structures. For example, if you need an array of floats to hold the scores, do not use float score[15]. Rather use float *score, then use dynamic memory allocation to hold required memory. You must use memory optimally, that is if you have only 6 scores, you must point to a memory chunk of sizeof(float)*6 bytes.
Similarly, to hold a name, instead of doing it with 2D char arrays, use 2D pointers (char firstName[15][20] char **firstName).
Problem: 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
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
Ans;
#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 lastname[][20], float score[]);
void search_by_firstname(int size, char firstname[][20], char lastname[][20], float score[]);
void search_by_lastname(int size, char firstname[][20], char lastname[][20], float score[]);
void sort_by_score(int size, char firstname[][20], char lastname[][20], float score[]);
void sort_by_lastname(int size, char firstname[][20], char lastname[][20], float score[]);
int main(void)
{
char fn[15][20]; // 15 rows 20 columns or 15 students 20 characters
char ln[15][20]; // 15 rows 20 columns or 15 students 20 characters
float sc[15]; // 15 scores max not all used
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]); //stores first name and last name characters in ith row starting at collumn 0
// score is stored in sc array starting at index 0
}
do
{
printf(" ---you may access the records by--- ");
printf("Print records (press 1) ");
printf("Search by first name (press 2) ");
printf("Search by last name (press 3) ");
printf("Sort by score (press 4) ");
printf("Sort by last name (press 5) ");
printf("Exit program (press 0) ");
printf("-------------------------- ");
printf("Please a function by entering a value from 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:
search_by_firstname(n, fn, ln, sc);
break;
case 3: search_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 search_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: ");
/*fflush(stdin);*/
gets(nametofind);
for (i = 0; i<size; i++)
{
if (strcmp(nametofind, firstname[i]) == 0) // if nametofind is found in firstname array
{
printf("First Name: %s, Last Name: %s, Score: %.2f ", firstname[i], lastname[i], score[i]);
}
}
}
void search_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: ");
/*fflush(stdin);*/
gets(nametofind);
for (i = 0; i<size; i++)
{
if (strcmp(nametofind, lastname[i]) == 0) // if nametofind is found in lastname array
{
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];
// bubble sort
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];
// bubble sort
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);