Help solve this recursive function problem in c Write a recu

Help solve this recursive function problem in c++.

Write a recursive function that generates this pattern XXXX XXX XX X XX XXX XXXX The length of the longest line in the pattern and the pattern\'s character are two of the recursive function\'s parameters.

Solution

#include <iostream>

using namespace std;

void printPatterns(int num,char pattern);//method declaration
//main program
int main()
{
int length;
   char pattern;
cout << \"Enter the max length of the pattern : \";
cin >> length;
   cout << \"Enter the pattern character : \";
cin >> pattern;
printPatterns(length,pattern);//calling the function to print the pattern   
cout << endl << \"Pattern complete\";
return 0;
} // end of main

//method to print pattern
void printPatterns(int num,char pattern)
{
if (num < 0)
   cout << endl << \"Please enter a number greate than 0\" << endl;

else{

if (num == 0) return;   

else{
if(num!=1){
for (int q = 1; q <= num; q++)
{
cout << pattern;//pinting the pattern
}
cout << endl;
}

printPatterns(num - 1,pattern); // calling the function recursively   

for (int q = 1; q <= num; q++) // printing the second part
{
           cout << pattern;
           }

cout << endl;
}
}
}

Help solve this recursive function problem in c++. Write a recursive function that generates this pattern XXXX XXX XX X XX XXX XXXX The length of the longest li

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site