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

Write an A5Q1 class containing a main method calling a recursive method to draw

ID: 3865537 • Letter: W

Question

Write an A5Q1 class containing a main method calling a recursive method to draw Sierpinski triangles (http: //en.wikipedia.org/wiki/Sierpinski_triangle). Sierpinski triangles are equilateral triangles that can be drawn recursively. You will use the StdDraw class to draw the triangles. Define two drawTriangles methods, one non-recursive, and one recursive method. The non-recursive version simply calls the recursive version. The recursive version will draw a "level n" Sierpinski triangle. A "level 1" triangle is just an ordinary equilateral triangle. A "level n" triangle is made up of three smaller "level n-1" Sierpinski triangles, as defined below.

Explanation / Answer

Answer for the given Question:

See the below code will draw the given sierpinski triangle shape with the help of recusrive call as well as non-recursce call.


#include<stdio.h>
void triangle(char display[100][100],int x, int y, int h) {

for (int row = y; row<=h-1+y; row++) {
for (int column = x; column<=2*(row+1-y)+x-2; column++) {
display[row][column-(row+1-y)+1] = '*';
}
}

for (int i = 0 ; i<100 ; i++) {
for (int j = 0; j<100; j++) {
if (display[i][j]=='')
display[i][j]=' ';
}
}

}

int main()
{
char display[100][100] = { {0} };

triangle(display, 20, 0, 5);

triangle(display, 15, 5, 5);
triangle(display, 25, 5, 5);

triangle(display, 10, 10, 5);
triangle(display, 30, 10, 5);

triangle(display, 5, 15, 5);
triangle(display, 15, 15, 5);
triangle(display, 25, 15, 5);
triangle(display, 35, 15, 5);


for (int i=0 ; i<100; i++) {
printf(" ");
for (int j=0; j<100; j++) {
printf("%c", display[i][j]);
}
}
}

Output:
*   
***
*****   
*******
*********   
* *
*** ***   
***** *****
******* *******   
********* *********
* *   
*** ***
***** *****   
******* *******
********* *********   
* * * *
*** *** *** ***   
***** ***** ***** *****
******* ******* ******* *******   
********* ********* ********* *********

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