Write a recursive function that generates this pattern XXXX

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

//code has been tested on gcc compiler

#include <iostream>

using namespace std;
void printPatterns(int longestline,char pattern)
{
if (longestline < 0) cout << endl << \"Please enter a positive number greater than 0\" << endl;

  
else{
if (longestline == 0) return; //base case

else{
for (int i = 1; i <= longestline; i++) //for printing upper part
{cout <<pattern ;}   
cout << endl;

printPatterns(longestline - 1,pattern); //calling function recursively
if(longestline!=1) //does not need to print 1 pattern again
{ for (int i = 1; i <= longestline; i++) //forprinting lower part
{cout << pattern;}

cout << endl;}
}
  
}
}
//end of method

int main()
{

printPatterns(4,\'X\'); //calling function with parameters,longestline=4 and pattern=X
return 0;
}

//end of code

********OUTPUT***********

XXXX                                                                                                                                                                            

XXX                                                                                                                                                                             

XX                                                                                                                                                                              

X                                                                                                                                                                               

XX                                                                                                                                                                              

XXX                                                                                                                                                                             

XXXX

********OUTPUT***********

Note:As you have not described any specific language i have done this in c++,please let me know in case of any doubt,thanks.

 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
 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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site