Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Recursion Summer 2018 For this lab you are to write two recursive functions and a main that calls them. The two functions have nothing to

Recursion Summer 2018

For this lab you are to write two recursive functions and a main that calls them. The two functions have nothing to do with each other, so you should do the easy one first and test it, and then tackle the second one. The functions are to be recursive, meaning that there will be no while or for anywhere in the program except where I have shown it in the main below. (No credit given for a non-recursive solutions.)

Problem One: Write a recursive function, called counting, that receives a single parameter which will always be a positive number and then prints all the even numbers from 0 up to, and including the number that was passed in, if that number is even, or one less than the number passed in if it is odd. (Up to means that you start with the smallest number and end with the largest number.)

Run with the program with the values 23, 18, and -2. (No output should be generated by the last.)

The main for this program will look like this:

int main(){

int x;

for(int j=0; j<3; ++j){

cout<

cin>>x;

cout<

counting(x);

}

// main for Part Two will go here

return 0;

}

The function that you are to implement is:

void counting(int n);

SEE NEXT PAGE

Problem Two: In this one the recursive function takes a string and two arguments that are array indexes. The program then reverses all the characters within the array indexes, so given s= abcdef and the numbers 2 and 5 the printed string should be abfedc. Remember to pass the string by reference and that the base case needs to consider the possibility of the two indexes crossing each other:

Heres what the main will look like:

// main for Part One goes in here.

string s;

int start, end;

cout<<"Enter a string:";

getline(cin,s);

cout<<"Now enter two numbers that are within the bounds of the string.";

cin>>start>>end;

cout<<"This is how your words look now: ";

reversing(s,start,end);

cout<

return 0;

}

And a sample output:

Enter a string:go bobcats

Now enter two numbers that are within the bounds of the string.3 7

This is how your words look now:

go acbobts

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_2

Step: 3

blur-text-image_3

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

Database Marketing The New Profit Frontier

Authors: Ed Burnett

1st Edition

0964535629, 978-0964535626

More Books

Students also viewed these Databases questions

Question

Describe the six tools required to use HR analytics in a firm.

Answered: 1 week ago

Question

Select suitable tools to analyze service problems.

Answered: 1 week ago