ClangStudy/diamond_art/diamond.c
2022-04-27 19:13:20 +09:00

87 lines
1.2 KiB
C

#include <stdio.h>
#include <stdlib.h>
int
output_whitespace(
int length
)
{
int i;
for ( i = 0 ; i < length ; i++ ) {
printf(" ");
}
return 0;
}
int
output_diamond_line(
int diamond_size,
int current_line
)
{
int i;
int j;
output_whitespace(diamond_size - current_line);
for ( i = 1 ; i < current_line ; i++ ) {
printf("%d", i);
}
for ( i = 0 ; i < current_line ; i++ ) {
printf("%d", current_line - i);
}
printf("\n");
return 0;
}
int
output_diamond(
int diamond_size
)
{
int i;
int j;
for ( i = 0 ; i < diamond_size; i++ ) {
output_diamond_line(diamond_size, i);
}
for ( j = diamond_size ; j > 0 ; j-- ) {
output_diamond_line(diamond_size, j);
}
return 0;
}
int
main(
int argc,
char *argv[]
)
{
int diamond_size;
if (argc < 2)
{
printf("Invalid command line arguments.\n");
return -1;
}
diamond_size = atoi(argv[1]);
if (diamond_size < 1 || diamond_size > 9)
{
printf("Can not create diamond (s: %d).\n", diamond_size);
return -1;
}
output_diamond(diamond_size);
return 0;
}