Question

In: Computer Science

The following questions are on recursion in C++ Programming. I had some trouble with these questions,...

The following questions are on recursion in C++ Programming.

I had some trouble with these questions, can you please help me? Thank you!

Consider the following recursive function

void funcEx8(int u, char v) //Line 1

{ //Line 2

if (u == 0) //Line 3

cout << u << " "; //Line 4

else //Line 5

{ //Line 6

int x = static_cast (v); //Line 7

if (v < 'A') //Line 8

v = static_cast (x + 1); //Line 9

else if (v > 'Z') //Line 10

v = static_cast (x - 1); //Line 11

cout << v << " "; //Line 12

funcEx8(u - 2, v); //Line 13 }

//Line 14 }

//Line 15 }

Identify the base case? Using line numbers

a.

Lines 3,4

b.

Lines 1 through 4

c.

Lines 7,8

d.

Lines 5,6

Consider the following recursive function

void funcEx8(int u, char v) //Line 1

{ //Line 2

if (u == 0) //Line 3

cout << u << " "; //Line 4

else //Line 5

{ //Line 6

int x = static_cast (v); //Line 7

if (v < 'A') //Line 8

v = static_cast (x + 1); //Line 9

else if (v > 'Z') //Line 10

v = static_cast (x - 1); //Line 11

cout << v << " "; //Line 12

funcEx8(u - 2, v); //Line 13 }

//Line 14 }

//Line 15 }

Identify the Recursive case? Using line numbers

a.

Lines 5 Through 15

b.

Lines 3,4

c.

1,2

d.

Lines 2,3

Consider the following recursive function

void funcEx8(int u, char v) //Line 1

{ //Line 2

if (u == 0) //Line 3

cout << u << " "; //Line 4

else //Line 5

{ //Line 6

int x = static_cast (v); //Line 7

if (v < 'A') //Line 8

v = static_cast (x + 1); //Line 9

else if (v > 'Z') //Line 10

v = static_cast (x - 1); //Line 11

cout << v << " "; //Line 12

funcEx8(u - 2, v); //Line 13 }

//Line 14 }

//Line 15 }

Valid or Invalid

funcEx8(26, '$'); is a valid call,

a.

None

b.

Yes and No

c.

No

d.

Yes

Consider the following recursive function:

void recFun(int u)

{

if (u == 0) cout << "Zero! ";

else

{

cout << "Negative ";

recFun(u + 1);

}

}

what is the output if recFun(8)

a.

Infinite loop, nonegative

b.

5

c.

0

d.

8

Consider the following recursive function:

void recFun(int u)

{

if (u == 0) cout << "Zero! ";

else

{

cout << "Negative ";

recFun(u + 1);

}

}

. what is the output if recFun(0)

a.

12

b.

infinite loop

c.

8

d.

zero

Consider the following recursive function:

void recFun(int u)

{

if (u == 0) cout << "Zero! ";

else

{

cout << "Negative ";

recFun(u + 1);

}

}

. what is the output if recFun(-2)

a.

8

b.

Negative Negative Zero!

c.

-2

d.

zero

Consider the following recursive function:

void recFun(int x)

{

if (x > 0)

{ cout << x % 10 << " ";

recFun(x / 10);

}

else

if (x != 0)

cout << x << endl;

}

what is the output of the above statement if recFun(258)?

a.

12

b.

3 4 6

c.

15

d.

8     5       2

Consider the following recursive function:

void recFun(int x)

{

if (x > 0)

{ cout << x % 10 << " ";

recFun(x / 10);

}

else

if (x != 0)

cout << x << endl;

}

. what is the output of the above statement if recFun(7)?

a.

7

b.

8

c.

8   5     2

d.

12

Consider the following recursive function:

void recFun(int x)

{

if (x > 0)

{ cout << x % 10 << " ";

recFun(x / 10);

}

else

if (x != 0)

cout << x << endl;

}

what is the output of the above statement if recFun(36)?

a.

5 3 8

b.

12

c.

6 3

d.

2 4 5

Consider the following recursive function:

void recFun(int x)

{

if (x > 0)

{ cout << x % 10 << " ";

recFun(x / 10);

}

else

if (x != 0)

cout << x << endl;

}

what is the output of the above statement if recFun(-85)?

a.

12

b.

-85

c.

8     5    2

d.

85

Solutions

Expert Solution

Q1)

Answer is Option A

In Recursive Function the best case is when the recursive part didn't run and the function ends . So In the given function if u is 0 then it ends just by printing u so it is the best case

Q2)

Answer is Option A

Recursive Part starts from line 5 when if condition is False and it comes in else part and then it starts the recursion under else part .

Q3)

Answer is Option D

Function needs two parameters i.e. one Int and other is Char So we pass 26 as Int and '$' as char . So Yes it is valid call

Q4)

Answer is Option A

As In recursion we are incrementing u by 1 each time so it will never become 0 and recursion only ends when u is 0 . So It goes in infinite loop

Q5)

Answer is Option D

The passes argument is 0 so if condition i.e. u==0 is true so we print the output Zero and function ends

Q6)

Answer is Option D

Now as u <0 So first else is true so negative will print and function is call by making -2 + 1= -1 So Again else is true and negative printed . Now u =0 hence we print zero

So Output is Negative Negative Zero

Q7)

Answer is Option D

Lets see how function will run

We have x = 258

First x% 10 = 8 is printed and x = 258/10 = 25

Second  x% 10 = 5 is printed and x = 25/10 = 2

At last x %10 = 2 and x= 2/10 = 0

Now Both If and else if is false  so function ends

Hence Output is 8 5 2

Q8)

Answer is Option A

Similarly as we do in last part Again x = 7

So x % 10 = 7 and x /10 = 0

Now Both If and else if is false  so function ends

Hence Output is 7

Q9)

Answer is Option C

Now x = 36

So x%10 = 6 and print 6 , x /10 = 36 /10= 3

x % 10 = 3 and print 3 , 3/10 =0

Now Both If and else if is false  so function ends

Hence Output is 6 3

Q10)

Answer is Option B

Now as X<0 So If condition didn't satisfy and we go to else and as x is not equal to 0 so it is True and we print x . So -85 is printed

These are the answer for each of the question

If u like the answer do Upvote it and have any doubt ask in comments

Thank You


Related Solutions

TCP client and server using C programming I am having trouble on how to read in...
TCP client and server using C programming I am having trouble on how to read in the IP adress and port number from the terminal Example: Enter IP address: 127.0.0.1 Enter Port Number: 8000 in both client and server code. How do can I make I can assign the Ip address and port number using the example above. the error I get is that the client couldn't connect with the server whenever i get the port number from the user...
In this programming assignment, you will write C code that performs recursion. For the purpose of...
In this programming assignment, you will write C code that performs recursion. For the purpose of this assignment, you will keep all functions in a single source file main.c. Your main job is to write a recursive function that generates and prints all possible password combinations using characters in an array. In your main() function you will first parse the command line arguments. You can assume that the arguments will always be provided in the correct format. Remember that the...
In this programming assignment, you will write C code that performs recursion. For the purpose of...
In this programming assignment, you will write C code that performs recursion. For the purpose of this assignment, you will keep all functions in a single source file main.c. Your main job is to write a recursive function that generates and prints all possible password combinations using characters in an array. In your main() function you will first parse the command line arguments. You can assume that the arguments will always be provided in the correct format. Remember that the...
In C++, Do some research to find the definitions of single recursion, binary recursion, and multiple...
In C++, Do some research to find the definitions of single recursion, binary recursion, and multiple recursion. What kinds of problems lend themselves to these different forms of recursion? Please be as detailed as possible. Thank you!
This is a question about C# programming: True / False questions: for (int i=5; i <...
This is a question about C# programming: True / False questions: for (int i=5; i < 55; i+=5) { decimal decPrcPerGal = decGalPrc + i; decPayPrc = iBuyGall * decPrcPerGal; if (decPrcPerGal > decMAX_GALPRC) { continue; } if (decPrcPerGal > decMAX_GALPRC) { break; } Console.WriteLine(" {1} gallon(s) at {0,3} would cost {2,8:$###,##0.00}", decPrcPerGal, iBuyGall, decPayPrc); } In the code above: 1. If decGalPrc is 10.57 decPrcPerGal will always be some number of dollars and 57 cents   True/ False? 2. In...
C programming Rewrite the following function using no loops, and only tail call recursion double question5...
C programming Rewrite the following function using no loops, and only tail call recursion double question5 (int in) { int i; int result; for (result = rand(), i = 0; i < in; i += 3) { result /= i; result += rand(); } return result; }
a. In your own words, define the concept of "Recursion" in terms of C++ programming. b....
a. In your own words, define the concept of "Recursion" in terms of C++ programming. b. In your own words, define the concept of "infinite recursion". c. In your own words, define "mutual recursion" as it pertains to C++ programming.
I have had some trouble understanding the IRR formula. Can you guy break it down.
I have had some trouble understanding the IRR formula. Can you guy break it down.
The following is for C programming language: I want to scan for initials in a line...
The following is for C programming language: I want to scan for initials in a line of text. my line of text is as follows: 12345 3.5000 a j 12346 4.1000 s p The first number represents the student ID, the second number represents the gpa, the third character represents the first initial and the fourth character represents the last initial of the student. My text file contains these values. The following is my code: fscanf(fp, "%d %c %c", &studentID,...
I am posting this problem because I've had trouble following the steps listed in similar problems....
I am posting this problem because I've had trouble following the steps listed in similar problems. Please don't answer if you are only going to copy/paste a version of those same steps; It will not be helpful. When a suspected drunk driver blows 186 mL of his breath through this breathalyzer, the breathalyzer produces an average of 323 mA of current for 10 s. Assuming a pressure of 1.0 atm and a temperature of 24 ∘C, what percent (by volume)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT