Lab 14 Practicing Recursive Functions Learning Objectives Th

Lab 14: Practicing Recursive Functions

Learning Objectives: The objective of this lab is to gain experience with writing recursive functions.

Assignment: Write the following recursive functions (no credit for iterative implementations) and Write a driver to test the functions
a.   Write a recursive function writeLine() that writes a character repeatedly to form a line of n characters. For example, writeLine(\'*\',5) should produce the line *****.
b.   Write a recursive function writeBlock() that uses the method writeLine() to write m lines of n characters each. For example, writeBlock(\'*\',5,3) should produce the following output:
*****
*****
*****

c.   Write a recursive function writeBackward() that write a given string of characters in reverse order. For example, writeBackward(“abcde”, 5) should give the output: edcba

d.   Write a recursive function void displayVector(const vector<T> & A, int first, int last) that receives a vector A, first index, last index and displays the vector in order

e.   Write a recursive function int vowels(string s, int size) that receives a string and returns the number of vowels in the string

f.   Write a recursive function bool Palindrome (string s) that receives a string and returns true if the string is palindrome, and false otherwise. Hint: use string size( ) and erase( ) functions.

g.   Write a recursive function void ReverseString (string s) that receives a string and print the string backwards. Hint: use string size( ) and erase( ) functions.

Solution

#include <iostream>
using namespace std;

void writeLine(char, int);
void writeBlock(char , int , int);

int main()
{
   char answer;
   char xchar;
   int numInt;

   do
   {
//testing writeLine()
   cout << \"Enter a character: \" << endl;
   cin >> xchar;
cout << \"Enter a non-negative number: \" << endl;
   cin >> numInt;
   writeLine(xchar, numInt);
   cout << endl;

   //testing writeBlock()
   int lnum;
   cout << \"Enter number of lines\";
   cin >> lnum;
   writeBlock(xchar, numInt, lnum);
   cout << \"again? \";
   cin >> answer;
   }
   while (answer == \'Y\' || answer == \'y\');
}

void writeLine(char x, int num)
{
  
   if (num >= 1) //anchor case
   {
       cout << x;
       writeLine(x, num - 1); //inductive
   }  
}

void writeBlock(char x, int num, int lnum)
{
   if (lnum >= 1) //anchor case
   {
   writeLine(x, num);
cout << \"\ \";
writeBlock(x, num, lnum - 1); //inductive
   }
}

Lab 14: Practicing Recursive Functions Learning Objectives: The objective of this lab is to gain experience with writing recursive functions. Assignment: Write
Lab 14: Practicing Recursive Functions Learning Objectives: The objective of this lab is to gain experience with writing recursive functions. Assignment: Write

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site