Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this exercise, write two recursive functions that output character patterns

ID: 3691602 • Letter: F

Question

For this exercise, write two recursive functions that output character patterns using asterisks. These are programming exercises #1 and #2 on page 1067 of the textbook. The first function should be given an integer parameter to indicate the number of asterisks to output. If the parameter value is 4, the pattern would be as follows: The second function should also output a pattern. The parameter or parameters for this function is up to you, but should include the number of asterisks to output. So, if the number of asterisks is 4, the pattern should be as follows: Neither of these functions should use a loop; they should use recursion. Implement both functions in a single source. Also include a main program that prompts for a single integer, then outputs both patters based on the input value.

Explanation / Answer

#include <iostream>
#include <string>
using namespace std;
void printRec (int);
void print (int r);
int main()
{
int rows;
cout<<"Enter number of rows: ";
cin>>rows;
printRec(rows);
cout<<"=================================="<<endl;
print (rows);
return 0;
}
void printRec(int r) {
int c, space;
if (r <= 0)
return;
for (c = r; c > 0; c--)
cout<<"* ";

cout<<" ";

printRec(--r);

for (c = 0; c <= r; c++)
cout<<"* ";

cout<<" ";
  
}
void print (int r) {
int c, space;
static int stars = -1;

if (r <= 0)
return;

space = r - 1;
stars += 1;

for (c = 0; c < space; c++)   
cout<<" ";
for (c = 0; c <= stars; c++)
cout<<"* ";

cout<<" ";

print(--r);

space = r + 1;
stars -= 1;
for (c = 0; c < space; c++)
cout<<" ";

for (c = 0; c <= stars; c++)
cout<<"* ";

cout<<" ";
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote