Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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(" ");
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote