I would do it like this:
Code:
itsme@itsme:~/C$ cat hourglass.c
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
void draw_line(int row, int height, char symbol)
{
int i;
for(i = 0;i < row;++i)
putchar(' ');
for(;i < height-row;++i)
putchar(symbol);
putchar('\n');
}
int main(void)
{
int height, row;
char symbol;
char buf[100];
do
{
printf("Enter a single character: ");
fflush(stdout);
fgets(buf, sizeof(buf), stdin);
symbol = *buf;
} while(!isprint(symbol));
do
{
printf("Enter an odd integer between 1 and 19: ");
fflush(stdout);
fgets(buf, sizeof(buf), stdin);
height = atoi(buf);
} while(height < 1 || height > 19 || !(height%2));
for(row = 0;row < height/2;++row)
draw_line(row, height, symbol);
for(row = height/2;row >= 0;--row)
draw_line(row, height, symbol);
return 0;
}
itsme@itsme:~/C$ ./hourglass
Enter a single character: I
Enter an odd integer between 1 and 19: 19
IIIIIIIIIIIIIIIIIII
IIIIIIIIIIIIIIIII
IIIIIIIIIIIIIII
IIIIIIIIIIIII
IIIIIIIIIII
IIIIIIIII
IIIIIII
IIIII
III
I
III
IIIII
IIIIIII
IIIIIIIII
IIIIIIIIIII
IIIIIIIIIIIII
IIIIIIIIIIIIIII
IIIIIIIIIIIIIIIII
IIIIIIIIIIIIIIIIIII
itsme@itsme:~/C$