Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Using C, This code needs to be modified to use malloc() and calloc() (each malloc and calloc statement need a free()) instead of structs to
Using C,
This code needs to be modified to use malloc() and calloc() (each malloc and calloc statement need a free()) instead of structs to read in file data. Here is an example of what is in the file:
F150 5.4 28000 white RAM1500 5.7 32000 orange
Here is the code that needs to be modified:
#include#include #include struct data { char str1[30]; float x; int n; char str2[30]; }; struct data d[100]; //Reading the data from the hw2.data file int inputRecord(struct data *d, const char *Filename) { int i, count=0; FILE *hw2Data; hw2Data = (fopen(Filename, "r")); if (hw2Data == NULL) { printf("Unable to open file"); exit(0); } do { fscanf(hw2Data, "%s%f%d%s", d[count].str1, &d[count].x, &d[count].n, d[count].str2); count ++; }while(getc(hw2Data) != EOF); return count - 1; } //Printing the data void printData(struct data *d, int size) { int i; for(i=0; i d[j].x) { t = d[i].x; d[i].x = d[j].x; d[j].x = t; p = d[i].n; d[i].n = d[j].n; d[j].n = p; strcpy(temp, d[i].str1); strcpy(d[i].str1, d[j].str1); strcpy(d[j].str1, temp); strcpy(temp, d[i].str2); strcpy(d[i].str2, d[j].str2); strcpy(d[j].str2, temp); } } } } //Sort the data by float in descending order void floatDesc(struct data d[], int size) { int i,j,p; float t; char temp[30]; for (i=0;i d[j].n) { t = d[i].x; d[i].x = d[j].x; d[j].x = t; p = d[i].n; d[i].n = d[j].n; d[j].n = p; strcpy(temp, d[i].str1); strcpy(d[i].str1, d[j].str1); strcpy(d[j].str1, temp); strcpy(temp, d[i].str2); strcpy(d[i].str2, d[j].str2); strcpy(d[j].str2, temp); } } } } //Sort the data by int in descending order void intDesc(struct data d[], int size) { int i,j,p; float t; char temp[30]; for (i=0;i
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