Question: #include /* Read a set of values from the user. Store the sum in the sum variable and return the number of values read. */

 #include /* Read a set of values from the user. Store

#include

/* Read a set of values from the user. Store the sum in the sum variable and return the number of values read. */

int read_values(double sum) { int values=0,input=0; sum = 0; printf("Enter input values (enter 0 to finish): "); scanf("%d",&input); while(input != 0) { values++; sum += input; scanf("%d",input); } return values; }

int main() { double sum=0; int values; values = read_values(sum); printf("Average: %g ",sum/values); return 0; }

(Assignment 1, individual) Segmentation Faults Recall what causes segmentation fault and bus errors from lecture. Common cause is an invalid pointer or address that is being dereferenced by the C program. Use the program average.c from the assignment page for this exercise. The program is intended to find the average of all the numbers inputted by the user. Currently, it has a bus error if you input more than one number Load average.c into GDB with all the appropriate information and run it. GDB will trap on the segmentation fault and give you back the prompt. First find where the program execution ended by using backtrace (bt as shortcut) which will print outa stack trace. Find the exact line that caused the segmentation fault. 1. What line caused the segmentation fault? 2. How do you fix the line so it works properly? le the code and run the program again. The but the average You can recompi the input values program by looking at the output of read values. To do this, either set a breakpoint using the line number or set a breakpoint in the readvalues function. Then continue executing to the end of the function and view the values being program now reads all GDB to fix the calculated is still incorrect. Use Answer the following questions: 3. What is the bug here? 4. How do you fix it

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!