Diamond
This commit is contained in:
parent
c072df226b
commit
ffd992d549
1 changed files with 35 additions and 0 deletions
35
diamond/diamond.c
Normal file
35
diamond/diamond.c
Normal file
|
@ -0,0 +1,35 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void d_line(unsigned int i, unsigned int n) {
|
||||
unsigned int j;
|
||||
for (j = 0; j < n - i; j++)
|
||||
putchar(' ');
|
||||
for (j = 0; j < (2*i - 1); j++)
|
||||
putchar('#');
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
void diamond(int n) {
|
||||
if (n <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned int i;
|
||||
|
||||
for (i = 1; i <= n; i++) {
|
||||
d_line(i, n);
|
||||
}
|
||||
for (i = n-1; i > 0; i--) {
|
||||
d_line(i, n);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
if (argc != 2) {
|
||||
fprintf(stderr, "Usage: %s [side]\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
diamond(atoi(argv[1]));
|
||||
}
|
||||
|
Reference in a new issue