-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmario.c
57 lines (47 loc) · 1.19 KB
/
mario.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Print an adjacent pyramids of hashes separated by a "gap"
#include <cs50.h>
#include <stdio.h>
int get_height(string prompt);
void print_pyramids(int height);
int main(void)
{
int height = get_height("Height: ");
print_pyramids(height);
}
// Prompt user for a positive integer between 1 and 8
int get_height(string prompt)
{
int n;
do
{
n = get_int("%s", prompt);
}
while (n < 1 || n > 8);
return n;
}
// Print an adjacent pyramids of hashes of height 'height' separated by a "gap"
void print_pyramids(int height)
{
// Print n rows
for (int i = 0; i < height; i++)
{
// Formatting for right-alignment
for (int j = 0; j < height - i - 1; j++)
{
printf("%s", " ");
}
// Print k hashes on each row of the first pyramid
for (int k = 0; k <= i; k++)
{
printf("%s", "#");
}
// Print a “gap” between adjacent pyramids equal to the width of two hashes
printf("%s", " ");
// Print k hashes on each row of the second pyramid
for (int k = 0; k <= i; k++)
{
printf("%s", "#");
}
printf("%s", "\n");
}
}