Thread: Help writing a program

  1. #16
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int i = 0;
    
    	int width = 6;
    	/* start of line */
    	putchar('*');
    
    	for(i = 1; i< width-1; i++)
    		putchar(' ' );
    
    	putchar('*');
    	putchar('\n');
    
    	/* end of line */
    	return 0;
    }
    try to put the code between comments into loop
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  2. #17
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    Quote Originally Posted by alex1067 View Post
    The instructor said I would need a loop. Can you give me an example of what I need to do?At the rate I am going I will be here all night. What I am doing is wrong I think.

    Code:
         6  #include <stdio.h>
         7
         8  int main(void)
         9
        10  {
        11
        12  int height, width;
        13
        14  printf("Enter width:");
        15  scanf("%d", &width);
        16  printf("Enter height:");
        17  scanf("%d", &height);
        18
        19  if (width == 4)
        20  printf("****/n");
        21  else if (width == 5)
        22  printf("*****/n");
        23  else if (width == 6)
        24  printf("******/n");
        25  else if (width == 7)
        26  printf("*******/n");
        27  else if (width == 8)
        28  printf("********/n");
        29  else if (width == 9)
        30  printf("*********/n");
        31  else if (width == 10)
        32  printf("**********/n");
        33  else if (width == 11)
        34  printf("***********/n");
    Dude, copy and paste.

    Sorry, I couldn't help myself.
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The outer loop should be the height. The inner loop will be the width, controlling each horizontal line printing.

    Code:
    int hgt, height, wdt, width;
    
    for(hgt = 0; hgt < height; hgt++)   {
       for(wdt = 0; wdt < width - 2; wdt++)   {
          if(wdt == 0 || wdt == width- 1)              //the outer border
             printf("*");                                          
          if(hgt == 0)                                //first line is all stars
             printf("*");
          else
             printf(" ");                             //inner spaces 
       }
    }
    The above is just off the cuff, and totally untested, should give you some idea's

  4. #19
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    first if is missing else I think - to avoid printing * twice for the conors

    But easier way - to take the first line completely from the loop and make loop on heght starting with the second line
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing a program to make a calendar
    By Northstar in forum C Programming
    Replies: 17
    Last Post: 11-07-2007, 11:34 AM
  2. writing a calendar program help please!!!
    By Newbie2006 in forum C Programming
    Replies: 7
    Last Post: 11-20-2002, 07:36 PM
  3. Replies: 5
    Last Post: 11-19-2002, 09:36 PM
  4. Help with a file writing program
    By pritesh in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 01:56 AM