Question: Use the gdb tool to find and fix the problem of the given program, average.c . It should output: The average is 864.5 when run

Use the gdb tool to find and fix the problem of the given program, average.c . It should output:

The average is 864.5

when run with the given input.txt file as ./average input.txt . Create a Makefile to compile the program.

#include  #include  #define MAX_SIZE 20 /* maximum 20 digits in file */ #define MAX_LINE 6 /* maximum 4 digits (+ 1 for newline) for each number. fgets reads n-1 chars*/ void read_numbers(FILE* fp, int numbers[], int* size){ *size = 0; char line[MAX_LINE]; while(fgets(line, MAX_LINE , fp) != NULL){ numbers[(*size)++] = atoi(line); //note that this is missing error checking } } double average(FILE* fp, char* fileName){ int size; int numbers[MAX_SIZE]; double sum = 0; read_numbers(fp, numbers, &size); for(int i = 0; i < size++; i++) sum += numbers[i]; return sum/size; } int main(int argc, char* argv[]){ if (argc != 2){ fprintf(stdout, "Usage: ./avg  "); exit(EXIT_FAILURE); } FILE* input; input=fopen(argv[1], "r"); if (input == NULL){ fprintf(stderr, "Problem opening file %s ", argv[1]); exit(EXIT_FAILURE); } double avg; avg = average(input, argv[1]); printf("The average is %g ", avg); return 0; }

input.txt =

865 3354 864 45 35 24

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!