Menu

(Solved) : Include Include Include Using Namespace Std Declaring New Struct Store Patient Data Struct Q43991694 . . .

#include <iostream>
#include <cstdlib>
#include <cstring>

using namespace std;

// Declaring a new struct to store patient data
struct patient {
int age;
char name[20];
float balance;
};

// TODO:
// IMPLEMENT A FUNCTION THAT COMPARES TWO PATIENTS BY AGE

// THE FUNCTION RETURNS AN INTEGER AS FOLLOWS:
// -1 IF THE AGE OF THE FIRST PATIENT IS LESS
// THAN THE SECOND PATIENT’S AGE
// 0 IF THE AGES ARE EQUAL
// 1 OTHERWISE

// TODO:
// IMPLEMENT A FUNCTION THAT COMPARES TWO PATIENTS BY BALANCEDUE

// THE FUNCTION RETURNS AN INTEGER AS FOLLOWS:
// -1 IF THE BALANCE FOR THE FIRST PATIENT IS LESS
// THAN THE SECOND PATIENT’S BALANCE
// 0 IF THE BALANCES ARE EQUAL
// 1 OTHERWISE

// TODO:
// IMPLEMENT A FUNCTION THAT COMPARES TWO PATIENTS BY NAME

// THE FUNCTION RETURNS AN INTEGER AS FOLLOWS:
// -1 IF THE NAME OF THE FIRST PATIENT GOES BEFORE
// THE SECOND PATIENT’S NAME
// 0 IF THE AGES ARE EQUAL
// 1 OTHERWISE
//
// HINT: USE THE strncmp FUNCTION
// (SEE http://www.cplusplus.com/reference/cstring/strncmp/)

// The main program
int main()
{
int total_patients = 5;
  
// Storing some test data
struct patient patient_list[5] = {
{25, “Juan Valdez “, 1250},
{15, “James Morris “, 2100},
{32, “Tyra Banks “, 750},
{62, “Maria O’Donell”, 375},
{53, “Pablo Picasso “, 615}
};
  
  
cout << “Patient List: ” << endl;
  
// TODO:
// IMPLEMENT THE CODE TO DISPLAY THE CONTENTS
// OF THE ARRAY BEFORE SORTING
  
cout << endl;
  
  
cout << “Sorting…” << endl;
  
// TODO:
// CALL THE qsort FUNCTION TO SORT THE ARRAY BY PATIENT AGE
  
cout << “Patient List – Sorted by Age: ” << endl;
  
// TODO:
// DISPLAY THE CONTENTS OF THE ARRAY
// AFTER SORTING BY AGE
  
cout << endl;
  
  
cout << “Sorting…” << endl;
  
// TODO:
// CALL THE qsort FUNCTION TO SORT THE ARRAY BY PATIENTBALANCE
  
cout << “Patient List – Sorted by Balance Due: ” <<endl;
  
// TODO:
// DISPLAY THE CONTENTS OF THE ARRAY
// AFTER SORTING BY BALANCE
  
cout << endl;
  
  
cout << “Sorting…” << endl;
  
// TODO:
// CALL THE qsort FUNCTION TO SORT THE ARRAY BY PATIENT NAME
  
cout << “Patient List – Sorted by Name: ” <<endl;
  
// TODO:
// DISPLAY THE CONTENTS OF THE ARRAY
// AFTER SORTING BY NAME
  
cout << endl;
  
return 0;
}

For the above skeleton code, please do thefollowing:

Display Function

Implement a function to display the contents of the patient_listarray.

Add code to call this function. Note that there are multipleplaces where this function needs to be called. Look for the // TODOcomments to find the correct locations.

Sorting the array by Age

Implement the code to sort the contents of the patient_listarray based on the value stored in the age field. To do this youwill need to implement code that relies on the qsort function fromthe C Standard library
(see http://www.cplusplus.com/reference/cstdlib/qsort/). As shownin the reference, this code requires two parts:

A function that compares two patient elements, based on thevalue stored in the age field.

A call to the qsort function, which includes the array to besorted, the number of elements in the array, the size of each arrayelement and the function used to compare the array elements.

Add code to call the qsort function, using the age comparisonfunction that you implemented. This code should be placed justunder the appropriate // TODO comment in main().

Sorting the array by Balance Due

Implement the code to sort the contents of the patient_listarray based on the value stored in the balance field. To do thisyou will need to implement code that relies on the qsort functionfrom the C Standard library
(see http://www.cplusplus.com/reference/cstdlib/qsort/). As shownin the reference, this code requires two parts:

A function that compares two patient elements, based on thevalue stored in the balance field.

A call to the qsort function, which includes the array to besorted, the number of elements in the array, the size of each arrayelement and the function used to compare the array elements.

Add code to call the qsort function, using the balancecomparison function that you implemented. This code should beplaced just under the appropriate // TODO comment in main().

Sorting the array by Patient Name

Implement the code to sort the contents of the patient_listarray based on the value stored in the name field. To do this youwill need to implement code that relies on the qsort function fromthe C Standard library
(see http://www.cplusplus.com/reference/cstdlib/qsort/). As shownin the reference, this code requires two parts:

A function that compares two patient elements, based on thevalue stored in the name field. Because name data is stored in anarray of characters, you cannot use the relational operators (<,>, <=, … ) to do the comparison, instead you should use afunction from the C Standard Library to determine the order of thenames, see(http://www.cplusplus.com/reference/cstring/strncmp/).

A call to the qsort function, which includes the array to besorted, the number of elements in the array, the size of each arrayelement and the function used to compare the array elements.

Add code to call the qsort function, using the name comparisonfunction that you implemented. This code should be placed justunder the appropriate // TODO comment in main().

Expert Answer


Answer to #include #include #include using namespace std; // Declaring a new struct to store patient data struct patient { int age…

OR