Code:
#include <stdio.h>
#include <stdlib.h>
#define NUM_PATIENT 100
#define GIVEN_HR 220
#define LEN 6
void getdata(FILE*spData, int ID[], float AveOfAveHR[], int dayExcrsHR[], int EstHR[], float ratioEstHR[], float ratioMaxHR[], int* patient);
void CalcestHRandRatioEstHR( int num, int age, int EstHR[], int MaxHR, float ratioEstHR[]);
void SortData(int ID[], float AveOfAveHR[], int dayExcrsHR[], int EstHR[], float ratioEstHR[], float ratioMaxHR[], int patient);
void printHeader();
void printData(int ID[], float AveOfAveHR[], int dayExcrsHR[], int EstHR[], float ratioEstHR[], float ratioMaxHR[], int patient);
int main (void)
{
// Global declarations
FILE* spData;
int ID[NUM_PATIENT];
float AveOfAveHR[NUM_PATIENT];
int dayExcrsHR[NUM_PATIENT];
int EstHR[NUM_PATIENT];
float ratioEstHR[NUM_PATIENT];
float ratioMaxHR[NUM_PATIENT];
int num;
int patient;
// Statements
if((spData=fopen("HR.txt","r"))== NULL)
{
printf("\n Error opennning file!\n");
exit(100);
}
else
{
printf("opened file!Data processed!\n");
}
getdata(spData, ID, AveOfAveHR, dayExcrsHR, EstHR, ratioEstHR, ratioMaxHR, &patient);
SortData(ID, AveOfAveHR, dayExcrsHR, EstHR, ratioEstHR, ratioMaxHR, patient);
printHeader();
printData(ID, AveOfAveHR, dayExcrsHR, EstHR, ratioEstHR, ratioMaxHR, patient);
fclose(spData);
system("PAUSE");
return 0;
}
/*======================== getData =======================
Pre:
Post:
*/
void getdata(FILE* spData, int ID[], float AveOfAveHR[], int dayExcrsHR[], int EstHR[], float ratioEstHR[], float ratioMaxHR[], int* patient)
{
// Local Declarations
int MaxHR;
int age;
float aveCommuHR;
int maxCommuHr;
float excersHR;
int i =0;
int j;
int size = LEN;
int countave = 0;
float sumAveCommuHR = 0;
int countDayExcers = 0;
int HighestMaxCommuHR = 0;
int count = 0;
// Statements
while(i < NUM_PATIENT && (fscanf(spData,"%d %d %d", &ID[i], &MaxHR, &age)) != EOF)
{
for(j = 0; j < size; j++)
{
fscanf(spData,"%f %d %f", &aveCommuHR, &maxCommuHr, &excersHR);
if(aveCommuHR > 0)
countave++; // count number of average commute HR
sumAveCommuHR += aveCommuHR; // add sum of average commuting HR of days
if(excersHR > 0)
countDayExcers++; // count the day that patient excercise
if(maxCommuHr > HighestMaxCommuHR)
HighestMaxCommuHR = maxCommuHr; // determine the highest value of Max commuting HR
}
i++;
countave = 0;
sumAveCommuHR = 0;
countDayExcers = 0;
HighestMaxCommuHR = 0;
count++;
AveOfAveHR[i] = sumAveCommuHR / countave;
dayExcrsHR[i] = countDayExcers;
CalcestHRandRatioEstHR(i, age, EstHR, MaxHR, ratioEstHR);
ratioMaxHR[i] = (float)HighestMaxCommuHR/ (float)EstHR[i] * 100;
}
*patient = count;
return;
}
/*======================= CalcestHRandRatioEstHR ===========================
Pre:
Post:
*/
void CalcestHRandRatioEstHR( int num, int age, int EstHR[], int MaxHR, float ratioEstHR[])
{
// Statements
EstHR[num] = GIVEN_HR - age;
ratioEstHR[num] = ((float)MaxHR / (float)EstHR[num]) * 100;
return;
}
/* ====================== SortData ==========================================
Pre:
Post:
*/
void SortData(int ID[], float AveOfAveHR[], int dayExcrsHR[], int EstHR[], float ratioEstHR[], float ratioMaxHR[], int patient)
{
// Local Declerations
float tempOfFloat;
int tempOfInt;
int current;
int smallest;
int walk;
for(current = 0; current < NUM_PATIENT; current++)
{
smallest = current;
// one sort pass each loop
for( walk = current +1; walk < patient; walk++)
if(ID[walk] < ID[smallest])
smallest = walk;
//swap value of ID
tempOfInt = ID[current];
ID[current] = ID[smallest];
ID[smallest] = tempOfInt;
// swap value of AveOfAveHR
tempOfFloat = AveOfAveHR[current];
AveOfAveHR[current] = AveOfAveHR[smallest];
AveOfAveHR[smallest] = tempOfFloat;
//swap value of dayExcrsHR
tempOfInt = dayExcrsHR[current];
dayExcrsHR[current] = dayExcrsHR[smallest];
dayExcrsHR[smallest] = tempOfInt;
//swap value of estimate heart rate
tempOfInt = EstHR[current];
EstHR[current] = EstHR[smallest];
EstHR[smallest] = tempOfInt;
//swap value of ratioEstHR
tempOfFloat = ratioEstHR[current];
ratioEstHR[current] = ratioEstHR[smallest];
ratioEstHR[smallest] = tempOfFloat;
//swap value of ratioMaxHR
tempOfFloat = ratioMaxHR[current];
ratioMaxHR[current] = ratioMaxHR[smallest];
ratioMaxHR[smallest] = tempOfFloat;
}
return;
}
/*======================= printHeader ==========================
Pre:
Post:
*/
void printHeader()
{
//Local Declaration
FILE *spOUT;
//Statements
if(!(spOUT = fopen("output.txt", "w")))
printf("Error writing output.txt");
fprintf(spOUT, "COMMUTING AND EXERCISE HEART RATE SUMMARY\n\n");
fprintf(spOUT, "SUBJECT AVERAGE DAYS ESTIMATED %%MEASURED %%MAX\n");
fprintf(spOUT, "NUMBER COMMUTING EXERCISED MAX HR MAX HR TO COMMUTING\n");
fprintf(spOUT, " HR ESTIMATED HR TO\n");
fprintf(spOUT, " HR MEASURED\n");
fclose(spOUT);
return;
}
/*======================= printData ==========================
Pre:
Post:
*/
void printData(int ID[], float AveOfAveHR[], int dayExcrsHR[], int EstHR[], float ratioEstHR[], float ratioMaxHR[], int patient)
{
//Local Declaration
FILE *spOUT;
int i;
//Statements
if(!(spOUT = fopen("output.txt", "a")))
printf("Error writing output.txt");
for(i = 0; i < patient; i++)
{
fprintf(spOUT ,"%4d %5.1f %d %3d %5.1f %5.1f\n",
ID[i], AveOfAveHR[i], dayExcrsHR[i], EstHR[i], ratioEstHR[i], ratioMaxHR[i]);
}
fclose(spOUT);
return;
}