Answered step by step
Verified Expert Solution
Link Copied!

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems 6th Asian Conference Aciids 2014 Bangkok Thailand April 7 9 2014 Proceedings Part I 9 2014 Proceedings Part 1 Lnai 8397

Authors: Ngoc-Thanh Nguyen ,Boonwat Attachoo ,Bogdan Trawinski ,Kulwadee Somboonviwat

2014th Edition

3319054759, 978-3319054759

More Books

Students also viewed these Databases questions

Question

Briefly describe each of the costs associated with inventory.

Answered: 1 week ago