Expendable house program, please help

This is a discussion on Expendable house program, please help within the C Programming forums, part of the General Programming Boards category; Hello, I wrote this program that prints out the house that looks something like this initially if you enter width ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Location
    Philadelphia, PA
    Posts
    27

    Expendable house program, please help

    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);
    
    }
    Last edited by ademkiv; 02-20-2006 at 02:46 PM.

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    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.
    Last edited by CrazedBrit; 02-20-2006 at 11:55 PM.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    Or just create two functions
    - drawRoof
    - drawWalls
    passing both a suitable parameter to indicate the size you want.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Location
    Philadelphia, PA
    Posts
    27
    Thank you for taking time to help, I will try to test the code later today. Have to go to class right now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program quits unexpected
    By skelesp in forum C Programming
    Replies: 34
    Last Post: 12-10-2008, 08:10 AM
  2. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  3. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  4. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21