Answer to Question #300696 in C++ for adi

Question #300696

Write a C++ recursive function PrintPattern1 to print following pattern using recursion. No

loops allowed whatsoever, and you can write other helping (recursive) functions. For example, calling your function with these argument PrintPattern1(1,10) should print following pattern. Your function prototype must be as follows:

void PrintPattern1(int start, int end);

pattern:

*

*

*

*

*

*

*


1
Expert's answer
2022-02-22T01:10:48-0500
#include <iostream>


using namespace std;


void PrintPattern3(int start, int end) {
    for (int i = 1; i < 2 * end; i++) {
        if (i == start || i == 2 * end - start) {
            printf("*");
        } else {
            printf(" ");
        }
    }
    printf("\n");
    if (start < end) {
        PrintPattern3(start + 1, end);
        for (int i = 1; i < 2 * end; i++) {
            if (i == start || i == 2 * end - start) {
                printf("*");
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }
}


int main()
{
    PrintPattern3(1, 5);
    return 0;
}

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