Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. The Fibonacci sequence is defined as a sequence of integers where each number

ID: 3801405 • Letter: 1

Question

1. The Fibonacci sequence is defined as a sequence of integers where each number is the sum of the two preceding ones. The following function returns the nth Fibonacci number. Show the computation steps for Fibonacci (4) (2 pts) int fibonacci (int n) if (n. 0) return 0 else if (n 1) return 1; else return fibonacci (n-1 fibonacci (n 2. What does the following function return for each input: (2 pts) nt puzzle (char val int a int b) if (a b) return 1; else if (val la val [b]) return puzzle (val, a b-1) -1 else return 0 a. val 'a', 'y, 'a', 'k') b-5 val m, a IT, m 3. The following function counts the occurrences of character x in an array. Fix the errors (3 pts) int coun toccurences (char data [J, char x, int idx) int count 0; if (data [idx] x) count 1; return countoccurences (data x, idx

Explanation / Answer

1. step 1: fibonacci(4)

step 2: if(4 == 0) condition is false.

step 3: else if ( 4 == 1) condition is false.

step 4: return fibonacci (3-1) + fibonacci(4-2) , so we are recursively calling the function fibonacci with 2 as an argument twice.

step 5: fibonacci(2)

step 6: if( 2 == 0) condition is false

step 7: if (2 == 1) condition is false

step 8: return fibonacci(1) + fibonacci(0)

step 9: fibonacci(1)

step 10: if (1 == 0) false

step 11: if ( 1 == 1) is true , so returns 1 ans in the same way fibonacci(0) returns 0

step 12: so we have retrun (1+0) + fibonacci (2) so step 5 to 11 are repeted and we get return (1+0)+(1+0),

i.e. 2

So the 4th fibonacci number is 2.

2. for the first input a, the function return 1 as , as long as a<b, the val[a] = val[b] and then there is a recursive call with val,a+1 and b-1 as the new arguments. this conitnues till a becomes greater than b, thus resulting in execution of the statement return 1

, and for the next input b, it returns 0 as for the first time a>b is false and val[a]=val[b]. but next recursive call results into val[a] != val[b], and the statement return 0 gets executed.

3. the errr is count = 1, as this is the assignment statement, the value of count will always be 1. The correct statement is count +=1; or cunt = count + 1;, which will result in incrementing the value of cout by 1 every time there is a match(if condition is true).

4. x is initialized to 17 in main, a call to cangeme(x) will result in x= 17+10 i.e. 27 ,so 27 is printed.

Next, a call changeme(x++) will take x= 17 as , the post increment operator will increment the value of x , once the function is called, so once again x=17 and will result in to x= 10+17 i.e. 27 and is printed.

now the value of x is incrented to 18, by the post increment operator x++.

Next , there is a call to changeme(++x) ,resulting in vakue of to be 19.

function changeme(19) will result into x = 19+10 i.e. 29 and is printed

So the output is as follows

27

27

29