Write a recursive function that takes as a parameter a nonnegative integer and g
ID: 3684323 • 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 <stdio.h>
void printRec (int);
int main () {
int rows;
printf("Enter number of rows: ");
scanf("%d", &rows);
printRec(rows);
return 0;
}
void printRec(int r) {
int c, space;
if (r <= 0)
return;
for (c = r; c > 0; c--)
printf("* ");
printf(" ");
printRec(--r);
for (c = 0; c <= r; c++)
printf("* ");
printf(" ");
}
2)-----------------------------------------------------------
#include <stdio.h>
void printRec (int);
int main () {
int rows;
printf("Enter number of rows: ");
scanf("%d", &rows);
printRec(rows);
return 0;
}
void printRec (int r) {
int c, space;
static int stars = -1;
if (r <= 0)
return;
space = r - 1;
stars += 1;
for (c = 0; c < space; c++)
printf(" ");
for (c = 0; c <= stars; c++)
printf("* ");
printf(" ");
printRec(--r);
space = r + 1;
stars -= 1;
for (c = 0; c < space; c++)
printf(" ");
for (c = 0; c <= stars; c++)
printf("* ");
printf(" ");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.