In: Computer Science
A research institute specializing in physiology is conducting a study on the strain and effects of commuting 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.
In this program you will write a C++ program to analyze a small subset of the data that has been collected.
INPUT: Take the input from the file HR.txt
Input data format:
The file consists of three columns, with six lines of data for each person, test subject.
The first line stores:
i. column 1 - each person’s ID number (integer),
ii. column 2 - clinically measured maximum heart rate (integer), and
iii. column 3 - age (integer).
For the next five lines, each contains:
(a) column 1 - the day’s average commuting heart rate
(b) column 2 - maximum commuting heart rate, and
(c) column 3 - exercise heart rate
for five consecutive working days.
Then, the six-line partition is repeated for the next person.
At times the heart rate monitors reacted to nearby power lines or other signal interference, 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 ID numbers and five for the calculated values as described below.
Using this information, calculate
OUTPUT: Output should be in the following format, figures aligned in "pretty" columns, and sorted by subject numberfrom low to high
Steps:
Checkpoints:
HR.txt
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
Screenshot
Program
//Header files
#include <iostream>
#include<fstream>
#include<string>
#include<iomanip>
using namespace std;
#define MAX 10
//Function prototypes
void get_Data(ifstream&, int*, double*, int*, int*, double*,
double*);
void sort_Data(int*, double*, int*, int*, double*, double*);
void header(ofstream&);
void output(ofstream&, const int[], const double[], const
int[], const int[], const double[], const double[]);
int main()
{
//Create parallel arrays
int ids[MAX];
double wklyAvgs[MAX];
int daysExercised[MAX];
int estmatedMaxHeartRate[MAX];
double heartRateRatio[MAX];
double highestHeartRateRation[MAX];
//Input file
ifstream in("HR.txt");
//File not open check
if (!in) {
cout << "File not found!!!"
<< endl;
exit(0);
}
//Read data from file to parrallel arrays
get_Data(in,ids, wklyAvgs, daysExercised,
estmatedMaxHeartRate, heartRateRatio,
highestHeartRateRation);
//Close file
in.close();
//Sort using parrallel arrays
sort_Data(ids, wklyAvgs, daysExercised,
estmatedMaxHeartRate, heartRateRatio,
highestHeartRateRation);
//Output file path
ofstream out("output.txt");
//Write header
header(out);
//Write data
output(out, ids, wklyAvgs, daysExercised,
estmatedMaxHeartRate, heartRateRatio,
highestHeartRateRation);
//Close file
out.close();
}
//Function to get data from file and store ino parallel
arrays
void get_Data(ifstream& in, int* ids, double* wAvg,int* days,
int* estimate, double* hrate, double* maxHrate) {
int i = 0;
while (!in.eof()) {
int measuredHR, age,
wklyCommutingCnt=0, wklyMaxCommuting = 0,exerciseCnt=0,
maxCommuting;
double wklyAvgCommuting =
0,avgCommuting,exercise;
in >>
ids[i]>>measuredHR>>age;
estimate[i] = 220 - age;
for (int j = 0; j < 5; j++)
{
in >>
avgCommuting >> maxCommuting >> exercise;
if (avgCommuting
!= -1) {
wklyAvgCommuting += avgCommuting;
wklyCommutingCnt++;
}
if
(wklyMaxCommuting<maxCommuting) {
wklyMaxCommuting=maxCommuting;
}
if (exercise !=
0) {
exerciseCnt++;
}
}
wAvg[i] = wklyAvgCommuting /
wklyCommutingCnt;
days[i] = exerciseCnt;
hrate[i] = (double(measuredHR) /
estimate[i]) * 100;
maxHrate[i] =
((double)wklyMaxCommuting / measuredHR) * 100;
string line;
getline(in, line);
i++;
}
}
//Function to sort array using id
void sort_Data(int* ids, double* wAvg, int* days, int* estimate,
double* hrate, double* maxHrate) {
for (int i = 0; i < MAX; i++)
{
for (int j = i + 1; j < MAX;
j++)
{
if (ids[j] <
ids[i])
{
int temp = ids[i];
ids[i] = ids[j];
ids[j] = temp;
double tempAvg = wAvg[i];
wAvg[i] = wAvg[j];
wAvg[j] = tempAvg;
int tempDay = days[i];
days[i] = days[j];
days[j] = tempDay;
int tempEstimate = estimate[i];
estimate[i] = estimate[j];
estimate[j] = tempEstimate;
double tempRate = hrate[i];
hrate[i] = hrate[j];
hrate[j] = tempRate;
double tempMax = maxHrate[i];
maxHrate[i] = maxHrate[j];
maxHrate[j] = tempMax;
}
}
}
}
//Write header into file
void header(ofstream& out) {
out << "Person's ID
WeeklyAvgCommuting
DaysExercised
EstimatedMaxHeartRate
PercentageHeartRate PercentageMaxHeartRate"
<< endl;
}
//Write data into file
void output(ofstream& out, const int ids[], const double
wAvg[], const int days[], const int estimated[], const double
hrate[], const double maxHRate[]) {
for (int i = 0; i < MAX; i++) {
out << fixed <<
setprecision(2) << ids[i] << setw(20) << wAvg[i]
<< setw(20) << days[i] << setw(20)
<<
estimated[i] << setw(25) << hrate[i] << setw(26)
<< maxHRate[i] << endl;
}
}
---------------------------------------------------------------
Output.txt
Person's ID
WeeklyAvgCommuting
DaysExercised
EstimatedMaxHeartRate
PercentageHeartRate
PercentageMaxHeartRate
1124
112.54
4
184
96.74
81.46
2438
118.00
4
194
103.09
82.50
2776
106.28
2
175
101.71
81.46
3521
113.82
2
169
107.10
87.85
4545
112.22
4
168
113.10
68.95
4915
127.70
0
193
95.85
91.35
6231
137.96
2
192
94.27
94.48
7345
132.22
3
184
105.98
86.67
8762
120.58
3
192
96.88
79.57
9439
126.20
2
199
98.99
80.71