Last active
February 19, 2024 13:21
-
-
Save GReturn/3b15f51cf10fb95239d98360feb5ec97 to your computer and use it in GitHub Desktop.
Print diamond - Recursive Implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
void printSpaces(int spaceIndex) | |
{ | |
if(spaceIndex <= 0) return; | |
printf(" "); | |
return printSpaces(spaceIndex-1); | |
} | |
void printStars(int starIndex) | |
{ | |
if(starIndex <= 0) return; | |
printf("* "); | |
return printStars(starIndex-1); | |
} | |
void drawLine(int spaces, int stars) | |
{ | |
printSpaces(spaces); | |
printStars(stars); | |
printf("\n"); | |
return; | |
} | |
void printRowUpper(int row, int starCount) | |
{ | |
if(row == 0) return; | |
drawLine(row-1, starCount); | |
printRowUpper(row-1, starCount+2); | |
return; | |
} | |
void printRowLower(int row, int starCount) | |
{ | |
if(row == 0) return; | |
printRowLower(row-1, starCount+2); | |
drawLine(row, starCount); | |
return; | |
} | |
void printDiamond(int height) | |
{ | |
printRowUpper(height, 1); | |
printRowLower(height-1, 1); | |
return; | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
printDiamond(4); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment