• 02-20-2006
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.

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); }```
• 02-21-2006
CrazedBrit
The only way for you to really do this nicely would be to change the number of lines you use for the roof depending on the width entered.
This is what I came up with, feel free to do it however you wish though.
Code:

```#include <stdio.h> int main() {         int loop,loop2,width,x,y;         puts("Enter a width for the house, it must be an odd number greater than 3");         scanf("%d",&width); /*prints roof*/         for(loop=0;loop<((width-1)/2);loop++)                 putchar(' ');         putchar('*');         putchar('\n');         y = 1;         x = (width-1)/2 - 1;         for(loop=0;loop<(width-1)/2;loop++)         {                 for(loop2=0;loop2<x;loop2++)                         putchar(' ');                 putchar('*');                 for(loop2=0;loop2<y;loop2++)                         putchar(' ');                 putchar('*');                 putchar('\n');                 y+=2;                 x--;         }         /*prints body*/         for(loop=0;loop<width;loop++)                 putchar('*');         putchar('\n');         for(loop=0;loop<width;loop++)         {                 putchar('*');                 for(loop2=0;loop2<width-2;loop2++)                         putchar(' ');                 putchar('*');                 putchar('\n');         }         for(loop=0;loop<width;loop++)                 putchar('*');         return(0); }```
You'll notice that with the use of a lot more loops the code can be cut from your 116 lines to about 48 with mine. Bear in mind that with a fairly repetetive thing like printing stars to make a symetrical house, there is almost definatly a way to do it using loops that makes the code shorter and cleaner. Also, not how I only use two variable to manage my loops, one for an outer loop and one for the inner loops nested inside it. If there were loops in loops in loops I would use even more. But as it is, this avoids having to declare a lot more variables then are actually needed. I also used putchar instead of printf, because it will print only one character, which is all you need in this program, and is simply neater than using printf. Sorry for the lack of comments, but if you have any questions just ask, and I will, or anyone else will happily explain.

edit: p.s. with a little tweaking it would be possible to make this program create a house with an even width, it would just require the point to be two stars wide instead of one. If you feel like a challenge try doing that and post your attempts. And if you do that, and you still feel up to a challenge, try to combine the two programs so that it decides for itself if the house is of even width or odd, and then execute the proper code.
• 02-21-2006
Salem
Or just create two functions
- drawRoof
- drawWalls
passing both a suitable parameter to indicate the size you want.
• 02-21-2006