//------------------------------------------------------------------ // // 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 ageOfOldest = 0; // Three variables to hold aggregate int sumOfAges =0; // data about patients int numberOfDiseases = 0; // ifstream infile("patient.dat"); // Open the patient record file //-------------------------------------------------------------- // // Read records from the file // // Since in this case we know that there are at least 7 records in // the file, we can use a for loop. 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(); // We are done reading from the file, so // we can close it. //-------------------------------------------------------------- // // Print out diseases in reverse order // // Since we have stored the data from the file in the arrays, we // can use it even though the file is now closed. We just step // through the array backwards printing out each member. cout << endl << "Diseases in reverse order" << endl << endl; for(recordCounter = LENGTH - 1; recordCounter >= 0; recordCounter--) { cout << diseases[recordCounter] << endl; } //-------------------------------------------------------------- // // Find the oldest patient // // To find the oldest patient, we go through the array entry by // entry, looking to see if the number that is the latest entry is // bigger than all the previous biggest. cout << endl << "The oldest patient is: "; for(recordCounter = LENGTH - 1; recordCounter >= 0; recordCounter--) { if(ages[recordCounter] > ageOfOldest) { ageOfOldest = ages[recordCounter]; } } cout << ageOfOldest << endl; //-------------------------------------------------------------- // // Compute average age of patients // // To calculate the average age, we first need to add up the ages // of all the patients. We do this by adding each member of the // array to a running total. for(recordCounter = 0; recordCounter < LENGTH; recordCounter++) { sumOfAges += ages[recordCounter]; // Add each age in turn to } // the running total. // 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 << "Average age is: "; cout << sumOfAges / (LENGTH * 1.0) << endl; //-------------------------------------------------------------- // // Count the number of cases of a given disease. // // Ask the user for a disease. Then look through the array, and increase // the count each time we find an instance of this 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 << endl; //-------------------------------------------------------------- // // End. // Note that we closed the file way back return 0; }