Answered step by step
Verified Expert Solution
Question
1 Approved Answer
//average.c #include #include #define MAX_SIZE 20 /* maximum 20 digits in file */ #define MAX_LINE 6 /* maximum 4 digits (+ 1 for newline) for
//average.c #includeUse the gdb tool to find and fix the problem of the given program, average.c. It should output: The average is 864.5 ./average input.txt . Create a Makefile to compile the when run with the given program. input.txt file as Files that need to be on GitHub: corrected version of average.c (you can call it average_corrected.c) Makefile to compile the program Demo: Grading for this part is all or nothing Show your TA how you used gdb to debug the program Run make on the new program and show your TA that your program behaves correctly now#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); /ote 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 "); 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; }
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