//------------------------------------------------------------------ // // A program that illustrates the use of arrays in // the context of the patient record example. // // Simon Parsons // March 6th 2007. // //------------------------------------------------------------------ //------------------------------------------------------------------ // // Link the necessary libraries #include #include using namespace std; //------------------------------------------------------------------ // // Declare variables int idNumber; int age; int disease; int zipCode; //------------------------------------------------------------------ // // A method to print out the patient data void printRecord() { cout << "Patient ID: \t" << idNumber << endl; cout << "Age: \t\t" << age << endl; cout << "ZIPcode: \t" << zipCode << endl; cout << "Suffers from: \t"<< disease << endl; cout << endl; } //------------------------------------------------------------------ // // The main method int main() { // Initialisation const int LENGTH = 7; // The length of arrays int diseases[LENGTH]; // Declare two arrays to hold int ages[LENGTH]; // patient data int recordCounter; // Declare counter int sumOfAges =0; // Two variables to hold aggregate int numberOfDiseases = 0; // data ifstream infile("patient.dat"); // Open the patient record file //-------------------------------------------------------------- // // Read records from the file for(recordCounter = 0; recordCounter < LENGTH ; recordCounter++) { infile >> idNumber; // Read data in from infile >> age; // file. infile >> disease; infile >> zipCode; // Store disease and age data in arrays diseases[recordCounter] = disease; ages[recordCounter] = age; printRecord(); // Print data } infile.close(); // Close file //-------------------------------------------------------------- // // Print out diseases in reverse order cout << endl << "Diseases in reverse order" << endl << endl; for(recordCounter = LENGTH - 1; recordCounter >= 0; recordCounter--) { cout << diseases[recordCounter] << endl; } //-------------------------------------------------------------- // // Compute average age of patients for(recordCounter = 0; recordCounter < LENGTH; recordCounter++) { sumOfAges += ages[recordCounter]; // Add each age in turn to } // the aggregate age. // Set the number of decimal places of the output cout.setf(ios::fixed, ios::floatfield); cout.precision(1); // Calculate the average age and print it cout << endl << "Average age is "; cout << endl << sumOfAges / (LENGTH * 1.0) << endl; //-------------------------------------------------------------- // // Count the number of cases of a given disease. // Get disease number from user cout << endl << "Enter disease: "; cin >> disease; // Count the number of times we see that disease for(recordCounter = 0; recordCounter < LENGTH; recordCounter++) { if(diseases[recordCounter] == disease) // If the disease in the { // array matches the one we numberOfDiseases += 1; // are looking for, increase } // the count. } // Print out the number of times we found the disease cout << endl << "Number of patients with "; cout << disease << " is " << numberOfDiseases << endl; //-------------------------------------------------------------- // // End. // Note that we closed the file way back return 0; }