PLEASE GIVE ME A FULL CODE BECAUSE I DIDNT GET THE CORRECT ANSWER BEFORE AND PLE
ID: 3688912 • Letter: P
Question
PLEASE GIVE ME A FULL CODE BECAUSE I DIDNT GET THE CORRECT ANSWER BEFORE AND PLEASE USE ONLY POINTERS IN IT ,NOT STRUCTURE OR FILE OR ANYTHING. Write the following program in C. You must use pointers for this lab. No arrays, no structures. 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. 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
Answer:
Program code to copy:
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
//function prototype
void menu();
void print_records(int size, char **firstname, char **lastname, float *score);
void search_by_lastname(int size, char **firstname, char **lastname, float *score);
void sort_by_score(int size, char **firstname, char **lastname, float *score);
void sort_by_lastname(int size, char **firstname, char **lastname, float *score);
void add(int size, char **firstname, char **lastname, float *score);
int delete(int size, char **firstname, char **lastname, float *score);
void median_of_scores(int size, char **firstname, char **lastname, float *score);
int main(void)
{
//declare the required variables
char **fn;
char **ln;
float *sc;
int n = 0;
int i = 0;
int x = -1;
//prompt the user to enter the size
printf("Please indicate the number of records you want to enter (min 5, max 15): ");
scanf("%d", &n);
if(n < 5 || n > 15)
printf("Enter more then 5 records or less than 15!");
else
{
//allocate memory for size of the array
fn = malloc(sizeof(char *) * n);
ln = malloc(sizeof(char *) * n);
sc = malloc(sizeof(float) * n);
//allocate memory for string in the array
for(int i = 0; i < n; i++)
{
fn[i] = malloc(sizeof(char) * 15);
ln[i] = malloc(sizeof(char) * 15);
}
//prompt the user to enter the names and their scores
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 column 0
// score is stored in sc array starting at index 0
}
do
{
//call the menu function
menu();
//prompt the user to input 0 - 7 values
printf("Press a number 1 to 7 and 0 - quit. ");
scanf("%d", &x);
printf("You have selected option: %d ", x);
//switch statement
switch(x)
{
//to print the records
case 1:
print_records(n, fn, ln, sc);
break;
//to add records
case 2:
add(n, fn, ln, sc);
n++;
break;
//to delete records
case 3:
n = delete(n, fn, ln, sc);
break;
//to search a name in the records
case 4:
search_by_lastname(n, fn, ln, sc);
break;
//to sort a records by scores
case 5:
sort_by_score(n, fn, ln, sc);
break;
//to sort records by lastname
case 6:
sort_by_lastname(n, fn, ln, sc);
break;
//to find the median and number of students
//above the median value
case 7:
median_of_scores(n, fn, ln, sc);
break;
//to quit
case 0:
return 0;
default:
printf("Please only enter a number from 1 to 7 and 0 to quit. ");
}
} while(x != 0);
}
return 0;
}
// display the menu
void menu()
{
printf(" Record Menu ");
printf("Print records (press 1) ");
printf("Add records (press 2) ");
printf("Delete records (press 3) ");
printf("Search by last name (press 4) ");
printf("Sort by score (press 5) ");
printf("Sort by last name (press 6) ");
printf("Median of score (press 7) ");
printf("Exit program (press 0) ");
printf(" ");
}
//Print records – prints records of all students
void print_records(int size, char **firstname, char **lastname, 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]);
}
}
//Add a new record – take a new record from the user for a new student.
void add(int size, char **firstname, char **lastname, float *score)
{
int n = size + 1;
//reallocate the size of all the arrays
firstname = realloc(firstname, n);
firstname[size] = malloc(sizeof(char) * 15);
lastname = realloc(lastname, n);
lastname[size] = malloc(sizeof(char) * 15);
score = realloc(score, n);
//prompt the user to enter the prompted values
printf(" Enter Student's Record details as prompted: ");
printf("First Name: ");
scanf("%s", firstname[size]);
printf("Last Name: ");
scanf("%s", lastname[size]);
printf("Student Score: ");
scanf("%f", &score[size]);
}
//Delete record(s) – to delete a record, ask for the last name of
//the student from the user.
int delete(int size, char **firstname, char **lastname, float *score)
{
char *deleteName;
int i = 0, j = 0;
//allocate memory to the deleteName
deleteName = malloc(sizeof(char) * 20);
//prompt the user to enter the name to delete
printf("Enter last name to delete: ");
scanf("%s", deleteName);
//logic to delete the name
for(int i = 0; i < size; i++)
{
if(strcmp(lastname[i], deleteName) == 0)
{
for(j = i; j < size - 1; j++)
{
strcpy(firstname[j], firstname[j + 1]);
strcpy(lastname[j], lastname[j + 1]);
score[j] = score[j + 1];
}
size--;
}
}
//print the records after deleting
print_records(size, firstname, lastname, score);
return size;
}
//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.
void search_by_lastname(int size, char **firstname, char **lastname, float *score)
{
int i = 0;
//allocate memory the name to search
char *nametofind = malloc(sizeof(char) * 20);
//prompt the user to enter the name to search
printf("Please enter last name of student record you wish to print: ");
scanf("%s", nametofind);
//logic to search the name
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]);
}
}
}
//Sort by score – sort the records of students according to their scores, and
//then print the sorted records.
void sort_by_score(int size, char **firstname, char **lastname, float *score)
{
int i = 0;
int j = 0;
float tempscore;
char *tempfirstname = malloc(sizeof(char) * 20);
char *templastname = malloc(sizeof(char) * 20);
//logic to sort then records by scores
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);
}
//Sort by last name – sort the records of students according to their names
//alphabetically, and then print the sorted records.
void sort_by_lastname(int size, char **firstname, char **lastname, float *score)
{
int i = 0;
int j = 0;
float tempscore;
char *tempfirstname = malloc(sizeof(char) * 20);
char *templastname = malloc(sizeof(char) * 20);
//logic to sort the records by last name
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);
}
// Find the median score – compute the median score and print it.
// Also, print how many students are above this median score.
void median_of_scores(int size, char **firstname, char **lastname, float *score)
{
int n = size;
float median = 0;
//sort the records by scors
sort_by_score(size, firstname, lastname, score);
//logic to find the median
if(size % 2 == 0)
{
n = size / 2;
median = (score[n] + score[n - 1]) / 2;
}
else
{
n = size / 2;
median = score[n];
}
//print the values of the median and the count
printf("The median of the score is: %f ", median);
printf("Number of scores that are greater than median are: %d ", (size - n));
}
Sample output:
Please indicate the number of records you want to enter (min 5, max 15):
5
Please input records of students (enter a new line after each record), with the
following format
first name last name score
Kelvin Jackson 67
Tom Yanky 70
Willam Norway 89
Rosy James 88
Oliver Franklin 60
Record Menu
Print records (press 1)
Add records (press 2)
Delete records (press 3)
Search by last name (press 4)
Sort by score (press 5)
Sort by last name (press 6)
Median of score (press 7)
Exit program (press 0)
Press a number 1 to 7 and 0 - quit.
1
You have selected option: 1
First Name: Kelvin, Last Name: Jackson, Score: 67.00
First Name: Tom, Last Name: Yanky, Score: 70.00
First Name: Willam, Last Name: Norway, Score: 89.00
First Name: Rosy, Last Name: James, Score: 88.00
First Name: Oliver, Last Name: Franklin, Score: 60.00
Record Menu
Print records (press 1)
Add records (press 2)
Delete records (press 3)
Search by last name (press 4)
Sort by score (press 5)
Sort by last name (press 6)
Median of score (press 7)
Exit program (press 0)
Press a number 1 to 7 and 0 - quit.
4
You have selected option: 4
Please enter last name of student record you wish to print: James
First Name: Rosy, Last Name: James, Score: 88.00
Record Menu
Print records (press 1)
Add records (press 2)
Delete records (press 3)
Search by last name (press 4)
Sort by score (press 5)
Sort by last name (press 6)
Median of score (press 7)
Exit program (press 0)
Press a number 1 to 7 and 0 - quit.
5
You have selected option: 5
First Name: Oliver, Last Name: Franklin, Score: 60.00
First Name: Kelvin, Last Name: Jackson, Score: 67.00
First Name: Tom, Last Name: Yanky, Score: 70.00
First Name: Rosy, Last Name: James, Score: 88.00
First Name: Willam, Last Name: Norway, Score: 89.00
Record Menu
Print records (press 1)
Add records (press 2)
Delete records (press 3)
Search by last name (press 4)
Sort by score (press 5)
Sort by last name (press 6)
Median of score (press 7)
Exit program (press 0)
Press a number 1 to 7 and 0 - quit.
6
You have selected option: 6
First Name: Oliver, Last Name: Franklin, Score: 60.00
First Name: Kelvin, Last Name: Jackson, Score: 67.00
First Name: Rosy, Last Name: James, Score: 88.00
First Name: Willam, Last Name: Norway, Score: 89.00
First Name: Tom, Last Name: Yanky, Score: 70.00
Record Menu
Print records (press 1)
Add records (press 2)
Delete records (press 3)
Search by last name (press 4)
Sort by score (press 5)
Sort by last name (press 6)
Median of score (press 7)
Exit program (press 0)
Press a number 1 to 7 and 0 - quit.
2
You have selected option: 2
Enter Student's Record details as prompted:
First Name: Edward
Last Name: Bill
Student Score: 77
Record Menu
Print records (press 1)
Add records (press 2)
Delete records (press 3)
Search by last name (press 4)
Sort by score (press 5)
Sort by last name (press 6)
Median of score (press 7)
Exit program (press 0)
Press a number 1 to 7 and 0 - quit.
1
You have selected option: 1
First Name: Oliver, Last Name: Franklin, Score: 60.00
First Name: Kelvin, Last Name: Jackson, Score: 67.00
First Name: Rosy, Last Name: James, Score: 88.00
First Name: Willam, Last Name: Norway, Score: 89.00
First Name: Tom, Last Name: Yanky, Score: 70.00
First Name: Edward, Last Name: Bill, Score: 77.00
Record Menu
Print records (press 1)
Add records (press 2)
Delete records (press 3)
Search by last name (press 4)
Sort by score (press 5)
Sort by last name (press 6)
Median of score (press 7)
Exit program (press 0)
Press a number 1 to 7 and 0 - quit.
3
You have selected option: 3
Enter last name to delete: Yanky
First Name: Oliver, Last Name: Franklin, Score: 60.00
First Name: Kelvin, Last Name: Jackson, Score: 67.00
First Name: Rosy, Last Name: James, Score: 88.00
First Name: Willam, Last Name: Norway, Score: 89.00
First Name: Edward, Last Name: Bill, Score: 77.00
Record Menu
Print records (press 1)
Add records (press 2)
Delete records (press 3)
Search by last name (press 4)
Sort by score (press 5)
Sort by last name (press 6)
Median of score (press 7)
Exit program (press 0)
Press a number 1 to 7 and 0 - quit.
You have selected option: 0
Press any key to continue…
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.