Question

In: Computer Science

1. compile and try to run broken.c 2. use the gdb debugger to try to find...

1. compile and try to run broken.c

2. use the gdb debugger to try to find where the seg fault occurs

3. after a bit of detective work with the use of gdb, answer the questions in the file questions.txt

4. fix the error(s) in broken.c

****broken.c*****

/* This program creates an array containing 10 integers
and then at the bottom checks to see if the number 11
is in the array.

Note: the program contains a bug that only shows up when
it is run.
*/

#include <stdio.h>
#include <stdlib.h>

int main( void ) {
int i = 0, count = 0, search = 1, found = 0;
int number = 5;
int table[10];

table[0] = number;
printf( "table[%i]: %i\n", count, table[count] );

count = 1;
while( count < 10 ) {
table[count] = number++ * 2;
printf( "table[%i]: %i\n", count, table[count] );
count++;
}

while( search = 1 ) {
if( table[i++] == 11 ) {
search = 0;
found = i - 1;
}
if( count == i ) {
search = 0;
}
}

if( found )
printf( "The number 11 is in the table at location: %d\n", found );
else
printf( "The number 11 is not in the table.\n" );

return 0;
}

questions.txt regarding broken.c

--------
broken.c
--------
2) What line in broken.c did the program seg fault on?


3) What was the value of the variable "count" at the time of the seg fault?


4) What was the value of the variable "i" at the time of the seg fault?


5) What was the value of the variable "search" at the time of the seg fault?


6) What are the values in the "table" array?


7) what do you think the bug is?

  

Solutions

Expert Solution

1. When the program broken.c is run the following output is obtained:

A runtime error named segmentation fault is recorded since memory mapping to a location seemed empty.In this case the array table had table[9] as the max but the code tried to access table[10].

2)The error occurs on this line :

if( table[i++] == 11 ) where the value of i increments to 10 and the system tries to access table[10] which caused the segmentation fault.

3)The answers to questions.txt is yet below

4) The code with errors fixed :

#include <stdio.h>
#include <stdlib.h>

int main( void ) {
int i = 0, count = 0, search = 1, found = 0;
int number = 5;
int table[10];

table[0] = number;
printf( "table[%i]: %i\n", count, table[count] );

count = 1;
while( count < 10 ) {
table[count] = number++ * 2;
printf( "table[%i]: %i\n", count, table[count] );
count++;
}

while( search ==1 ) {         //the comparative operator is == . The = operator is assignment operator, which will always return
                                                //1(true) and thus the loop would be infinite.
  
if( table[i++] == 11 ) {
search = 0;
found = i - 1;
}

if( count == i ) {
search = 0;                                   
break;                                          //works without break
}  

}

if( found )
printf( "The number 11 is in the table at location: %d\n", found );
else
printf( "The number 11 is not in the table.\n" );

return 0;
}

output to this code:

Questions.txt:

2) On the line if( table[i++] == 11 ) as explained before.

3) count was 10 at the moment.

4) i was also 10 at the moment.

5)search was 1 at the moment

6) The values for array table are:

5 10 12 14 16 18 20 22 24 26    (total 10 data)

7)The bug was in the while loop.The comparing should have been like while(search==0) instead of while(search=0). Since the latter would result in infinite looping, looks like while(TRUE).

If you have any doubts pending mention them in the comments. If you have are satisfied with the answer please leave in a thumbs up, it really matters.

Thank You.

table[0]: 5 table[1]: 10 table [2] : 12 table [3] : 14 table[4]: 16 table[5] : 18 table[6] : 20 table [7] : 22 table[8]: 24 table[9] : 26 Segmentation fault (core dumped) ... Program finished with exit code 139 Press ENTER to exit console.]

table[0]: 5 table[1]: 10 table[2]: 12 table[3] : 14 table[4]: 16 table[5] : 18 table[6] : 20 table [7] : 22 table[8]: 24 table[9] : 26 The number 11 is not in the table. ... Program finished with exit code 0 Press ENTER to exit console.

table[0]: 5 table[1]: 10 table [2] : 12 table [3] : 14 table[4]: 16 table[5] : 18 table[6] : 20 table [7] : 22 table[8]: 24 table[9] : 26 Segmentation fault (core dumped) ... Program finished with exit code 139 Press ENTER to exit console.]

table[0]: 5 table[1]: 10 table[2]: 12 table[3] : 14 table[4]: 16 table[5] : 18 table[6] : 20 table [7] : 22 table[8]: 24 table[9] : 26 The number 11 is not in the table. ... Program finished with exit code 0 Press ENTER to exit console.


Related Solutions

in C++ Compile and run the program for the following values: r = 2 Cm, h...
in C++ Compile and run the program for the following values: r = 2 Cm, h = 10 Cm. The answer should be: The cross section area of the cylinder is 3.8955634 c The side area of the cylinder is 19.474819 inch-sqr Did you get the same answer? Explain the reason for such an error and fix the problem. Modify the previous program to include a new function called total_area, that computes the total suface area of a cylinder. The...
Use the TestCorrectness.java/TestCorrectness.cpp to compile, fix, and run your code. QueueUsingStack.java is also provided, but you...
Use the TestCorrectness.java/TestCorrectness.cpp to compile, fix, and run your code. QueueUsingStack.java is also provided, but you will need to complete some codes. public class TestCorrectness {    public static void main(String[] args) throws Exception {        int queueSize = 7;        QueueUsingStack qViaStack = new QueueUsingStack(queueSize);        Queue queue = new Queue(queueSize);        System.out.println("**** Enqueue Test ****");        System.out.println();        for (int i = 1; i <= 4; i++) {            int...
Compile and run the following code then answer the following questions: a.) Run this command from...
Compile and run the following code then answer the following questions: a.) Run this command from the shell prompt: ./a.out ls -F Explain, in your own words, why you see the screen output you do and how that output relates to both the content of the program AND the nature of the shell command used to invoke it. Be sure to comment on the pointer arithmetic done inside the line: execvp(*(argv+1), argv+1); b.) Run this command from the shell prompt:...
Task #1 void Methods Copy the file geometry.cpp. This program will compile, but when you run...
Task #1 void Methods Copy the file geometry.cpp. This program will compile, but when you run it , it doesn’t appear to do anything except wait. That is because it is waiting for user input, but the user doesn’t have the menu to choose from yet. We will need to create this. Above the main, write the prototype for a function called PrintMenu that has no parameter list and does not return a value.    Below the main, write the function...
UNIX/LINUX LAB (1) Review the sample shell program (tryShell.c), and compile/run the program (tryShell) with the...
UNIX/LINUX LAB (1) Review the sample shell program (tryShell.c), and compile/run the program (tryShell) with the following commands, and at the end, use CTRL+C to terminate the program: ls ls -l tryShell* date whoami hostname uname -a ctrl+C    (2) Run the program (tryShell) with "time -p" with a few commands: time -p ./tryShell (3) Edit the program (tryShell.c) so that it will exit (terminate the program) when the input command string is "exit" try shell.c code at bottom //////////// #include...
Group 7 Section 2 Group Project 2 1. Let’s try to find out how inflation affect...
Group 7 Section 2 Group Project 2 1. Let’s try to find out how inflation affect nominal interest rates. To do so: a. Plot the three-month U.S. Treasury bill rate (FRED code: TB3MS) from 1960 to the present. What long-run pattern do you observe? What may have caused this pattern? b. Plot the inflation rate based on the percent change from a year ago of the U.S. consumer price index (FRED code: CPIAUCSL) from 1960 to the present. How does...
UNIX/LINUX LAB   (1) Compile hello.c program: gcc hello.c -o hello (2) Compile hello.c program with -static...
UNIX/LINUX LAB   (1) Compile hello.c program: gcc hello.c -o hello (2) Compile hello.c program with -static option: gcc -static hello.c -o hello1 (3) Use size command to see the memory layout of these two programs (hello and hello1). (4) Write a brief summary of what you have noted here. Submit the session history (log) of this activity and a brief summary. hello.c program bottom //////////// #include <stdio.h> #include <stdlib.h> int main() { printf("Hello World\n"); // waw hello Richard and ok...
*IN JAVA* EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define...
*IN JAVA* EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define an exception class and call it from the driver program. For this lab you will complete two parts. Part (a) of the lab implements try… catch and uses an existing exception in C# or in Java. Write a program that implements an ArrayIndexOutOfBounds error. Part (b) writes a program that converts a time from 24-hour notation to 12-hour notation. Assume the user will enter...
(in C# please.) EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2....
(in C# please.) EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define an exception class and call it from the driver program. For this lab you will complete two parts. Part (a) of the lab implements try… catch and uses an existing exception in C# or in Java. Write a program that implements an ArrayIndexOutOfBounds error. Part (b) writes a program that converts a time from 24-hour notation to 12-hour notation. Assume the user will...
6. Write a program in C programming (compile and run), a pseudocode, and draw a flowchart...
6. Write a program in C programming (compile and run), a pseudocode, and draw a flowchart for each of the following problems: a) Obtain three numbers from the keyboard, compute their product and display the result. b) Obtain two numbers from the keyboard, and determine and display which (if either) is the smaller of the two numbers. c) Obtain a series of positive numbers from the keyboard, and determine and display their average (with 4 decimal points). Assume that the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT