LAB 7 Biomedical Research A research institute specializing in physiology is con
ID: 3805441 • Letter: L
Question
LAB 7
Biomedical Research
A research institute specializing in physiology is conducting a study on the strain and effects ofcommuting trips done on a bicycle or walking. When the physiological strain of commuting trips hasbeen studied, the results will be used in exercise prescriptions, which can then use commuting as partof an exercise regimen.In this program you will write a C++ program to analyze a small subset of the data that has beencollected.INPUT: Redirect the input from the file HR.txt.The file consists of three columns, with six lines of data for each person. The first line stores eachperson’s ID number (integer), clinically measured maximum heart rate (integer), and age (integer).The following five lines contain the day’s average commuting heart rate, maximum commuting heartrate, and exercise heart rate for five consecutive working days. Then, the six line sequence is repeatedfor the next person. At times the heart rate monitors reacted to nearby power lines or other objects,and gave false readings. These incorrect measurements were rejected and show up in the data file as-1. On the days the person did not exercise, the exercise heart rate value is zero.PROCESSING: Use precisely six parallel arrays: one for subject numbers and five for the calculatedvalues as described below.
Look at data for subject 4545 as this is discussed:
4545 190 52114.8 130 113.1102.6 131 0.0117.4 129 149.1-1 -1 114.0114.1 123 119.5
Using this information, calculate1. Average of the average commuting heart rates for each person.
For subject 4545 this would be (114.8 + 102.6 + 117.4 + 114.1)/4 since there was a daythat no reading was available.
2. Number of days that the person exercised on his/her own.
For subject 4545 this is 4 since one of the days is 0.
3. Estimated maximum heart rate = 220 – age.
For subject 4545 this is 220 – 52 = 168
4. Ratio (%) of measured maximum heart rate to estimated maximum heart rate.The maximum heart rate for each person has been measured during a maximumoxygen uptake test in the laboratory. During the test, the person exercised withincreasing intensity until exhaustion, and then the maximum heart rate was measuredclose to the end of the test. Using this measured maximum heart rate and the estimatedmaximum heart rate, calculate the percentage of the measured maximum heart ratewith respect to the estimated maximum heart rate. Note that this percentage canexceed 100%.
For subject 4545, 190/168 * 100 = 113.1%
5. Ratio (%) of highest commuting heart rate to measured maximum heart rate. Find the highestmaximum commuting heart rate for a person, and then calculate the percentage of this valuewith respect to the measured maximum heart rate.
For subject 4545, 131/ 190 = 69.4%
OUTPUT: Output should be in the following format and sorted by subject number from low to high:
COMMUTING AND EXERCISE HEART RATE SUMMARYSUBJECT AVERAGE DAYS ESTIMATED %MEASURED %MAXNUMBER COMMUTING EXERCISED MAX HR TO COMMUTING HR ESTIMATED HR TO MAX HR MEASURED
XXXX XXX.X X XXX XXX.X XXX.X…SAMPLE:
COMMUTING AND EXERCISE HEART RATE SUMMARYSUBJECT AVERAGE DAYS ESTIMATED %MEASURED %MAXNUMBER COMMUTING EXERCISED MAX HR TO COMMUTING HR ESTIMATED HR TO MAX HR MEASURED
1124 112.5 4 184 96.7 81.5…
The data the professor wants us to use is HR.txt
6231 181 28140.1 170 101.8128.4 156 0.0145.2 171 106.2131.3 165 0.0144.8 170 0.01124 178 36122.8 139 138.4119.6 142 0.0117.8 134 133.4115.3 145 134.287.2 109 120.57345 195 36128.4 151 104.6120.5 153 0.0134.5 166 140.4127.4 156 0.0150.3 169 158.59439 197 21121.5 143 112.2128.9 145 0.0126.1 159 134.5123.0 152 0.0131.5 147 0.04545 190 52114.8 130 113.1102.6 131 0.0117.4 129 149.1-1 -1 114.0114.1 123 119.52438 200 26123.0 165 105.4118.2 130 122.0121.7 136 124.5116.1 130 0.0111.0 152 103.52776 178 45110.3 120 129.1107.8 132 0.0111.5 145 135.0-1 -1 0.095.5 119 0.08762 186 28122.7 146 151.9116.0 137 0.0119.9 145 0.0123.3 148 150.0121.0 142 156.34915 185 27120.3 169 0.0130.2 150 0.0123.0 158 0.0133.4 152 0.0131.6 159 0.03521 181 51108.3 133 119.3-1 -1 0.0117.6 147 125.3106.7 131 0.0122.7 159 0.0
Can you please write the code without using global variables, she does not allow global variables at all.
Explanation / Answer
Soltion:
Note: Use the input file as spefied in the question as three colums
C++ ode
#include<iostream>
#include<fstream>
#include<cstdlib>
#include <iomanip>
#define LIMIT 1000
using namespace std;
//Function prototypes
int readFile(ifstream& fileIn, int Ident[], double commonAVG[], int Exdays[],int maxEstHr[], double maxRatio[],double highRatio[]);
void ratioCalc(int HRmeassured,int maxEstHr,double highMaxCommHR,double &maxRatio,double &highRatio);
void dataSort(int cnt, int Ident[], double commonAVG[], int Exdays[],int maxEstHr[], double maxRatio[],double highRatio[]);
//To print the top of the output file
void setOutfileTop(ostream &outfile)
{
outfile<<"Number of the Subject Commuting Heart Rate Average Number of Days Exercised Max Heart Rate Estimated % Estimated Max Heart Rate Measured % Measured Max Commuting"<<endl;
}
void writeData(ostream &outfile,int cnt, int Ident[], double commonAVG[], int Exdays[],int maxEstHr[], double maxRatio[],double highRatio[])
{
for(int itr=0;itr<cnt;itr++)
{
//Specify the formatting
outfile<<Ident[itr]<<setw(30)<<commonAVG[itr]<<setw(30)<<Exdays[itr]<<setw(30);
outfile<<maxEstHr[itr]<<setw(50)<<maxRatio[itr]<<setw(40)<<highRatio[itr]<<endl;
}
}
//the amin function
int main(void)
{
ifstream fileIn;
ofstream fileOut;
int Ident[LIMIT];
double commonAVG[LIMIT];
int Exdays[LIMIT];
int maxEstHr[LIMIT];
double maxRatio[LIMIT];
double highRatio[LIMIT];
int cnt = 0;
fileIn.open("HR.txt");
if(fileIn.fail())
{
cout << "No file exist"<< endl;
exit(1);
}
fileOut.open("HRout.txt");
if(fileIn.fail())
{
cout << "Output file can't be created"<< endl;
exit(1);
}
cnt = readFile(fileIn, Ident, commonAVG, Exdays, maxEstHr, maxRatio, highRatio);
fileIn.close();
dataSort(cnt, Ident, commonAVG, Exdays, maxEstHr, maxRatio, highRatio);
setOutfileTop(fileOut);
writeData(fileOut,cnt, Ident, commonAVG, Exdays, maxEstHr, maxRatio, highRatio);
fileOut.close();
system("pause");
return 0;
}
//method readfile definition
int readFile(ifstream& fileIn, int Ident[], double commonAVG[], int Exdays[],
int maxEstHr[], double maxRatio[],double highRatio[])
{
int cnt = 0;
int age,HRmeassured,AVGvalid=0;
double avergCommHR,maxCommHR,exHR,highMaxCommHR;
while (cnt < LIMIT && !fileIn.eof())
{
commonAVG[cnt]=0;
Exdays[cnt]=0;
maxEstHr[cnt]=0;
maxRatio[cnt]=0;
highRatio[cnt]=0;
AVGvalid=0;
highMaxCommHR=0;
fileIn >> Ident[cnt]>> HRmeassured >> age;
cout << Ident[cnt] << " " << HRmeassured <<" " << age << endl;
maxEstHr[cnt]=220-age;
for(int itr =0; itr<5; itr++)
{
fileIn >> avergCommHR >> maxCommHR >> exHR;
cout <<" "<<avergCommHR << " " << maxCommHR << " " << exHR<<endl;
if(exHR!=0)
Exdays[cnt]++;
if(avergCommHR!=-1)
{
commonAVG[cnt]+=avergCommHR;
AVGvalid++;
}
if(maxCommHR>highMaxCommHR)
highMaxCommHR=maxCommHR;
ratioCalc(HRmeassured,maxEstHr[cnt],highMaxCommHR, maxRatio[cnt], highRatio[cnt]);
}
commonAVG[cnt]/=AVGvalid;
cnt ++;
}
return cnt;
}
void ratioCalc(int HRmeassured,int maxEstHr ,double highMaxCommHR,double &maxRatio,double &highRatio)
{
maxRatio=HRmeassured*100.0/maxEstHr;
highRatio=highMaxCommHR*100.0/HRmeassured;
}
void dataSort(int cnt, int Ident[], double commonAVG[], int Exdays[],int maxEstHr[], double maxRatio[],double highRatio[])
{
int tem;
double tem1;
for (int vi = 0; vi < cnt; vi++)
{
int minIdx = vi;
for (int itr = vi+1; itr < cnt; itr++)
{
if (Ident[itr] < Ident[minIdx])
{
minIdx= itr;
}
}
if(minIdx != vi)
{
tem=Ident[vi];
Ident[vi]=Ident[minIdx];
Ident[minIdx]=tem;
tem=commonAVG[vi];
commonAVG[vi]=commonAVG[minIdx];
commonAVG[minIdx]=tem;
tem=Exdays[vi];
Exdays[vi]=Exdays[minIdx];
Exdays[minIdx]=tem;
tem=maxEstHr[vi];
maxEstHr[vi]=maxEstHr[minIdx];
maxEstHr[minIdx]=tem;
tem1=maxRatio[vi];
maxRatio[vi]=maxRatio[minIdx];
maxRatio[minIdx]=tem1;
tem1=highRatio[vi];
highRatio[vi]=highRatio[minIdx];
highRatio[minIdx]=tem1;
}
}
}
Sample Input File:
6231 181 28
140.1 170 101.8
128.4 156 0.0
145.2 171 106.2
131.3 165 0.0
144.8 170 0.0
1124 178 36
122.8 139 138.4
119.6 142 0.0
117.8 134 133.4
115.3 145 134.2
87.2 109 120.5
7345 195 36
128.4 151 104.6
120.5 153 0.0
134.5 166 140.4
127.4 156 0.0
150.3 169 158.5
9439 197 21
121.5 143 112.2
128.9 145 0.0
126.1 159 134.5
123.0 152 0.0
131.5 147 0.0
4545 190 52
114.8 130 113.1
102.6 131 0.0
117.4 129 149.1
-1 -1 114.0
114.1 123 119.5
2438 200 26
123.0 165 105.4
118.2 130 122.0
121.7 136 124.5
116.1 130 0.0
111.0 152 103.5
2776 178 45
110.3 120 129.1
107.8 132 0.0
111.5 145 135.0
-1 -1 0.0
95.5 119 0.0
8762 186 28
122.7 146 151.9
116.0 137 0.0
119.9 145 0.0
123.3 148 150.0
121.0 142 156.3
4915 185 27
120.3 169 0.0
130.2 150 0.0
123.0 158 0.0
133.4 152 0.0
131.6 159 0.0
3521 181 51
108.3 133 119.3
-1 -1 0.0
117.6 147 125.3
106.7 131 0.0
122.7 159 0.0
Sample Output:
Number of the Subject Commuting Heart Rate Average Number of Days Exercised Max Heart Rate Estimated % Estimated Max Heart Rate Measured % Measured Max Commuting
1124 112.54 4 184 96.7391 81.4607
2438 118 4 194 103.093 82.5
2776 106.275 2 175 101.714 81.4607
3521 113.825 2 169 107.101 87.8453
4545 112.225 4 168 113.095 68.9474
4915 127.7 0 193 95.8549 91.3514
6231 137 2 192 94.2708 94.4751
7345 132 3 184 105.978 86.6667
8762 120 3 192 96.875 79.5699
9439 126 2 199 98.995 80.7107
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.