averagec include Read a set of values from the user Store

average.c

~~~~~~~~~~~~~~~~~

#include <stdio.h>

/*
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;
}

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?

Solution

Hi,

I have fixed the issues and highlighted the code changes below

#include <stdio.h>
/*
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); //segment fault error
}
return values;
}
int main() {
double sum=0;
int values;
values = read_values(sum);
printf(\"Average: %g\ \",sum/values);
return 0;
}

OUtput:

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                                              

sh-4.2$ main                                                                                                                                                                                                                                                           

Enter input values (enter 0 to finish):                                                                                                                                                                                                                                

4                                                                                                                                                                                                                                                                      

5                                                                                                                                                                                                                                                                      

6                                                                                                                                                                                                                                                                      

7                                                                                                                                                                                                                                                                      

3                                                                                                                                                                                                                                                                      

0                                                                                                                                                                                                                                                                      

Average: 5

average.c ~~~~~~~~~~~~~~~~~ #include <stdio.h> /* Read a set of values from the user. Store the sum in the sum variable and return the number of values re
average.c ~~~~~~~~~~~~~~~~~ #include <stdio.h> /* Read a set of values from the user. Store the sum in the sum variable and return the number of values re

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site