Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.37 KB | None | 0 0
  1. /*
  2. Kevin Tancho
  3. kevintancho@gmail.com
  4. LAB 7
  5. */
  6.  
  7. #include <iostream>
  8. #include <fstream>
  9. #include <iomanip>
  10. #include <string>
  11.  
  12. using namespace std;
  13.  
  14. //Prototypes
  15. void sortSelect(int arr[], int num, double average[10],
  16.     int daysExercised[10], double estMaxHR[10], double ratioMaxToEst[10], double ratioHighToMax[10]);
  17.  
  18. void getData(int maxHR, int age, int subject[], double day1Avg, double day1MaxCommute, double day1exerciseHR,
  19.     double day2Avg, double day2MaxCommute, double day2exerciseHR,
  20.     double day3Avg, double day3MaxCommute, double day3exerciseHR,
  21.     double day4Avg, double day4MaxCommute, double day4exerciseHR,
  22.     double day5Avg, double day5MaxCommute, double day5exerciseHR, double average[10],
  23.     int daysExercised[10], double estMaxHR[10], double ratioMaxToEst[10], double ratioHighToMax[10]);
  24.  
  25. void calculation(int maxHR, int age, int i, double day1Avg, double day1MaxCommute, double day1exerciseHR,
  26.     double day2Avg, double day2MaxCommute, double day2exerciseHR,
  27.     double day3Avg, double day3MaxCommute, double day3exerciseHR,
  28.     double day4Avg, double day4MaxCommute, double day4exerciseHR,
  29.     double day5Avg, double day5MaxCommute, double day5exerciseHR, double average[10],
  30.     int daysExercised[10], double estMaxHR[10], double ratioMaxToEst[10], double ratioHighToMax[10]);
  31.  
  32. void headerOutput(ostream& out);
  33.  
  34. void resultOutput(ostream& out, int subject[], double average[10], int daysExercised[10], double estMaxHR[10], double ratioMaxToEst[10], double ratioHighToMax[10]);
  35.  
  36. int main()
  37. {
  38.     //Declare variables
  39.     int subjectID[10], maxHR = 0, age = 0, daysExercised[10];
  40.     double average[10], estMaxHR[10], ratioMaxToEst[10], ratioHighToMax[10];
  41.     double day1Avg = 0, day1MaxCommute = 0, day1exerciseHR = 0;
  42.     double day2Avg = 0, day2MaxCommute = 0, day2exerciseHR = 0;
  43.     double day3Avg = 0, day3MaxCommute = 0, day3exerciseHR = 0;
  44.     double day4Avg = 0, day4MaxCommute = 0, day4exerciseHR = 0;
  45.     double day5Avg = 0, day5MaxCommute = 0, day5exerciseHR = 0;
  46.  
  47.     ofstream outFile;
  48.     outFile.open("output.txt");
  49.     if (outFile.fail())
  50.     {
  51.         cout << "No Such File" << endl;
  52.         exit(100);
  53.     }
  54.  
  55.     outFile << "Kevin Tancho" << endl;
  56.     outFile << "kevintancho@gmail.com" << endl;
  57.     outFile << "LAB 7" << endl;
  58.     getData(maxHR, age, subjectID, day1Avg, day1MaxCommute, day1exerciseHR, day2Avg, day2MaxCommute, day2exerciseHR,
  59.         day3Avg, day3MaxCommute, day3exerciseHR, day4Avg, day4MaxCommute, day4exerciseHR, day5Avg, day5MaxCommute, day5exerciseHR,
  60.         average, daysExercised, estMaxHR, ratioMaxToEst, ratioHighToMax);
  61.  
  62.     sortSelect(subjectID, 10, average, daysExercised, estMaxHR, ratioMaxToEst, ratioHighToMax);
  63.    
  64.     headerOutput(outFile);
  65.  
  66.     resultOutput(outFile, subjectID, average, daysExercised, estMaxHR, ratioMaxToEst, ratioHighToMax);
  67.  
  68. }
  69.  
  70. //getdata
  71. /*
  72. maxHR - reference to number of maximum heart rate
  73. age - reference to number of age of the subject
  74. subject - reference to subject number
  75. day'n'Avg - reference to the average commuting heart rate on day 'n'
  76. day'n'MaxCommute - reference to the maximum commuting heart on day 'n'
  77. day'n'exerciseHR - reference to the number of exercise heart rate on day 'n'
  78. */
  79. void getData(int maxHR, int age, int subject[],
  80.     double day1Avg, double day1MaxCommute, double day1exerciseHR,
  81.     double day2Avg, double day2MaxCommute, double day2exerciseHR,
  82.     double day3Avg, double day3MaxCommute, double day3exerciseHR,
  83.     double day4Avg, double day4MaxCommute, double day4exerciseHR,
  84.     double day5Avg, double day5MaxCommute, double day5exerciseHR, double average[10],
  85.     int daysExercised[10], double estMaxHR[10], double ratioMaxToEst[10], double ratioHighToMax[10])
  86. {
  87.     ifstream inFile;
  88.  
  89.     //Open file
  90.     inFile.open("HR.txt");
  91.     if (inFile.fail())
  92.     {
  93.         cout << "No Such File" << endl;
  94.         exit(100);
  95.     }
  96.  
  97.  
  98.     while (!inFile.eof())
  99.     {
  100.         for (int i = 0; i < 10; i++) {
  101.             inFile >> subject[i];
  102.             inFile >> maxHR;
  103.             inFile >> age;
  104.             inFile >> day1Avg >> day1MaxCommute >> day1exerciseHR >> day2Avg >> day2MaxCommute >> day2exerciseHR >> day3Avg >> day3MaxCommute >> day3exerciseHR
  105.                 >> day4Avg >> day4MaxCommute >> day4exerciseHR >> day5Avg >> day5MaxCommute >> day5exerciseHR;
  106.             calculation(maxHR, age, i, day1Avg, day1MaxCommute, day1exerciseHR, day2Avg, day2MaxCommute, day2exerciseHR, day3Avg, day3MaxCommute, day3exerciseHR,
  107.                 day4Avg, day4MaxCommute, day4exerciseHR, day5Avg, day5MaxCommute, day5exerciseHR, average, daysExercised, estMaxHR, ratioMaxToEst, ratioHighToMax);
  108.         }
  109.  
  110.     }
  111.  
  112. }
  113. //calculation
  114. /*Pre: maxHR - number of maximum heart rate
  115. age - number of age of the subject
  116. i - order number of the arrays
  117. day'n'Avg - the average commuting heart rate on day 'n'
  118. day'n'MaxCommute - maximum commuting heart on day 'n'
  119. day'n'exerciseHR - the number of exercise heart rate on day 'n'
  120. average - number of average commuting heart rate in 5 days
  121. daysExercised - number of days exercised
  122. extMaxHR - number of estimated heart rate
  123. ratioMaxToEst - ratio in percentage of measured maximum heart rate to calculated estimate maximum heart rate
  124. ratioHighToMax - ratio in percentage of measured maximum commuting heart rate to measured maximum heart rate
  125.  
  126. Post: none
  127.  
  128. Purpose: to calculate the average of commuting heart rate in 5 days
  129. to calculate the number of days exercised
  130. to calculate the number of estimated heart rate
  131. to calculate the ratio of measured maximum heart rate to calculated estimate maximum heart rate in percentage
  132. to calculate the ratio of measured maximum commuting heart rate to measured maximum heart rate in percentage
  133. */
  134.  
  135.  
  136. void calculation(int maxHR, int age, int i, double day1Avg, double day1MaxCommute, double day1exerciseHR,
  137.     double day2Avg, double day2MaxCommute, double day2exerciseHR,
  138.     double day3Avg, double day3MaxCommute, double day3exerciseHR,
  139.     double day4Avg, double day4MaxCommute, double day4exerciseHR,
  140.     double day5Avg, double day5MaxCommute, double day5exerciseHR, double average[10],
  141.     int daysExercised[10], double estMaxHR[10], double ratioMaxToEst[10], double ratioHighToMax[10]) {
  142.     if (day1Avg > 0 && day2Avg > 0 && day3Avg > 0 && day4Avg > 0 && day5Avg > 0) {
  143.         average[i] = (day1Avg + day2Avg + day3Avg + day4Avg + day5Avg) / 5;
  144.     }
  145.     else {
  146.         if (day1Avg <= 0) {
  147.             average[i] = (day2Avg + day3Avg + day4Avg + day5Avg) / 4;
  148.         }
  149.         if (day2Avg <= 0) {
  150.             average[i] = (day1Avg + day3Avg + day4Avg + day5Avg) / 4;
  151.         }
  152.         if (day3Avg <= 0) {
  153.             average[i] = (day2Avg + day1Avg + day4Avg + day5Avg) / 4;
  154.         }
  155.         if (day4Avg <= 0) {
  156.             average[i] = (day2Avg + day3Avg + day1Avg + day5Avg) / 4;
  157.         }
  158.         if (day5Avg <= 0) {
  159.             average[i] = (day2Avg + day3Avg + day4Avg + day1Avg) / 4;
  160.         }
  161.     }
  162.     int days = 5;
  163.     if (day1exerciseHR == 0) {
  164.         days--;
  165.     }
  166.     if (day2exerciseHR == 0) {
  167.         days--;
  168.     }
  169.     if (day3exerciseHR == 0) {
  170.         days--;
  171.     }
  172.     if (day4exerciseHR == 0) {
  173.         days--;
  174.     }
  175.     if (day5exerciseHR == 0) {
  176.         days--;
  177.     }
  178.     daysExercised[i] = days;
  179.     estMaxHR[i] = 220 - age;
  180.     ratioMaxToEst[i] = maxHR / estMaxHR[i] * 100;
  181.     double highestCommute = 0;
  182.     if (day1MaxCommute > highestCommute) {
  183.         highestCommute = day1MaxCommute;
  184.     }
  185.     if (day2MaxCommute > highestCommute) {
  186.         highestCommute = day2MaxCommute;
  187.     }
  188.     if (day3MaxCommute > highestCommute) {
  189.         highestCommute = day3MaxCommute;
  190.     }
  191.     if (day4MaxCommute > highestCommute) {
  192.         highestCommute = day4MaxCommute;
  193.     }
  194.     if (day5MaxCommute > highestCommute) {
  195.         highestCommute = day5MaxCommute;
  196.     }
  197.     ratioHighToMax[i] = highestCommute / maxHR * 100;
  198. }
  199.  
  200. /*
  201. Pre:
  202. */
  203. void sortSelect(int arr[], int num, double average[10],
  204.     int daysExercised[10], double estMaxHR[10], double ratioMaxToEst[10], double ratioHighToMax[10])
  205. {
  206.     int current; int walker;
  207.     int smallestIndex;
  208.     int temp, temp2;
  209.     double temp1, temp3, temp4, temp5;
  210.  
  211.     for (current = 0; current < num - 1; current++)
  212.     {
  213.         smallestIndex = current;
  214.         for (walker = current; walker < num; walker++)
  215.         {
  216.             if (arr[walker] < arr[smallestIndex])
  217.                 smallestIndex = walker;
  218.         }//for walker
  219.  
  220.          //Swap to position smallest at what is the current position
  221.         temp = arr[current];
  222.         arr[current] = arr[smallestIndex];
  223.         arr[smallestIndex] = temp;
  224.         temp1 = average[current];
  225.         average[current] = average[smallestIndex];
  226.         average[smallestIndex] = temp1;
  227.         temp2 = daysExercised[current];
  228.         daysExercised[current] = daysExercised[smallestIndex];
  229.         daysExercised[smallestIndex] = temp2;
  230.         temp3 = estMaxHR[current];
  231.         estMaxHR[current] = estMaxHR[smallestIndex];
  232.         estMaxHR[smallestIndex] = temp3;
  233.         temp4 = ratioMaxToEst[current];
  234.         ratioMaxToEst[current] = ratioMaxToEst[smallestIndex];
  235.         ratioMaxToEst[smallestIndex] = temp4;
  236.         temp5 = ratioHighToMax[current];
  237.         ratioHighToMax[current] = ratioHighToMax[smallestIndex];
  238.         ratioHighToMax[smallestIndex] = temp5;
  239.     }//for current
  240.     return;
  241. }
  242. /*
  243. pre: none
  244. post: output of the header
  245. purpose: to post output of the header
  246. */
  247. //header output
  248. void headerOutput(ostream& out) {
  249.     out << "COMMUTING AND EXERCISE HEART RATE SUMMARY" << endl << endl;
  250.     out << "SUBJECT     AVERAGE      DAYS         ESTIMATED         %MEASURED          %MAX" << endl;
  251.     out << "NUMBER      COMMUTING    EXERCISED     MAX HR              TO              COMMUTING" << endl;
  252.     out << "            HR                                          ESTIMATED          HR TO" << endl;
  253.     out << "                                                        MAX HR             MEASURED" << endl;
  254. }
  255.  
  256. /*
  257. pre: subjectID - the number of subject ID
  258. average - number of average commuting heart rate in 5 days
  259. daysExercised - number of days exercised
  260. extMaxHR - number of estimated heart rate
  261. ratioMaxToEst - ratio in percentage of measured maximum heart rate to calculated estimate maximum heart rate
  262. ratioHighToMax - ratio in percentage of measured maximum commuting heart rate to measured maximum heart rate
  263. post: calculation from the given data
  264. purpose: to post the summary result from the calculated data
  265. */
  266. //result calculation output
  267. void resultOutput(ostream& out, int subjectID[], double average[10], int daysExercised[10], double estMaxHR[10], double ratioMaxToEst[10], double ratioHighToMax[10]) {
  268.     for (int i = 0; i < 10; i++) {
  269.         out << setprecision(1) << fixed << right << setw(5);
  270.         out << subjectID[i] << "\t    " << average[i] << "\t    " << daysExercised[i] << "\t\t" << estMaxHR[i] << "\t\t" << ratioMaxToEst[i] << "\t\t   " << ratioHighToMax[i] << endl;
  271.     }
  272. }
  273.  
  274. /*
  275. Kevin Tancho
  276. kevintancho@gmail.com
  277. LAB 7
  278. COMMUTING AND EXERCISE HEART RATE SUMMARY
  279.  
  280. SUBJECT     AVERAGE      DAYS         ESTIMATED         %MEASURED          %MAX
  281. NUMBER      COMMUTING    EXERCISED     MAX HR              TO              COMMUTING
  282. HR                                          ESTIMATED          HR TO
  283. MAX HR             MEASURED
  284. 1124        112.5           4           184.0            96.7              81.5
  285. 2438        118.0           4           194.0           103.1              82.5
  286. 2776        106.3           2           175.0           101.7              81.5
  287. 3521        113.8           2           169.0           107.1              87.8
  288. 4545        112.2           4           168.0           113.1              68.9
  289. 4915        127.7           0           193.0            95.9              91.4
  290. 6231        138.0           2           192.0            94.3              94.5
  291. 7345        132.2           3           184.0           106.0              86.7
  292. 8762        120.6           3           192.0            96.9              79.6
  293. 9439        126.2           2           199.0            99.0              80.7
  294. Press any key to continue . . .
  295. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement