1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Marrrta [24]
3 years ago
6

Segmentation Faults Recall what causes segmentation fault and bus errors from lecture. Common cause is an invalid pointer or add

ress 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;
}
Computers and Technology
1 answer:
vfiekz [6]3 years ago
8 0

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.

<em>void read_values(double sum, double arr []) {</em>

Next:

<em>assign sum to arr[0] and values to arr[1]</em>

<em />

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:

<em>sum = arr[0];</em>

<em>values = arr[1];</em>

<em />

Lastly, calculate and print average:

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

See attachment for complete modified program

Download txt
You might be interested in
Which term is used to identify the connection of computers that are physically close to one another?
marta [7]

Answer: Local Area Network (LAN)

Explanation:

A Local Area Network (LAN) is a computer network that interconnects computers within a limited physical area such as a residence, school, laboratory, university campus or office building.

An example of  LAN network is "Phone, Computer and TV connected to a single network (such as a Home Network) via Cables, Wifi, Bluetooth or Hotspot". When the devices are interconnected or connected to a LAN, it becomes accessible between each other.

A simplest example of a LAN Device is a <em>Home Router.</em>

6 0
3 years ago
Read 2 more answers
WIN
Delicious77 [7]
The correct answer is D
6 0
3 years ago
Read 2 more answers
Which statement is true for slide or positive film?
Hitman42 [59]

Answer:

B.

Slides or positive films give high quality pictures with higher color saturation and contrast.

Explanation:

The above is true for slides with positive film. This is because, the positive film happens to have a high resolution when it was been used thereby giving out a high quality video at the end of the recording. The higher colour saturation and contrast are also an attribute of a slide  or positive film.

6 0
3 years ago
Which of the following would increase the Demand for jam?
gladu [14]
The answer would be B. An increase in the price of a complement.
8 0
3 years ago
What are some text effects found in the WordArt gallery? Check all that apply.
Softa [21]
The answer is bevel, glow, reflection, shadow, and soft edges
3 0
4 years ago
Read 2 more answers
Other questions:
  • Match the tool to its description.
    15·1 answer
  • Which of the following is LEAST needed when programming a computer?
    7·1 answer
  • There are several methods of updating information and data on a webserver. We must consider who performs those updates upfront w
    9·2 answers
  • A well-diversified portfolio needs about 20-25 stocks from different categories is this True or False?
    6·2 answers
  • ____ devices are high-performance storage systems that are connected individually to a network to provide storage for the comput
    5·1 answer
  • Cual es la definición de grouded?​
    7·1 answer
  • Select four tasks associated with operating system maintenance. Cleaning inside the computer Defragmenting the hard drive Deleti
    11·1 answer
  • Apply the Fill - Teal, Accent 4, Soft Bevel text effect (the 5th
    12·1 answer
  • ____ is an example of a set of prewritten classes that allows you to access data stored in a database.
    8·1 answer
  • How can edge computing be used to increase sustainability
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!