Answer to Question #300361 in C++ for Adi

Question #300361

Write a generic C++ recursive function PrintPattern2 to print diamond pattern using recursion. No loops allowed whatsoever, and you can write other helping (recursive) functions. For example, calling your function with these argument PrintPattern2(5,1,5) should print following pattern.

Your function prototype must be as follows:

void PrintPattern2(int, int ,int);


1
Expert's answer
2022-02-21T01:27:44-0500
#include <stdio.h>

void PrintPattern2(int a, int b, int c) {
    for (int i = 1; i < a; i++) {
        printf(" ");
    }
    for (int i = 0; i < 2 * b - 1; i++) {
        printf("*");
    }
    printf("\n");
    if (b < c) {
        PrintPattern2(a - 1, b + 1, c);
        for (int i = 1; i < a; i++) {
            printf(" ");
        }
        for (int i = 0; i < 2 * b - 1; i++) {
            printf("*");
        }
        printf("\n");
    }
}

int main() {
  PrintPattern2(5, 1, 5);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog