Write a C or C++ program incorporating recursion to solve the following problem.
ID: 645140 • Letter: W
Question
Write a C or C++ program incorporating recursion to solve the following problem.
Accept a sequence of k characters from the user and use the characters to produce a k concentric squares. The outermost square is made up of the letter a(k), the square inside that is made from the letter a(k-1), the square inside that is made from the letter a(k-2), and so on, with the innermost square made from the letter a(1). For example, if k=5, and the values of a are ("R","O","C","K","Y") then the executed program should display:
Y Y Y Y Y Y Y Y Y
YK K K K K K K Y
Y K C C C C C K Y
Y K C O O O C K Y
Y K C O R O C K Y
Y K C O O O C K Y
Y K C C C C C K Y
Y K K K K K K K Y
Y Y Y Y Y Y Y Y Y
For this question I wrote a program in C++ but I cann't print the squares. Can someone help me with fixing my code? My code:
#include
#include
using namespace std;
string Pattern(int k, string letters[], string printlines[], int number);
string FilltheArray(int k, string letters[], string printlines[], int number);
int main(void)
{
int i, k;
string letters [10];
string printlines[19];
cout<<"How many charcaters would you like to enter"<<endl;
cin>>k;
letters[k];
printlines[k * 2 - 1];
cout<<"Enter the words!"< for(i=0; i < k; i++)
{
cout<<"Enter a character: ";
cin>>letters[i];
cout< }
printlines[k * 2 - 1] = Pattern(k, letters, printlines, 1);
k = 2*k-1;
cout< cout<<"The End!!"<
return 0;
}
string Pattern(int k, string letters[], string printlines[], int number)
{
if(k > 0){
FilltheArray(k ,letters, printlines, number);
Pattern(k - 1, letters, printlines, number + 1);
}
return printlines[k];
}
string FilltheArray(int k, string letters[], string printlines[], int number)
{
int q;
for(q = number; q <= number + 2 * (k-1); q++)
{
printlines[q] = letters[k];
}
return printlines[q];
}
Explanation / Answer
string Pattern(int k, string letters[], string printlines[], int number);
string FilltheArray(int k, string letters[], string printlines[], int number);
These two statements are to be declared in side the main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.