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...
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; }
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 need C++ programming with output. I have tried other programming and it does not work....
I need C++ programming with output. I have tried other programming and it does not work. So please give me the one that actually works. Assignment 1 Design your own linked list class that works as a template class. It should provide member functions for appending, inserting and deleting nodes. The destructor should destroy the list. The class should also provide a member function that will display the contents of the list to the screen. The class should also provide...
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude...
Write a program that performs a merge-sort algorithm without using a recursion. c++ programming language(Only #inlclude <iostream>)
I am having trouble with a C++ code that I'm working on. It is a spell...
I am having trouble with a C++ code that I'm working on. It is a spell checker program. It needs to compare two arrays, a dictionary, and an array with misspelled strings that are compared to the strings in the dictionary. the strings that are in the second array that is not in the Dictionary are assumed to be misspelled. All of the strings in the dictionary are lowercase without any extra characters so the strings that are passed into...
IN C# WITH SCREENSHOTS OF THE CODE RECURSION Objectives • Learn the basics of recursion. Background...
IN C# WITH SCREENSHOTS OF THE CODE RECURSION Objectives • Learn the basics of recursion. Background There are many problems that loops simplify, such as displaying every pixel to a screen or receiving repetitive input. However, some situations that can be simplified with looping are not easily solvable using loops. This includes problems that require back tracking and being able to use information from previous iterations, which would normally be lost when using an iterative loop. In those cases, it...
Using dev c++ I'm having trouble with classes. I think the part that I am not...
Using dev c++ I'm having trouble with classes. I think the part that I am not understanding is sending data between files and also using bool data. I've been working on this program for a long time with many errors but now I've thrown in my hat to ask for outside help. Here is the homework that has given me so many issues: The [REDACTED] Phone Store needs a program to compute phone charges for some phones sold in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT