1 #include <iostream>
2 using namespace std;
3 int fib(int n)
4 {
5 if(n==1 || n==2) return 1;
6 int prev1 = 1, prev2 = 1;
7 for(int i = 3; i <= n; i++)
8 {
9 int fib = prev1 + prev2;
10 prev2 = prev1;
11 prev1 = fib;
12 }
13 return prev1;
14 }
15 int main()
16 {
17 int n = 1;
18 cin>>n;
19 cout<<"nth Fibonacci number is "<<fib(n)<<endl;
20 return 0;
21 }
Compile the above code with the -g flag
as shown below. The flag ensures that the compiled program has required debugging information to work with gdb
g++ fibonacci.cpp -g -o fib
Now try executing the above generated executable.
$ ./fib
4
nth Fibonacci number is 3
Now we try running the above executable with a debugging tool called gdb.
gdb fib
Breakpoints
int n = 1;
like this:b fibonacci.cpp:17
r
or run
. gdb will now start executing the program fib
.Printing
Try printing the current value of variable n as follows:
print n
or
p n
Is the printed value of n = 1 ?
NO, because the command
int n = 1
has not yet been executed.
To execute/move forward by a command, enter step
or s
.
Now try printing n again as described above. The value should change since the command at line 17 is executed.
To print the active breakpoints at any time, enter info break
.
We now delete the breakpoint added at line 17 using delete 1
or d 1
to delete the first breakpoint.
Add a new breakpoint at line 10 to observe the values of successive fibonacci numbers as they get evaluated.
Enter c
and press enter to continue the program execution, input a value for n ( >= 3 ) and press enter to run the code for nth fibonacci number.
You should break at line number 10. Try printing the value of variable fib
at this point. What do you think it should be? Does it match your expectation?
Backtrace
bt
and observe the output. This command outputs your location in the code in terms of functions that have been called. For instance, we are currently at line 10 inside the fib() function which in turn was called by the main() function.To observe the rest of the run, continue (c
) the code. Do you hit the breakpoint at line 10 again?
1 #include <iostream>
2 using namespace std;
3 int main()
4 {
5 int numbers [] = {3, 4, 9, 16};
6 for(int i = 0; i<4; i++)
7 {
8 cout<<numbers[i]<<" divided by i is "<<(numbers[i]/i)<<endl;
9 }
10 }
g++ faults.cpp -g -o faults
and open the executable with gdb gdb faults
.r
) once gdb is loaded.i
at this point.)q
or quit
.numbers[i]/i
with numbers[i]/(i+1)
in the division operation at line 8
in faults.cpp
.r
to run the program again, NOW:
Example
Original Queue : FRONT -> “alpha” | “beta” | “gamma” <- BACK
Required Queue : FRONT -> “alpha” | “beta” | “gamma” | “alpha” | “beta” | “gamma” <- BACK
queue<string> clone(queue<string> input_queue)
{
// Modify and return the input_queue after cloning
return input_queue;
}
(
and )
)
has a corresponding matching (
.Consider the following string:
(()())
Here, every )
has a corresponding matching (
. Hence this is balanced.
However, consider the following string:
()())
Here, the )
at the last position, does not have a matching (
. Hence this is not balanced.
)
with the LAST encountered unmatched (
as we read the string from left to right?(
?bool isBalanced ( string input_string )
{
// Return true if parentheses in input_string are balanced, false otherwise
}
n
people of your COL100 class, sitting in a circle, appearing for the minor exam. You, being in the class, are also a part of this group.n
and a number k
which indicates that k-1
persons are skipped and k-th
person’s paper is collected.Consider n = 5
and k = 2
. Here, the last person to submit is at position 3.
Initially, the circle can be represented in a linear fashion as follows:
.1 2 3 4 5
Here, the .
is put before the element from where we begin counting for k
.
The first person to submit is 2
. The state changes as follows:
1 .3 4 5
The next person to submit will be 4
. The state changes as follows:
1 3 .5
The next person to submit will be 1
. The state changes as follows:
.3 5
The next person to submit will be 5
. The state changes as follows:
.3
Since 3
is the last person to remain, he submits at the end. Hence, the solution is 3
.
int getLastPosition( int n, int k )
{
// Your code goes here
}