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

C - Programming NOTE: \"This lab needs to be completed usin array. Usage of stru

ID: 3730915 • Letter: C

Question

C - Programming

NOTE: "This lab needs to be completed usin array. Usage of structure is not Write a following inputs: program to maintain student records. Your program should take the 1. Student first name (max. 20 characters) 2. Student last name, (max. 20characters) 3. Student scores (float/double), eg. 85.4 Your program should be able to take records of a minimum of 5 students and maximum of 15 students Aftler taking the records, you should provide 6 functionalities to the user. 1. Print records-prints records of all students 2. Search by first name-prints record of the student with a given first name. If there aremultiple students with the same first name, print 3. Search by last name- prints record of the student with a given last name. If there are multiple students with 4. Sort by score -sort the records of students according to their scores, and then print the sorted records. S. Sort by last name -sort the records of students according to their names alphabetically,and then print the sorted records. 6. Find Max score-prints record of the student with the maximum score. If there are multiple students with the same maximum records for all ofthem the same last name, print records for all ofthem print score, print records for all of them. Find Min score-prints record of the student with the minimum score. If there are multiple students with the same minimum score, print records for all of them. 8. Exit the program-terminate on a specific input from the user. Let that specific input bean inleger of value 0. You should print the record in the following format: First Name: firstname 1, Last Name: lastname 1, Score: score 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 Please indicate number of records you want to enter (min 5, max 15) lof 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 functonalite Print records (press ) Search by first name (press 2) Search by last name (press 3) Sont by score (press 4) Sont by last name (press S Find Max Score (press 6) Find Min Score (press 7 Exit the program (press 0) performs that and t provides this menu againo After user chooses functionality, your program select another functionality. This goes on until user presses 0.

Explanation / Answer

NOTES: In the following program, an additional funcition called getMax(arr[], a, b) was created that finds the maximum element specifically between the index a and b. This has been done in order to make it easier to get the maximum element in the subarray during selection sort

#include<iostream>

String firstnames[15][20], lastnames[15][20];
double scores[20];

void main(){
int n;
cout<<"Please indicate the number of records you want to input (Min 5 max 15): "
cin >> n

for(int i = 0; i < n; i++)
{
cout<<"Enter firts name: ";
cin>>firstnames[i];
cout<<" Enter last name: ";
cin>>lastnames[i];
cout<<" Enter score: ";
cin>> scores[i];
cout<<" ";
}

while (true)
{
cout<<"Print records(press 1) Search by first name(press 2) Search by last name (press 3) Sort by score(press 4) Sort by last name (press 5) Find max score(press 6) Find Min score(press 7)Exit the program (press 0)";

int choice;
ciin>>choice;

switch (choice)
case 1:
printRecs();
break;
case 2:
searchFirst();
break;
case 3:
searchLast();
break;
case 4:
sortScore();
break;
case 5:
  sortLast();
break;
case 6:
findMax();
break;
case 7:
findMin();
break;
case 8:
return;

}
}//end of while loop that will go back to the menu
}//end of main()

void printRecs(int n)
{
for(int i = 0; i < n; i++)
{
cout<<"Firstname: "<<firstnames[i]<<"Lastname: "<<lastnames[i]<<"Score: "<<scores[i];
}
}

void searchFirst(int n)
{
char key[20];

cout<<"Enter name you'd like to search for: ";
cin>> key;
for(int i = 0; i < n; i++)
{
if ((strcmp(key, firstnames[i]) == 0)
{
  cout<<"Firstname: "<<firstnames[i]<<"Lastname: "<<lastnames[i]<<"Score: "<<scores[i];
}
}
}

void searchLast(int n)
{
char key[20];

cout<<"Enter name you'd like to search for: ";
cin>> key;
for(int i = 0; i < n; i++)
{
if ((strcmp(key, lastnames[i]) == 0)
{
  cout<<"Firstname: "<<firstnames[i]<<"Lastname: "<<lastnames[i]<<"Score: "<<scores[i];
}
}
}

void sortScore(int n)
{
for(int i = 0; i < n; i++)
{
int max = getMax(scores, i, n);

for(int j = i; j < n; j++)
{
if (scores[j] == max)
{
int temp = scores[i];
scores[i] = scores[j];
scores[j] = temp;
}
}
}
for(int i = 0; i < n; i++)
{
  cout<<"Firstname: "<<firstnames[i]<<"Lastname: "<<lastnames[i]<<"Score: "<<scores[i];
}
}
void sortLast(int n)
{
int error = 0;
do
{
error = 0;
for (int i = 0; i < n-1; i++)
{
if (strcmp(lastnames[i], lastnames[i+1] < 0))
{
//Swap
char temp[20];
strcpy(temp, lastnames[i]);
strcpy(lastnames[i], lastnames[i+1]);
strcpy(lastnames[i+1], temp);

errors++;
}
}
}while(errors > 0);
  
printRecs(n);
}

int findMax()
{
return getMax(scores, 0, n);
}

int findMin(int n)
{
int min = scores[0];
for(int i = 0; i < n; i++)
{
if (scores[i] < min)
{
min = scores[i];
}
}

return min;
}

int getMax(int arr[], a, b)
{
int max = arr[a];
for(int i = a; i < b; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
}
return max;
}