Lab #13: Recursion Solution

$30.00

Description

For this lab you are to work in teams of two with the following rules:

  • The two of you will sit together and use one computer.

  • Each person must be typing on the keyboard for approximately 50% of the time – the easiest way to do this is to have one person be the typist for the first part and then have the other person be the typist for the second part.

  • If you complete this lab on your own or not in the presence of a lab instructor there will be an automatic 20% deduction in your grade.

The task here is 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){ // loop to do three test runs in 1 run

cout<<”Enter a number: “;

cin>>x;

cout<<”The even numbers counting to there:\n”;

counting(x);

}

// main for Part Two will go here

return 0;

}

The function that you are to implement is:

int counting(int n);

ONCE YOU HAVE TESTED AND SAVED THIS PROGRAM CHANGE TYPIST AND TURN THE PAGE OVER.

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:

Here’s 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:\n”;

reversing(s,start,end);

cout<<s<<endl;

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

Submit to Blackboard, your source code, showing both of the recursive functions and the main that calls them, and the script file that shows a complete running of the program WITH CALLS TO BOTH OF THE FUNCTIONS.