Write a recursive function that takes as a parameter a nonnegative integer and g
ID: 3682031 • 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. Write a recursive function to generate the following pattern of stars: 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.Explanation / Answer
1)
#include <iostream>
using namespace std;
int str(int);
int main()
{
int n=4;
str(n);
return 0;
}
int str(int n)
{ int x;
if(n==0)
return -1;
for(int i=0;i<n;i++)
cout<<"*";
cout<<endl;
x= str(n-1);
for(i=0;i<n;i++)
cout<<"*";
cout<<endl;
return 0;
2)
2.
C program to print diamond using recursion
#include <stdio.h>
void print (int);
int main () {
int rows;
scanf("%d", &rows);
print(rows);
return 0;
}
void print (int r) {
int c, space;
static int stars = -1;
if (r <= 0)
return;
space = r - 1;
stars += 2;
for (c = 0; c < space; c++)
printf(" ");
for (c = 0; c < stars; c++)
printf("*");
printf(" ");
print(--r);
space = r + 1;
stars -= 2;
for (c = 0; c < space; c++)
printf(" ");
for (c = 0; c < stars; c++)
printf("*");
printf(" ");
}
}
#include <iostream>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.