Created
July 23, 2019 16:15
-
-
Save haithamsha/ee1a3a40e8e160a7bf29c5622b798cf3 to your computer and use it in GitHub Desktop.
Mario problem set 1 with c lang
This file contains hidden or 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> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| void makepyramid(int n); | |
| char * printSpaces(int n); | |
| int main() | |
| { | |
| int inputVal = 0; | |
| int scanVal =0; | |
| // Get the rows value from the user until the value between 1 and 8 | |
| while(inputVal<=0 || inputVal > 8) { | |
| printf("Height: "); | |
| scanVal = scanf("%i", &inputVal); | |
| } | |
| makepyramid(inputVal); | |
| return 0; | |
| } | |
| void makepyramid(int n) { | |
| // Double n and add 2 to it | |
| n = n*2 + 2; | |
| int r = n/2 +1; | |
| // Make Nested loop to print number of rows of hashes | |
| for(int i = 2; i<n;i+=2) { | |
| // Inject number of spaces according to this equation z = (n-i)/2+1 | |
| int z = (n-i)/2-1; | |
| printf("%s", printSpaces(z)); | |
| for(int j=i;j>0;j--) { | |
| printf("#"); | |
| if(j == (i/2) + 1) { | |
| printf(" "); | |
| } | |
| } | |
| printf("\n"); | |
| } | |
| } | |
| char * printSpaces(int n) { | |
| char *str = (char *)malloc(sizeof(char)*n); | |
| for(int i = 0; i<n;i++) { | |
| strcat(str, " "); | |
| } | |
| return str; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment