C++ Program A research institute specializing in physiology is conducting a stud
ID: 3803990 • Letter: C
Question
C++ Program
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 has been studied, the results will be used in exercise prescriptions, which can then use commuting as part of an exercise regimen.
Write a C++ program to analyze a small subset of the data that has been collected.
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 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
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 sorted by subject number from low to high with the following:
Subject Number
Average Commuting Heart Rate
Days Exercised
Estimated Max Heart Rate
% Measured to Estimated Max Heart Rate
% Max Commuting to be Measured
Checkpoints:
No two-dimensional arrays, no menu function, and no structures may be used
1. Input function fills all five arrays while calling calculation function for each subject.
2. One and only one function must be used to calculate the percentage of measured maximum to estimated heart rate and the percentage of highest commuting heart rate to measure maximum heart rate
3. Use a selection sort to sort subject numbers. This needs to be a separate function.
4. A separate function must be used to output the table heading ONLY
5. Numerical output is done in a separate output function. Output must beformatted as shown. Redirect output to a file.
Contents of HR.txt, formatted as on the txt 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
This is my code so far.
#include<iostream>
#include<fstream>
#include<cstdlib>
#define MAX 1000
using namespace std;
int getData(ifstream& inFile, int id[], int hr[], int age[],
int dayAvg[], int maxHR[],
double estM[], double maxComm[]);
int main(void)
{
ifstream inFile;
int id[MAX];
int hr[MAX];
int age[MAX];
int dayAvg[MAX];
int maxHR[MAX];
double estM[MAX];
double maxComm[MAX];
int count = 0;
inFile.open("HR.txt");
if(inFile.fail())
{
cout << "No Such File"<< endl;
exit(1);
}
count = getData(inFile, id, hr, age, dayAvg, maxHR, estM, maxComm);
return 0;
}
int getData(ifstream& inFile, int id[], int hr[], int age[],
int dayAvg[], int maxHR[],
double estM[], double maxComm[])
{
int count = 0;
int heart, ageV;
double commHR, maxcommHR, exHR;
while (count < MAX&& !inFile.eof())
{
inFile >> id[count]>> heart >> ageV;
cout << id[count] << " " << heart <<" " << ageV << endl;
for(int i =0; i<5; i++)
{
inFile >> commHR >> maxcommHR >> exHR;
cout <<commHR << " " << maxcommHR << " " << exHR;
}
count ++;
inFile.ignore();
}
return count;
}
Please help me complete the whole problem. Im having hard time to write the rest of the code.
Im using C++ language. This is my last chance to ask so please help me.
Explanation / Answer
Here is the complete code. You may verify the values for subject number 4545. In the question 131/190 is wrongly calculated as 69.4 but actual is 68.94.
#include<iostream>
#include<fstream>
#include<cstdlib>
#include <iomanip>
#define MAX 1000
using namespace std;
int getData(ifstream& inFile, int id[], double avgCommHR[], int daysExcd[],int estMaxHR[], double ratioMaxEstHR[],double ratioHighMaxHR[]);
void calculateRatios(int measuredHR,int estMaxHR,double highMaxCommHR,double &ratioMaxEstHR,double &ratioHighMaxHR);
void sort(int count, int id[], double avgCommHR[], int daysExcd[],int estMaxHR[], double ratioMaxEstHR[],double ratioHighMaxHR[]);
//prints the header into output file
void printHeader(ostream &outfile)
{
outfile<<"Subject Number Average Commuting Heart Rate Days Exercised Estimated Max Heart Rate % Measured to Estimated Max Heart Rate % Max Commuting to be Measured"<<endl;
}
void printData(ostream &outfile,int count, int id[], double avgCommHR[], int daysExcd[],int estMaxHR[], double ratioMaxEstHR[],double ratioHighMaxHR[])
{
for(int i=0;i<count;i++)
{
//using setw() to specify column width so that output is neatly formatted
outfile<<id[i]<<setw(20)<<avgCommHR[i]<<setw(30)<<daysExcd[i]<<setw(20);
outfile<<estMaxHR[i]<<setw(30)<<ratioMaxEstHR[i]<<setw(45)<<ratioHighMaxHR[i]<<endl;
}
}
int main(void)
{
ifstream inFile;
ofstream outFile;
int id[MAX];
double avgCommHR[MAX]; //array to store calculated value of average commuting HR
int daysExcd[MAX]; //array to store number of days exercised
int estMaxHR[MAX]; //array to store estimated max HR
double ratioMaxEstHR[MAX]; //array to store ratio meassuredHR/estimated max HR
double ratioHighMaxHR[MAX]; //array to store highestmaxHR/measuredHR
int count = 0;
//open and check input file
inFile.open("HR.txt");
if(inFile.fail())
{
cout << "No Such input File HR.txt"<< endl;
exit(1);
}
//open and check output file
outFile.open("HRout.txt");
if(inFile.fail())
{
cout << "Output file HR-out.txt could not be created"<< endl;
exit(1);
}
count = getData(inFile, id, avgCommHR, daysExcd, estMaxHR, ratioMaxEstHR, ratioHighMaxHR);
inFile.close(); //close input file
//sort the data on subject number
sort(count, id, avgCommHR, daysExcd, estMaxHR, ratioMaxEstHR, ratioHighMaxHR);
printHeader(outFile); //print header in output file
printData(outFile,count, id, avgCommHR, daysExcd, estMaxHR, ratioMaxEstHR, ratioHighMaxHR);
outFile.close();
return 0;
}
//reads data from input file and returns the number of records read
int getData(ifstream& inFile, int id[], double avgCommHR[], int daysExcd[],
int estMaxHR[], double ratioMaxEstHR[],double ratioHighMaxHR[])
{
int count = 0;
int age,measuredHR,validAvg=0;
double avergCommHR,maxCommHR,exHR,highMaxCommHR;
while (count < MAX && !inFile.eof())
{
avgCommHR[count]=0;
daysExcd[count]=0;
estMaxHR[count]=0;
ratioMaxEstHR[count]=0;
ratioHighMaxHR[count]=0;
validAvg=0; //initialize number of valid average Commuting HR values
highMaxCommHR=0; //iititalize the highest maximum commuting HR
inFile >> id[count]>> measuredHR >> age;
cout << id[count] << " " << measuredHR <<" " << age << endl;
estMaxHR[count]=220-age; //calculate the Estimated maximum heart rate = 220 – age.
for(int i =0; i<5; i++)
{
inFile >> avergCommHR >> maxCommHR >> exHR;
cout <<" "<<avergCommHR << " " << maxCommHR << " " << exHR<<endl;
if(exHR!=0) //if 0 then not excercised that day ,otherwise increase the days
daysExcd[count]++; //increase the days
if(avergCommHR!=-1) //if no error in the value
{
avgCommHR[count]+=avergCommHR; //add it total of previous values
validAvg++;
}
if(maxCommHR>highMaxCommHR) //if this is bigger than previously saved highest maximum commuting HR
highMaxCommHR=maxCommHR;
calculateRatios(measuredHR,estMaxHR[count],highMaxCommHR, ratioMaxEstHR[count], ratioHighMaxHR[count]);
}
//the sum total was calculate above, divide it valid number of averages, to get average
avgCommHR[count]/=validAvg;
count ++;
}
return count;
}
//calculate the 2 ratios
void calculateRatios(int measuredHR,int estMaxHR ,double highMaxCommHR,double &ratioMaxEstHR,double &ratioHighMaxHR)
{
ratioMaxEstHR=measuredHR*100.0/estMaxHR;
ratioHighMaxHR=highMaxCommHR*100.0/measuredHR;
}
//selection sort on subject number
void sort(int count, int id[], double avgCommHR[], int daysExcd[],int estMaxHR[], double ratioMaxEstHR[],double ratioHighMaxHR[])
{
int tempi;
double tempd;
for (int j = 0; j < count; j++)
{
int minIdx = j;
// test elements after j to find the smallest
for (int i = j+1; i < count; i++)
{
// if this element is less, then it is the new minimum
if (id[i] < id[minIdx])
{
minIdx= i; // found new minimum; remember its index
}
}
if(minIdx != j)
{
//swap the records , all arrays because they are all related to one single record
tempi=id[j];
id[j]=id[minIdx];
id[minIdx]=tempi;
tempi=avgCommHR[j];
avgCommHR[j]=avgCommHR[minIdx];
avgCommHR[minIdx]=tempi;
tempi=daysExcd[j];
daysExcd[j]=daysExcd[minIdx];
daysExcd[minIdx]=tempi;
tempi=estMaxHR[j];
estMaxHR[j]=estMaxHR[minIdx];
estMaxHR[minIdx]=tempi;
tempd=ratioMaxEstHR[j];
ratioMaxEstHR[j]=ratioMaxEstHR[minIdx];
ratioMaxEstHR[minIdx]=tempd;
tempd=ratioHighMaxHR[j];
ratioHighMaxHR[j]=ratioHighMaxHR[minIdx];
ratioHighMaxHR[minIdx]=tempd;
}
}
}
output file
Subject Number Average Commuting Heart Rate Days Exercised Estimated Max Heart Rate % Measured to Estimated Max Heart Rate % Max Commuting to be Measured
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
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.