Thread: While statements and loops

  1. #1
    Registered User
    Join Date
    Oct 2010
    Location
    The land of nod
    Posts
    32

    While statements and loops

    Hello all!
    I'm currently working on a very basic program to draw a rectangle using asterisks, like so:

    ****
    ****
    ****
    ****

    Not much point in it, but its all part of the learning process.

    I'm trying to use while statements to get the program to draw the shape. I'm not sure if its the most efficient way of doing this but its working so far for me. At the moment I have 2 while statements which work fine, except when you run the program instead of making a rectangle my program makes this shape;

    ****
    *
    *
    *

    I figure its because my second while statement doesnt point back to my first one, or that I have to put the second while statement within the first. However all my attempts at this have been fruitless. If anyone could point me in the right direction it'd be much appreciated!

    Thank you!

    Native


    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main(int argc, char *argv[])
    {
            int width = 0;
            int height = 0;
    
            printf("Please enter a width and height seperated by a comma\n");
            scanf("%d,%d",&width,&height);
    
    
            while (width>0)
            {
                    printf("*");
                    width--;
            }
    
            while (height>0)
            {
                    printf("*\n");
                    height--;
            }
    }
    Last edited by Native; 10-27-2010 at 07:01 AM. Reason: tidying

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Your assumption about putting one loop in the other is correct, although the problem you were having will have to remain a mystery since we cannot see what you have tried.

    But the idea for printing a rectangle can be done as follows: loop “height” times. Each iteration of the loop, do another loop, this time doing it “width” times. Print an asterisk each iteration. This isn't quite complete, but you should be able to figure out what to do. You might look at a for loop.

    When you're having trouble with a problem like this, see if you can't sketch out what you need to do on paper. Draw asterisks (or dots, or whatever) in a rectangle, and see what steps you had to take. You'll find out how many asterisks you'll need (which, of course, is simply w×h; but it's easy to forget about the simple things when you're learning a new language); how the repetition you use while drawing might be translated into a program; and so on.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Location
    The land of nod
    Posts
    32
    Well I thought maybe this would work, but still not happening. I've also noticied my program is printing one too many dots horizontally '>_<'

    Code:
            while (width>0)
            {
                    printf("*");
                    width--;
             
                    while (height>0)
                    {
                            printf("*\n");
                            height--;
                    }
            }
    Last edited by Native; 10-27-2010 at 08:23 AM.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Are you sure that newline is where you want it to be? Remember, the command on the inner loop gets executed width*height times. How many newlines do you actually want?

    Now, how many asterisks do you actually want in the end? Width * height, right? So if the inner loop is executed width * height times, and the one in the outer loop is executed width times, how many asterisks are you actually printing?

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Consider what height will start out at the second time through the outer (width) loop. You're missing one small detail the way it is now.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Location
    The land of nod
    Posts
    32
    Thanks for your help guys, just put on some music and cracked open a red stripe. Will post again after playing around with some code!

  7. #7
    Registered User
    Join Date
    Oct 2010
    Location
    The land of nod
    Posts
    32
    Well I have tried everything possible (examples below) and I cannot get this thing working - I know you guys are trying to help by hinting at what to do but honestly I've gotten this far already but I cannot see what is wrong..... :/

    Code:
      while (width>0)
            {
                    printf("*");
                    width--;
             
                    while (height>0)
                    {
                            printf("*\n");
                            height--;
                    }
            }
    Code:
      while (height>0)
            {
                    printf("*");
                    height--;
             
                    while (width>0)
                    {
                            printf("*\n");
                            width--;
                    }
            }
    Code:
      while (height>0)
            {
                    printf("*\n");
                    height--;
             
                    while (width>0)
                    {
                            printf("*");
                            width--;
                    }
            }
    Code:
    while (width>0)
            {
                    printf("*");
                    width--;
    
                    if (width=0) 
                    {
                            printf("\n");
                     
                            while (height>0)
                            {
                                    printf("*");
                                    height--;
                            }
                    }
            }
    Last edited by Native; 10-27-2010 at 09:16 AM. Reason: needed formatting

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You might try something like this...
    Code:
    int width = 20;   // total width of patch
    int height = 10;  // total height of patch
    int wt;           // temporary width
    
    while (height-- > 0)
       { wt = width;
           while (wt-- > 0)
               { puts("*"); }
            puts("\n"); }
    The trick is to not destroy the initializer for the inner loop.

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Also, it might be clearer with for loops instead.
    Code:
    int width = 20;
    int height = 10;
    int w, h;
    
    for(w = 0;w < width;++w)
    {
      for(h = 0;h < height;++h)
        putchar('*');
      putchar('\n');
    }
    If you understand what you're doing, you're not learning anything.

  10. #10
    Registered User
    Join Date
    Oct 2010
    Location
    The land of nod
    Posts
    32
    Trying not to terminate my loop but still not working. Driving me nuts :/

    Code:
    #include <stdio.h>
    
    int main()
    {
            int width;
            int height;
            int twidth;
    
            printf("Please enter a width and height seperated by a comma\n");
            scanf("%d,%d",&width,&height);
    
            while (height>0)
            {
                    twidth = width;
    
                    while (width>0)
                    {
                            printf("*");
                            width = width-1;
                    }
                    height = height-1;
                    printf("\n");
            }
    }

  11. #11
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    You are not restoring the width.

  12. #12
    Registered User
    Join Date
    Oct 2010
    Location
    The land of nod
    Posts
    32
    Quote Originally Posted by cas View Post
    You are not restoring the width.
    Sorry I am a beginner to C so im not hugely sure what you mean

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Native View Post
    Trying not to terminate my loop but still not working. Driving me nuts :/

    Code:
    #include <stdio.h>
    
    int main()
    {
            int width;
            int height;
            int twidth;
    
            printf("Please enter a width and height seperated by a comma\n");
            scanf("%d,%d",&width,&height);
    
            while (height>0)
            {
                    twidth = width;
    
                    while (twidth>0) <--- use twidth here
                    {
                            printf("*");
                            twidth = twidth-1;  <--- and here 
                    }
                    height = height-1;
                    printf("\n");
            }
    }
    Your problem is that you are destroying your initializer (width) for the inner loop.

  14. #14
    Registered User
    Join Date
    Oct 2010
    Location
    The land of nod
    Posts
    32
    Quote Originally Posted by CommonTater View Post
    Your problem is that you are destroying your initializer (width) for the inner loop.
    Cheers man it finally works. I just wanted to check what is actually going on as I think I only have a partial understanding.

    Code:
            while (height>0)
            {
                    twidth = width;
    
                    while (twidth>0)
                    {
                            printf("*");
                            twidth = twidth-1;
                    }
                    height = height-1;
                    printf("\n");
            }
    Does that section set twidth to equal width so that wheen the 2nd part of the loop is completed it references back to width which is still at its original value?

    Thanks again
    Native

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Native View Post
    Does that section set twidth to equal width so that wheen the 2nd part of the loop is completed it references back to width which is still at its original value?
    Correct. When the inner loop graduates, the outer loop moves to it's next iteration, the function reassigns twidth to width. Since width is now untouched it's getting the same value every time. In your original attempts you were altering the value of width... which would be reflected in the next iteration of your loop.

    As an exercise in understanding you should sit with paper and pencil and work out the steps that are executed, with the values of each variable at each step... it will become much clearer.

    Also you should try to do it with the loops counting up from 0... it will require different exit conditions in your loops and will provide you with an interesting look at different ways to do the same thing.

    As an optimization you can change var = var - 1; to var--; which gives you the same result. The -- is C shorthand for subtract 1... ++ adds 1. But it's also a little bit faster (not that speed really matters here)
    Last edited by CommonTater; 10-27-2010 at 12:09 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. beginner help loops
    By aadil7 in forum C Programming
    Replies: 9
    Last Post: 03-15-2010, 01:41 AM
  2. Loops: The 'any' inquiry
    By chubbs1900 in forum C++ Programming
    Replies: 6
    Last Post: 12-10-2007, 10:35 AM
  3. do and while loops
    By cj_h4x0r in forum C++ Programming
    Replies: 13
    Last Post: 10-23-2006, 04:17 PM
  4. Need Help With My Loops??
    By wwwslyguy in forum C Programming
    Replies: 4
    Last Post: 10-12-2006, 01:15 AM
  5. Loops using while statement?????
    By bearcat19 in forum C Programming
    Replies: 1
    Last Post: 01-28-2002, 11:17 AM