Question
#include #include #include char *in_file = input_file.txt; char *out_file = output_file.txt; #define MAX_ENTRIES 10 int main() { FILE *in_fp, *out_fp; char Name[10][30]; float Height[10], Weight[10],
#include
#include
#include
char *in_file = "input_file.txt";
char *out_file = "output_file.txt";
#define MAX_ENTRIES 10
int main()
{
FILE *in_fp, *out_fp;
char Name[10][30];
float Height[10], Weight[10], BMI[10]; // Static allocation
int i = 0, j;
time_t cur_time = time(NULL);
// Open some files for reading and writing
if((in_fp = fopen(in_file, "r")) == NULL) {
fprintf(stderr, "Cannot open input file <%s> ", in_file);
return -1;
}
if((out_fp = fopen(out_file, "w")) == NULL) {
fprintf(stderr, "Cannot open output file <%s> ", out_file);
return -2;
} else {
fprintf(out_fp, "Rundate %s ", ctime(&cur_time));
fflush(out_fp);
}
// Do something with the files.
while (i < MAX_ENTRIES) {
j = fscanf(in_fp, "%s %f %f", Name[i], &Height[i], &Weight[i]);
if (j < 1) {
printf ("Got a zero on read, must be done ");
break;
}
++i;
}
for (j=0; j
BMI[j] = 703.*Weight[j]/(Height[j]*Height[j]);
}
for (j=0; j
fprintf(out_fp, "Name: %s ", Name[j]);
fprintf(out_fp, "Height: %6.2f ", Height[j]);
fprintf(out_fp, "Weight: %6.2f ", Weight[j]);
fprintf(out_fp, "BMI: %6.2f ", BMI[j]);
}
// Before exiting, close the files.
if (fclose(in_fp) == EOF)
return -3;
if (fclose(out_fp) == EOF)
return -4;
return 0;
}
usign the code above
Make the code first read a single integer from its input file - this defines the number of patients it is to read.
Name, Height, Weight, and BMI must all now be declared as pointers (Name is a pointer pointer) rather than automatic arrays. Also add Age and MaxHeart as float pointers.
Based on the integer you first read, perform dynamic allocation for the variables above that are actually going to be read using scanf() (not for calculated variables).
Within the while loop currently bounded by MAX_ENTRIES (which will now run to the integer you read first), read all the data using scanf() into their appropriate variables (suggestion: read Age at the end of the other variables).
In a single call to a function, compute BMI for all the patients. Then in another call to a function, compute MaxHeart for all the patients. These functions will need to allocate memory as needed.
Finally, revise the output such that a function is called to output everything - that is, everything that's currently in the loop, plus Age & MaxHeart, to the output file.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started