Hello,
I wrote this program that prints out the house that looks something like this initially if you enter width as 7.
Code:
*
* *
* *
* *
*********
* *
* *
* *
* *
* *
* *
* *
* *
* *
*********
But when I enter any other odd number bigger than 7, for example 9, the square expands properly, but the top part (up-side-down V) does't expand properly. Here's what it looks like when 9 is the width:
Code:
*
* *
* *
* *
***********
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
***********
I tried changing my formulas, but I just can't get it right. I know that there should be a pattern in them, but I can't figure it out.
Please help me.
Here's my code:
Code:
#include<stdio.h>
int
main(void)
{
int width;
printf("Enter width for the house, it must be odd number bigger than 5> ");
scanf("%d", &width);
/* prints out first star */
int first =0;
while(first < (width-1)/2)
{
printf("%c",' ');
first=first+1;
}
printf("%c",'*');
printf("\n");
/* prints out second stars */
int before =0;
while(before <(width-3)/2)
{
printf("%c",' ');
before=before+1;
}
printf("%c",'*');
int second =0;
while(second < (width-4)/2)
{
printf("%c",' ');
second=second+1;
}
printf("%c",'*');
printf("\n");
/* prints out third stars */
int before1 =0;
while(before1 <(width-4)/2)
{
printf("%c",' ');
before1=before1+1;
}
printf("%c",'*');
int third =0;
while(third < (width-1)/2)
{
printf("%c",' ');
third=third+1;
}
printf("%c",'*');
printf("\n");
/* prints out fourth stars */
int before2 =0;
while(before2 <(width-6)/2)
{
printf("%c",' ');
before2=before2+1;
}
printf("%c",'*');
int third2 =0;
while(third2 < (width+3)/2)
{
printf("%c",' ');
third2=third2+1;
}
printf("%c",'*');
printf("\n");
/* prints out square */
int count = 0;
while(count < width)
{
printf("%c", '*');
count=count+1;
}
printf("\n");
int count1 = 0;
while(count1 < width)
{
printf("%c", '*');
int count2 = 0;
while(count2 < width-2) {
printf("%c", ' ');
count2=count2+1;
}
printf("%c", '*');
printf("\n");
count1=count1+1;
}
int count3 = 0;
while(count3 < width)
{
printf("%c", '*');
count3=count3+1;
}
printf("\n");
return(0);
}