Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
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