Question
Hello, I am having trouble with this. Could you point out the syntax and bugs in this program that needs to be fixed so it
Hello, I am having trouble with this. Could you point out the syntax and bugs in this program that needs to be fixed so it produces the following output.
---------------------------------------------------------------------*/ #include #include // Some symbolic constants #define TXT_INPUT_FILE "data.txt" #define NUM 3 // number of computed values // The following are indexes into the array // containing computed values #define MINIX 0 #define MAXIX 1 #define AVGIX 2 // For creating bin file #define MAX_NUM_VALUES 500 // up to 500 values are saved in the bin file #define BIN_FILE "data1.bin" // function prototypes void analyseFile(FILE *, double *); void saveInBinFile(FILE *); /*--------------------------------------------------------------------- Function: main Description: Main opens a text file and calls analyseFile to determine the minimum, the maximum, and the average of all values in the file. Results are displayed. Finally calls saveInBinFile to save the contents of the file into a binary file (the binary file is used in the next exercise). ------------------------------------------------------------------------*/ void main(void) { // Variable declarations FILE ifp; //input file pointer double outputVals[NUM]; // Open the input file ifp = fopen(TXT_INPUT_FILE, "r"); if(ifp == NULL) printf("Cannot open input file %s ",TXT_INPUT_FILE); else { // Analyze contents analyseFile(ifp, outputVals); // Display the results printf("Analysis of file data gives: "); printf(" Minimum: %.3f ", outputVals[MINIX]); printf(" Maximum: %.3f ", outputVals[MAXIX]); printf(" Average: %.3f ", outputVals[AVGIX]); saveInBinFile(ifp); fclose(ifp); } } /*----------------------------------------------------------------------- Function: analyseFile Parameters: fp - file pointer of file containing the data outPt - reference to array for storing output values Description: Scans all values in the file and computes three output values: the minimum value, the maximum value and the average value of the data in the file. ------------------------------------------------------------------------*/ void analyseFile(FILE *fp, double *outPt) { // Variable declarations double min, max; // minimum and maximum values of the data double avg; // average value int count; // To count number of values for computing average double val; // value read from the file // Start at the beginning of the file rewind(fp); // this is a safety check and allows this function // to be called multiple times with the same file. // Initialization of variables count = 0; fscanf(fp,"%lf",&val); // Read the first value min = val; max = DBL_MAX; // initialize min/max avg = 0.0; // Initialize to 0 for summing values while(feof(fp) = 0) // Not end of file { // Minimum value if(min > val) min = val; if(max
/*----------------------------------------------------------------------- Function: saveInBinFile Parameters: fp - file pointer of input file containing the data in text form Description: Copies all data in the txt file into an array and then saves the data into a bin file. ------------------------------------------------------------------------*/ void saveInBinFile(FILE *fp) { // Declarations double values[MAX_NUM_VALUES]; // Cannot read more than MAX_NUM_VALUES int ix; // Indexes into array and counts number of values read FILE *binfp; // File pointer for binary file // Read in values into the array rewind(fp); // start at the beginning of the file for(ix = 0; ix Analysis of file data gives inimum: -19.925 Maximum: 39.982 Average: 9.689 Process returned 0 (0x0) execution time 0.007s ress any key to cont inue
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