Question
can anyone please help me to write a recursive ( must in recursive function) function in c++ to print the asterisk pattern in the screenshot below. thanks
as this:
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the recursive implementation of the code. Comments are added in the code itself. The logic of the code is a recursive function which is being executed in a bottom-up manner. The recursive function has two parts, one is the base case which handles the termination of the recursion properly and other is the normal recursion. Fin below the code, the screenshot of the code and the desired output. Try to dry run the code on a paper to understand the logic.

Code

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
void asterisk_recur(int n,int c)
{
   // Base case or Termination case
   if(n==1)
   {
       // appending the space characters
       for(int i=0;i<c;i++)
           cout<<" ";
       cout<<"*";
   }
   else
   {
       asterisk_recur(n/2,c);
       cout<<"\n";
       // appending the space characters
       for(int it=0;it<c;it++)
           cout<<" ";
        // print the asterisks
        for(int it=0;it<n;it++)
           cout<<"* ";
        cout<<"\n";
        asterisk_recur(n/2,c+n);
        }
}

// main function
int main()
{
   // calling the recursive function to generate pattern
   asterisk_recur(8,0);
    return 0;
}

Screenshot

1 #include <bits/stdc++.h> 2 #include iostream» 3 using namespace std; 4 void asterisk_recur(int n,int c) /Base case if(n-=1)

Output

media%2Fe2a%2Fe2ab4063-fb9e-45ce-ac3f-e8

Add a comment
Know the answer?
Add Answer to:
can anyone please help me to write a recursive ( must in recursive function) function in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT