Write a recursive function that takes as a parameter a nonnegative integer and g
ID: 3667589 • Letter: W
Question
Write a recursive function that takes as a parameter a nonnegative integer and generates the following pattern of stars. If the nonnegative integer is 4, then the pattern generated is:
****
***
**
*
*
**
***
****
Also, write a program that prompts the user to enter the number of lines in the pattern and uses the recursive function to generate the pattern. For example, specifying 4 as the number of lines generates the above pattern.
Explain each step of the code and it was written that way.
Program must be written in c++
Explanation / Answer
#include<iostream>
using namespace std;
void line(int,int,int);
int main()
{
int i;
do
{
cout<<"how many rows?";
cin>>i;
if(i<0)
cout<<"must be nonnegative ";
}
while(i<0);
line(i,1,i);
system("pause");
return 0;
}
void line(int m,int n,int p)
{
int i;
if(m>p)
return;
for(i=0;i<m;i++)
cout<<"*";
if(m>0)
cout<<endl;
if(m==0 ==0)
line(m+1,0,p);
else
line(m-1,n,p);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.