//this program reads a list of blood pressures for several patients.
//It then sorts them according to systolic, diastolic, and pulse pressures.
//After sorting, it prints the patients from each list with the highest pressures.
#include <iostream>
using namespace std;
//global constants
const int NUMBER_OF_PATIENTS=10;
const int NUMBER_IN_DANGER=5;

//data types
struct bloodType
    {
    int ID;
    int systolic;
    int diastolic;
    int pulse;
    };
enum pressType {SYS,DIA,PUL};

//functions
void readData(bloodType[]);
void sortData(bloodType[],pressType);
void printData(bloodType[]);

int main()
    {
    pressType i;
    bloodType patientList[NUMBER_OF_PATIENTS];
    readData(patientList);
    for(i=SYS;i<=PUL;i=pressType(i+1))
        {
        sortData(patientList,i);
        printData(patientList);
        }
    return 0;
    }

//Read systolic and diastolic pressures for each patient on the list.
//Calculate pulse pressure as the difference between systolic and diastolic.
//Assign patient ID numbers consecutively.
void readData(bloodType patientList[NUMBER_OF_PATIENTS])
    {
    int i;
    for (i=1;i<=NUMBER_OF_PATIENTS;i++)
        {
        cout <<"please enter the systolic and diastolic pressures for patient "<<i<<endl;
        cin>>patientList[i-1].systolic>>patientList[i-1].diastolic;
        patientList[i-1].ID=i;
        patientList[i-1].pulse=patientList[i-1].systolic-patientList[i-1].diastolic;
        }
    }

//Sort the list of patients in descending order by the pressure specified in whichPressure
//This function uses Bubble Sort.
void sortData(bloodType patientList[NUMBER_OF_PATIENTS],pressType whichPressure)
    {
    int i,last;
    bloodType temp;
    bool outOfOrder;
    for(last=NUMBER_OF_PATIENTS-1;last>=0;last--)
        for(i=0;i<=last-1;i++)
            {
            switch(whichPressure)//which field am I sorting by?
                {
                case SYS:
                    outOfOrder=patientList[i].systolic<patientList[i+1].systolic;
                    break;
                case DIA:
                    outOfOrder=patientList[i].diastolic<patientList[i+1].diastolic;
                    break;
                case PUL:
                    outOfOrder=patientList[i].pulse<patientList[i+1].pulse;
                    break;
                }
            if(outOfOrder)
                {
                temp=patientList[i];
                patientList[i]=patientList[i+1];
                patientList[i+1]=temp;
                }
            }
    }

//Print the blood pressure data from the top few patients in the list.
void printData(bloodType patientList[NUMBER_OF_PATIENTS])
    {
    int i;
    cout<<"ID systolic diastolic pulse\n";
    for (i=0;i<=NUMBER_IN_DANGER-1;i++)
        {
        cout<<patientList[i].ID<<" "<<patientList[i].systolic
            <<" "<<patientList[i].diastolic<<" "<<patientList[i].pulse<<endl;
        }
        cout<<endl;
    }