0% found this document useful (0 votes)
1 views

COMP PROG

The document contains a series of programming questions and answers related to recursive functions in C++. It includes explanations of function calls, outputs of specific code snippets, and the behavior of recursive algorithms. Key outputs include the results of functions like something(4), print(12), and sum(8), along with the workings of various recursive functions.

Uploaded by

Janina torre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

COMP PROG

The document contains a series of programming questions and answers related to recursive functions in C++. It includes explanations of function calls, outputs of specific code snippets, and the behavior of recursive algorithms. Key outputs include the results of functions like something(4), print(12), and sum(8), along with the workings of various recursive functions.

Uploaded by

Janina torre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

QUESTIONS ANSWER

Line 14 because of the “float” variable

What’s the output of the calling something(4);? execution of something(4):


1. Call: something(4)
int something(int number) o number = 4, so it executes 4 *
{ something(3).
if(number <= 0) 2. Call: something(3)
return 1; o number = 3, so it executes 3 *
else something(2).
return number 4 * something(number-1); 3. Call: something(2)
o number = 2, so it executes 2 *
} something(1).
4. Call: something(1)
o number = 1, so it executes 1 *
something(0).
return number 4 * something(3); 5. Call: something(0)
return number 3 * something(2) o number = 0, so it returns 1.
return number 2 * something(1); Returning Values:
return number 1 * something(0); • something(0) = 1
• something(1) = 1 * 1 = 1
1*0=0 • something(2) = 2 * 1 = 2
2*1=2 • something(3) = 3 * 2 = 6
3*2=6 • something(4) = 4 * 6 = 24
4*6=24 Final Output:
The output of something(4) is 24.

void print(int n) 1100


{
if (n == 0)
return;
cout<<n%2;
cout<<n/2;
}
What will be the output of print(12)?

What is the output of the program? Shortcut:

Consider the following recursive function fun(x, y). What is the value 4+3=7
of fun(4, 3) 7+3=10
int fun(int x, int y) 10+2=12
{ 12+1=13
if (x == 0)
return y;
return fun(x - 1, x + y);
}

Line 5

Line 14 and line 4

5+0
What will be the output of the following C++ code?
#include <iostream>
using namespace std;

int fun(int=0, int = 0);

int main()
{
cout << fun(5);
return 0;
}
int fun(int x, int y) {
return (x+y);
}

What is the output? Hi Jayar

#include <iostream>
using namespace std;

void greet()
{
cout<<"Hi Jayar";
}
int main()
{
greet();
return 0;
}
int sum(int n) { 1. sum(8):
if (n==0) Since n != 0, the function calls sum(7) and
return n; returns 8 + sum(7).
else 2. sum(7):
return n + sum(n-1); Since n != 0, the function calls sum(6) and
} returns 7 + sum(6).
What will be the output of sum(8). 3. sum(6):
Since n != 0, the function calls sum(5) and
returns 6 + sum(5).
4. sum(5):
Since n != 0, the function calls sum(4) and
returns 5 + sum(4).
5. sum(4):
Since n != 0, the function calls sum(3) and
returns 4 + sum(3).
6. sum(3):
Since n != 0, the function calls sum(2) and
returns 3 + sum(2).
7. sum(2):
Since n != 0, the function calls sum(1) and
returns 2 + sum(1).
8. sum(1):
Since n != 0, the function calls sum(0) and
returns 1 + sum(0).
9. sum(0):
The base case is reached, so the function
returns 0.
Now, unwinding the recursion:
• sum(1) returns 1 + 0 = 1
• sum(2) returns 2 + 1 = 3
• sum(3) returns 3 + 3 = 6
• sum(4) returns 4 + 6 = 10
• sum(5) returns 5 + 10 = 15
• sum(6) returns 6 + 15 = 21
• sum(7) returns 7 + 21 = 28
• sum(8) returns 8 + 28 = 36

What does the following function print for n = 25? 10011


void fun(int n)
{
if (n == 0)
return;
cout<<n%2;
fun(n/2);
}

What does the following recursive code do? Prints the numbers from 1 to 10
void my_recursive_function(int n)
{ my_recursive_function(10) calls
if(n == 0) my_recursive_function(9).
return; my_recursive_function(9) calls
my_recursive_function(n-1); my_recursive_function(8).
cout<<n; my_recursive_function(8) calls
} my_recursive_function(7).
int main() my_recursive_function(7) calls
{ my_recursive_function(6).
my_recursive_function(10); my_recursive_function(6) calls
return 0; my_recursive_function(5).
} my_recursive_function(5) calls
my_recursive_function(4).
my_recursive_function(4) calls
my_recursive_function(3).
my_recursive_function(3) calls
my_recursive_function(2).
my_recursive_function(2) calls
my_recursive_function(1).
my_recursive_function(1) calls
my_recursive_function(0).
my_recursive_function(0) reaches the base
case and returns without printing anything.

Prints 1 (from my_recursive_function(1)).


Prints 2 (from my_recursive_function(2)).
Prints 3 (from my_recursive_function(3)).
Prints 4 (from my_recursive_function(4)).
Prints 5 (from my_recursive_function(5)).
Prints 6 (from my_recursive_function(6)).
Prints 7 (from my_recursive_function(7)).
Prints 8 (from my_recursive_function(8)).
Prints 9 (from my_recursive_function(9)).
Prints 10 (from my_recursive_function(10)).
What’s the output of the following code? 22
int rec(int num){
return (num) ? num%10 + rec(num/10):0;
}

int main(){
cout<<rec(4567);
}

What will be the output of the following C++ code? 6


#include<iostream>
using namespace std;

int fun(int x = 0, int y = 0, int z=0)


{ return (x + y + z); }

int main()
{
cout << fun(1,2,3);
return 0;
}

What will be the output of the following code? 9


int cnt=0;
void my_recursive_function(int n)
{
if(n == 0)
return;
cnt++;
my_recursive_function(n/10);
}
int main()
{
my_recursive_function(123456789);
cout<<cnt;
return 0;
}

You might also like