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 the input exceeds 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 out a stack trace. Find the exact line that caused the segmentation fault.
Q13. What line caused the segmentation fault?
Q14. How do you fix the line so it works properly?
You can recompile the code and run the program again. The program now reads all the input values but the average calculated is still incorrect. Use gdb to fix the 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 read_values function. Then continue executing to the end of the function and view the values being returned. (To run until the end of the current function, use the finish command).
Q15. What is the bug? How do you fix it?
//average.c
#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):\n");
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\n",sum/values);
return 0;
}

Answers

Answer 1

Answer:

See Explanation

Explanation:

Q13. Line that caused the segmentation fault?

The segmentation fault was caused by line 15 i.e. scanf("%d",input);

Q14. How the line was fixed?

The reason for the segmentation fault is that the instruction to get input from the user into the integer variable "input" was not done correctly.

The correction to this is to modify scanf("d",input) to scanf("%d",input);

Q15. The bug?

The bug is that the method needs to return two value; the sum of the inputted numbers and the count of the inputted numbers.

However. it only returns the count of the inputted number.

So, the average is calculated as: 0/count, which will always be 0

How it was fixed?

First, change the method definition to: void and also include an array as one of its parameters.

void read_values(double sum, double arr []) {

Next:

assign sum to arr[0] and values to arr[1]

In the main method:

Declare an array variable: double arr [2];

Call the read_values function using: read_values(sum,arr);

Get the sum and values using:

sum = arr[0];

values = arr[1];

Lastly, calculate and print average:

printf("Average: %g\n",sum/values);

See attachment for complete modified program


Related Questions

Write VHDL code for the circuit corresponding to an 8-bit Carry Lookahead Adder (CLA) using structural VHDL (port maps). (To be completed before your lab session.)

Answers

Answer:

perdo si la pusiera es español te ayudo pero no esta en español

whats this oh _____
what is it
A) jane
B) juan.
C)juan
D) horse

Answers

Answer:

juan duhhhhh hehehe

Explanation:

Write a class called Dragon. A Dragon should have a name, a level, and a boolean variable, canBreatheFire, indicating whether or not the dragon can breathe fire. The class should have getter methods for all of these variables - getName, getLevel, and isFireBreather, respectively.

Answers

Answer: A dragon name could be name Holls and at level 14 and can breathe fire

Explanation:

Other Questions
4x+x=25 what will x equal?? what's a word problem for 10x=2 Manuel buys 2 shirts at $24 each as well as one pair of pants for $35. He has a coupon for 30% off the shirts. What is the total price he will pay if there is 6% tax? $58.10$61.59$72.72$87.98 what is 14/20 as a percentage ? The answers plzzzzzzz Hockey teams are often confronted with the dilemma of whether or not to pull a goalie at the end of the game when they are down a goal or two. One analyst reports that in 40% of those games in which a goalie was pulled a goal was scored with one empty net, and of those goals scored 25% were scored by the team that pulled the goaltender. Given that the goalie has been pulled by a team, what is the probability that team will score a goal? The seed capsules of the Himalayan balsam plant explode when touched and can eject seeds as far as 16 feet from the parent plant. Is this a structural adaptation, behavioral adaptation, or both? A designer lamp costs 475It is reduced by 30% in a saleHow much does the lamp now cost? Pls help Ill brainlest ASAP Will give brainliest to the most helpful answer!I just need 3-4 facts about:William TravisStephen AustinJoe (William Travis slave)Susanna Dickinson 9x + 9y = 9y = 7(, ) please I really need help 7. (07.03) An equation is shown below: 4(2x - 5) + 15 = 11 Write the steps you will use to solve the equation and explain each step. (5 points) please help me I'm confused Which term is used to describe the human body's reaction to stress? Please help! I'm stuck Members of congress are expected to:A. give voters information about important events.B. use bills of attainder to influence the government.C. decided whether new laws are unconstitutional. D. veto laws that are unpopular with the people they represent.Thank you! what are the standard units of displacement? Please help ASAP!!!! if you can, can you help me with this?? :) Thank you lots!! Identify one advancement or attitude that is portrayed in F-451 that Bradbury imagined. Why is the advancement you have selected important(why Bradbury chose to write about it) and what does it say about the future of society.