Question: Based on the code below, will need to make some modifications to execute the following in order to make it a full dynamic memory (in

Based on the code below, will need to make some modifications to execute the following in order to make it a full dynamic memory (in C programming):

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.

#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;

}

Used the following as inputs:

Lafredo 65. 130.

Elsa 64. 120.

Jenna 76. 173.

Sidney 86. 212.

Berta 61. 128.

Kylie 74. 183.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!