Program almost finished

This is a discussion on Program almost finished within the C Programming forums, part of the General Programming Boards category; Last weekend I posted about this program to make a "table shape" out of asterisks. I am almost there except ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    32

    Program almost finished

    Last weekend I posted about this program to make a "table shape" out of asterisks. I am almost there except for the missing left leg of the table ,and the bottom row of asterisks does't need to be there. Any ideas? Thanks

    Code:
    Code:
     
         6  #include <stdio.h>
         7
         8  int main(void)
         9
        10  {
        11
        12  int height, width, hgt, wdt;
        13
        14  printf("Enter width:");
        15  scanf("%d", &width);
        16  printf("Enter height:");
        17  scanf("%d", &height);
        18
        19
        20  for(hgt = 0; hgt < height; hgt++)   {
        21     for(wdt = 0; wdt < width - 2; wdt++)   {
        22        if(wdt == 0 || wdt == width- 1)
        23           printf("*\n");
        24    else if(hgt == 0 || hgt == height - 1)
        25           printf("*");
        26        else
        27           printf(" ");
        28     }
        29  }
        30  printf("\n");
        31  printf("PROGRAM ENDS\n");
        32  return 0;
        33
        34  }
    Here is the output

    Code:
    Code:
    Enter width:10
    Enter height:5
    *
    ********
           *
           *
           *
    *******
    PROGRAM ENDS




    Any suggestions? Thank you for help!

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    It's probably best you call them 'rows' and 'columns' rather than, 'width' and 'height'.

    The pseudo-code might look something like:
    Code:
    
    FOR row = 0; row < numRows; row++
    
        FOR column = 0; column < numColumns; column++
        
            IF column == 0 || row == 0 || column == numColumns - 1      First column, last column or first row?
                PRINT "*"
            ELSE
                PRINT " "                                               Neither first row, first column or last column
                
        END FOR
        PRINT "\n"
    END FOR
    Last edited by zacs7; 04-05-2008 at 10:45 PM. Reason: Can't spell :(

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int height, width, hgt, wdt;
    
    	printf("Enter width:");
    	scanf("%d", &width);
    	printf("Enter height:");
    	scanf("%d", &height);
    	
    	for ( hgt = 0; hgt < height; hgt++)   {
    		for ( wdt = 0; wdt < width - 2; wdt++)   {
    			if ( wdt == 0 || wdt == width- 1) printf("*\n") ;
    			else if ( hgt == 0 || hgt == height - 1) printf("*");
    			else printf(" ");
    		}
    	}
    	printf("\n");
    	printf("PROGRAM ENDS\n");
    	return 0;
    }
    You want full width.
    You want a newline on each line, but only after the full width has been printed each (AKA, after your inner loop completes)
    For blue and green, you pretty much got it (good job!), just fix the other two things.

    Here's the logic you need:

    1) If col==0 or col==max, print a star.
    2) else if row==0 or row==max, print a star
    3) else print a blank
    4) print a newline
    5) repeat
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    32
    Ok the - 2 does not need to be there for the width. it just needs to be less than the width. That gives me the left leg. Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  2. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 09:48 AM
  3. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM
  4. Replies: 3
    Last Post: 01-14-2003, 10:34 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM

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