-
Notifications
You must be signed in to change notification settings - Fork 0
/
digitshapes.c
58 lines (52 loc) · 1.05 KB
/
digitshapes.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
#include <stdio.h>
#include <assert.h>
void digit_row(int start, int width) {
for (int curr = start, count = 0; count< width; ++count, ++curr) {
if (curr == 10) {
curr = 0;
}
printf("%d",curr);
}
printf("\n");
}
void digit_box(int height, int width) {
assert(height > 2);
assert(width > 2);
for (int i = 1; i <= height; ++i) {
if ((i == 1) || (i == height)){
digit_row(i, width);
} else {
for (int j = 1; j <= width; ++j) {
if (j == 1) {
printf("%d",i);
} else if (j == width) {
printf("%d\n",i+1);
} else {
printf(" ");
}
}
}
}
}
void space_row(int n){
for(;n > 0; --n){
printf(" ");
}
}
void digit_diamond(int n) {
assert(n % 2 == 1);
for (int i = 0; i < n; ++i) {
space_row(n - 1 - i);
digit_row(i + 1, 2 * i + 1);
}
for (int i = n - 2; i >= 0; --i) {
space_row(n - 1 - i);
digit_row(i + 1, 2 * i + 1);
}
}
int main(void) {
digit_box(3,3);
digit_box(4,12);
digit_diamond(3);
digit_diamond(7);
}