Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this lab, you are asked to write recursive functions for functions that has been written in iterative form. You do not need to alter

In this lab, you are asked to write recursive functions for functions that has been written in iterative form. You do not need to alter the main() function. Your main task is to write the recursive functions for the tasks below

1. (1 point) Problem 1: Print out all odd positive number that are less than the user input. For example, if the user input is 5, your program should print out 1 3

2. (1 point) Problem 2: Count the number of upper-case letters in a word. For example, if user input is ''heLLo'', your program answer that there are 2 upper-case letters

3. (1 point extra credit) Extra-credit Problem: Display a string backward. For example, if user input is "heLLo", your program should display "oLLeh"

SAMPLE CODE;

#include #include #include using namespace std;

//------------------------- Sample ----------------------------- // Iteratively (non-recursively) display numbers up to some limit void sampleIterative( int counter, int limit) { // Display numbers up to the limit while( counter < limit) { cout << counter << " "; counter++; } cout << endl; // display a new line }//end sampleIterative()

// Same thing, now using recursion void sampleRecursive( int counter, int limit) { // Check for the base condition if( counter == limit) { cout << endl; } else { cout << counter << " "; // make the recursive call sampleRecursive( ++counter, limit); } }//end sampleRecursive()

//------------------------- Problem 1 -------------------------- // Display odd numbers less than some limit void problemOneOddNumbersIterative( int counter, int limit) { // Display numbers up to the limit while( counter < limit) { if( counter % 2 == 1) { cout << counter << " "; } counter++; } cout << endl; // display a new line }//end problemOneOddNumbersIterative()

// Same thing, now done recursively. >>> YOU SUPPLY CODE IN THIS FUNCTION <<< void problemOneOddNumbersRecursive( int counter, int limit) { //*** WRITE YOUR CODE HERE for problem 1 ... }//end problemOneOddNumbersRecursive()

//------------------------- Problem 2 -------------------------- // Count the number of upper-case letters in an array int problemTwoCountUpperIterative( int index, char arrayOfLetters[]) { int counter = 0; while( index < strlen( arrayOfLetters)) { char currentChar = arrayOfLetters[ index]; // first character // If the current character is upper case, increment counter if( isupper( currentChar)) { counter++; } // advance pointer to next character index++; } return counter; }//end problemTwoCountUpperIterative()

// Same thing, now done recursively. int problemTwoCountUpperRecursive( int index, char arrayOfLetters[]) { //*** WRITE YOUR CODE HERE for problem 2 ... return 0; // you may need to change this line }//end problemTwoCountUpperRecursive()

//------------------------- Problem 3 -------------------------- // Display a string of characters in reverse order void problemThreeDisplayReversedIterative(char arrayOfLetters[81] ) { // start at end of string and display characters for( long i=strlen( arrayOfLetters); i>0; i--) { cout << arrayOfLetters[ i-1]; } cout << endl; }//end problemThreeReverseIterative()

// Same thing, now done recursively. void problemThreeDisplayReversedRecursive(int index, char charArray[81]) { //*** WRITE YOUR CODE HERE for problem 3 ... }//end problemThreeReverseRecursive()

//---------------------------------------------------------- //YOU SHOULD NOT EDIT CODE IN THE MAIN() FUNCTION //---------------------------------------------------------- int main() { cout<<" Enter a choice:"; cout<<" 0. Problem 0: Display counts"; cout<<" 1. Problem 1: Odd Numbers"; cout<<" 2. Problem 2: Count Upper Case"; cout<<" 3. Problem 3: Reverse String"; cout<<" 4. Exit "; int choice; cin>> choice; switch(choice){ case 0: { //------------------------------------------------------ // Problem 0: This one is an already completed example. // Count to some limit using a while loop. // Then the recursive version does the same thing. // For the other functions below you will be given the iterative (non-recursive) // version, and will have to create the recursive version. int counter = 0; int limit; cout << "Sample Problem "; cout << "Enter the limit: "; cin >> limit; sampleIterative( counter, limit); // Iterative (non-recursive) version sampleRecursive( counter, limit); // Recursive version break; } case 1: { //------------------------------------------------------ // Problem 1: Display the odd numbers less than some limit int counter = 0; int limit; cout << "Problem 1 "; cout << "Enter the limit: "; cin >> limit; problemOneOddNumbersIterative( counter, limit); problemOneOddNumbersRecursive( counter, limit); // <-- you supply the code for this one cout << " "; break; } case 2: { //------------------------------------------------------ // Problem 2: Given a C string, count how many characters are upper-case char arrayOfLetters[ 81]; cout << "Problem 2 "; cout << "Enter a line of input with mixed-case letters: "; int counter = 0; // First get rid of carriage-return lingering on input buffer from previous question char c; cin.get( c); // Now read in user input cin.getline( arrayOfLetters, 81); // read in an entire line cout << "Number of upper-case done Iteratively is: " << problemTwoCountUpperIterative( 0, arrayOfLetters) << endl; cout << "Number of upper-case done Recursively is: " << problemTwoCountUpperRecursive( counter, arrayOfLetters) // <-- you supply the code for this one << endl; cout << endl; break; } case 3: { //------------------------------------------------------ // Problem 3: Enter letters to be displayed in reverse order char arrayOfLetters[ 81]; cout << "Enter input to be reversed Iteratively: "; // read in user input into the letters cin.ignore(); cin.getline(arrayOfLetters, 81); // read in an entire line problemThreeDisplayReversedIterative(arrayOfLetters); cout << endl; problemThreeDisplayReversedRecursive(0, arrayOfLetters); cout << endl; break; } case 4: { cout<<"Exiting with code 0"; } } }//end main()

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

Murach's SQL Server 2012 For Developers

Authors: Bryan Syverson, Joel Murach, Mike Murach

1st Edition

1890774693, 9781890774691

More Books

Students also viewed these Databases questions

Question

What are Measures in OLAP Cubes?

Answered: 1 week ago

Question

How do OLAP Databases provide for Drilling Down into data?

Answered: 1 week ago

Question

How are OLAP Cubes different from Production Relational Databases?

Answered: 1 week ago