Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++: So I was given these homework questions to answer and I would like you to check them to make sure they are correct and

C++: So I was given these homework questions to answer and I would like you to check them to make sure they are correct and help me out on D). Thank you in advance!!

A) What is direct recursion?

Direct recursion: the recursive function calls itself explicitly. Direct recursion is the concept of calling a function to itself from the same function

---------------------------------------------------------

B) Consider the following recursive function:

void funcRec(int u, char v) //Line 1 { if (u == 0) //Line 2 cout << v; //Line 3 else //Line 4 { //Line 5 char w; //Line 6 w = static_cast //Line 7 (static_cast(v) + 1); funcRec(u - 1, w); //Line 8 } //Line 9 } //Line 10

Now answer the following questions:

a) Identify the base case.

if ( u == 0)

cout << v;

b) Identify the general case.

funRec(u,v)=funcRec(u-1, w);

where, w= v +1

c) What is the output of the following statement? funcRec(5, 'A'); .

F

--------------------------------------------------------

C) 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 any, of the following statements?

a) recFun(8);

continues infinitely since u+1 is never 0.

Thus, the output is

"Negative Negative Negative ..." until the stack overflows.

b) recFun(0);

immediately returns, printing "Zero! "

c) recFun(-2);

prints "Negative ", calls recFun(-1); prints "Negative ", callsrecFun(0), then prints "Zero! " and terminates.

"Negative Negative Zero! "

-------------------------------------------------

D) Consider the following function:

int test(int x, int y) {

if (x <= y) return y - x; else return test(x - 1, y + 1);

}

What is the output of the following statements?

a) cout << test(3, 100) << endl;

b) cout << test(15, 7) << endl;

---------------------------------------------

E) Suppose that intArray is an array of integers, and length specifies the number of elements in intArray. Also, suppose that low and high are two integers such that 0 <= low < length, 0 <= high < length, and low < high. That is, low and high are two indices in intArray.

a) Write a recursive definition that reverses the elements in intArray between low and high.

void func(int intArray[],int low,int high,int length)

{

int temp;

if(low ==length || high <0)return;

temp=intArray[low];

intArray[low]=intArray[high];

intArray[high]=temp;

func(intArray,low+1,high-1,length);

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning ASP.NET 4.5 Databases

Authors: Sandeep Chanda, Damien Foggon

3rd Edition

1430243805, 978-1430243809

More Books

Students also viewed these Databases questions

Question

What is focal length? Explain with a diagram and give an example.

Answered: 1 week ago

Question

What is physics and how does it apply in daily life?

Answered: 1 week ago