Hello there, I am taking a Data Structures & Algorithms class in Java this semes
ID: 3732822 • Letter: H
Question
Hello there,
I am taking a Data Structures & Algorithms class in Java this semester and we are currently working on Recursion. I am struggling with understanding it and I don't really know how to approach the first part. If someone could help me with this, that would be great! I have also attached the assignment.
Thanks!
Assignment
1. Write a recursive method printstarPattern () that takes a nonnegative integer as a parameter and generates the following pattern of stars. If the nonnegative integer is 4, then the pattern generated is: k*a ka t* 2 Also, write a main method that prompts the user to enter the number of lines in the pattern and uses the recursive method to generate the pattern. For example, specifying the number of lines to be 4 generates the preceding patternExplanation / Answer
#include<iostream>
using namespace std;
void printPattern(int i, int limit)
{
if (i>limit)
return;
else
{
for (int j=i;j<=limit;j++)
cout<<"*";
cout<<endl;
printPattern(i+1,limit);
for(int j=i;j<=limit;j++)
cout << "*";
cout<<endl;
}
}
int main()
{
int num;
cout<<"Enter number : ";
cin>>num;
printPattern(1,num);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.