baic C++ Assignment : Write the following recursive functions (no credit for ite
ID: 3761570 • Letter: B
Question
baic C++ 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.
Explanation / Answer
#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)
{
cout << x;
writeLine(x, num - 1);
}
}
void writeBlock(char x, int num, int lnum)
{
if (lnum >= 1)
{
writeLine(x, num);
cout << " ";
writeBlock(x, num, lnum - 1);
}
}
c)
#include <string.h>
#include <iostream>
using namespace std;
void recuverse(char s[]);
int main()
{
char s[] = "abcde";
recuverse(s);
cout << s << endl;
}
void recuverse(char *s)
{
size_t len = strlen(s);
if (len < 2) return;
char t = s[len-1];
s[len-1] = '';
recuverse(s+1);
s[len-1] = s[0];
s[0] = t;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.