In this lab you are asked to create a C program to implement
In this lab, you are asked to create a C++ program to implement a bloodpressure class as defined below. The class reads from a blood pressure file (text), and calculates the average blood pressure for everyone. It also calculates the number of people in the file, the average of all of them, and the total number of blood pressures saved in the file. Practice to use a local variable, a static variable, and a global variable to store the results.
Define a global variable to store the total number of people in the file
Define a static variable in the average function to store the number of blood pressures
Print the output as the example below.
You need to create the program as shown in the lecture with three files: bloodpressure.h, bloodpressure.cpp, and Main.cpp to implement it and test it on the Main.cpp file. In the main() function, you need to create a bloodpressure object, load data from a file, calculate and print the average information as below.
----------------Input------------
ID Name How many Pressure measurements
4132 Tom 5 80 90 81 100 89 79
5123 Jim 4 100 110 120 121 122
4322 Mike 3 89 121 111
6544 Nancy 6 95 122 112 121 123 99
---------------Output-----------
ID Name Average Pressure
4132 Tom ???
5123 Jim ???
4322 Mike ???
6544 Nancy ???
The average blood pressure for all of them is ???
The total number of people in the file is ???
The total number of blood pressures in the file is ???
HEADER FILE:
#ifndef BLOODPRESSURE_H
#define BLOODPRESSURE_H
#include <string>
using namespace std;
class bloodpressure{
public:
bloodpressure();
void loadBP(string fileName);
float average(int SUM, int NUM);
void print();
private:
float totalAverage; // sum of all average blood pressures
int numberOfPeople; // total number of people in the file
int numberOfBP; // total number of blood pressures
};
#endif
Solution
// BloodPressure.h
#ifndef BLOODPRESSURE_H
#define BLOODPRESSURE_H
#include <string>
using namespace std;
class bloodpressure
{
public:
bloodpressure();
void loadBP(string fileName);
float average(int SUM, int NUM);
void print();
private:
float totalAverage; // sum of all average blood pressures
int numberOfPeople; // total number of people in the file
int numberOfBP; // total number of blood pressures
};
#endif
// BloodPressure.pp
#include \"BloodPressure.h\"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
bloodpressure::bloodpressure()
{
totalAverage = 0.0;
numberOfPeople = 0;
numberOfBP = 0;
}
float bloodpressure::average(int SUM, int NUM)
{
float avgResult = (float)SUM / NUM;
return avgResult;
}
void bloodpressure::loadBP(string filename)
{
string remaining;
ifstream infile(filename.c_str());
int pressure, totalSum = 0, totalmeasurements;
string name;
int inputId;
float avgResult = 0.0;
float inputAverage = 0.0;
if (infile.is_open())
{
cout << \"ID\\tName\\tAverage Pressure\" << endl;
while (true)
{
totalSum = 0;
avgResult = 0.0;
infile >> inputId;
cout << inputId << \"\\t\" ;
infile >> name;
cout << name << \"\\t\" ;
infile >> totalmeasurements;
for (int i = 0; i<totalmeasurements; i++)
{
infile >> pressure;
totalSum = totalSum + pressure;
numberOfBP++;
}
getline(infile,remaining);
avgResult = average(totalSum, totalmeasurements);
cout << avgResult << endl;
inputAverage = inputAverage + avgResult;
numberOfPeople++;
if(infile.eof())
break;
}
totalAverage = inputAverage/numberOfPeople;
infile.close();
}
}
void bloodpressure::print()
{
cout << \"\ \ The average blood pressure for all of them is \" << totalAverage << endl;
cout << \"The total number of people in the file is \" << numberOfPeople << endl;
cout << \"The total number of blood pressures in the file is \" << numberOfBP << endl;
}
// main.cpp
#include <iostream>
#include <string.h>
#include <fstream>
#include \"BloodPressure.h\"
using namespace std;
int main()
{
bloodpressure bp;
bp.loadBP(\"data.txt\");
bp.print();
return 0;
}
/*
output:
ID Name Average Pressure
4132 Tom 88
5123 Jim 112.75
4322 Mike 107
6544 Nancy 112
The average blood pressure for all of them is 104.938
The total number of people in the file is 4
The total number of blood pressures in the file is 18
*/



