Program in C bool load_data(char*, int *, double *,char *, int ): This function
ID: 3824512 • Letter: P
Question
Program in C
bool load_data(char*, int *, double *,char *, int ): This function takes the input file name, integer, double and character pointers and the size as integer. It opens the input file. If unable to open it, returns false. Otherwise load the account information from the text file into the integer, double and character arrays and return true at the end. The first char pointer is the string holding the name of the file to open, int * is the account ID array, double * is the amount array, the second char * is the designation array and the last int is the size or the number of records. Use fscanf or other library functions to read in the data.
void print_data(int *, double *, char *, int): This function takes integer array , double array, character array and the integer size and displays the data on the console stored in these arrays as shown in the sample output below. Use proper output formatting and heading to display the output. Remember this function prints the data on the screen.
Explanation / Answer
Please find my implementation of both methods.
Please let me know in case of any issue.
bool load_data(char *file, int *id, double *amt,char *desig, int n){
int i = 0;
FILE * fp(file, "r");
// error check in opening file
if(fp == NULL){
return false;
}
// trading designation, id and amt in each ineration
while(i < n){
fscanf(fp, "%s%d%lf", &desig[i], &id[i], &amt[i]);
i++;
}
fclose(fp); // closing file
return true;
}
void print_data(int *id, double *amt, char *desig, int n){
int i;
for(i=0; i<n; i++){
printf("%d %.3lf %s ", id[i], amt[i], desig[i]);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.