Thread: Printing a rectangle using for loops.

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    23

    Printing a rectangle using for loops.

    Hi everyone, I'm trying to make a program that prints a rectangle using "*" asterisks. I am only allowed to use "for loops" and I simply cannot get this to work properly. Here is my code so far...
    Code:
    int main(void)
    {
    //RectangleSize represents the area of the rectangle(width*length)
        int length, width, RectangleSize;
    
        printf("Enter an integer for length:");
        scanf("%d", &length);
    
        printf("Enter an integer for width:");
        scanf("%d", &width);
    
        RectangleSize = length*width; 
        printf("The area of your rectangle is: %d\n", RectangleSize);
        printf("%d in decimal\n", RectangleSize); 
        printf("%x in hexadecimal\n", RectangleSize);
        printf("%o in octal\n", RectangleSize);
    
        int x;
        for(x=1; x<=width; x++)
        {
            printf("*");
        }
        
        for(x=0; x<=length-2; x++)
        {
            x=0;
    
            for(; x==0; x++)
            {    printf("*");    }
    
            for(; x<=width-2; x++)
            {    printf(" ");    }
    
            for(;x==width;)
            {    printf("*\n"); 
                for(x=1; x<=width; x++){
            
            {
                printf("*");
            
        
            
        
        }
    
       
        return 0;
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should indent your code properly, e.g.,
    Code:
    int main(void)
    {
        //RectangleSize represents the area of the rectangle(width*length)
        int length, width, RectangleSize;
    
        printf("Enter an integer for length:");
        scanf("%d", &length);
    
        printf("Enter an integer for width:");
        scanf("%d", &width);
    
        RectangleSize = length*width;
        printf("The area of your rectangle is: %d\n", RectangleSize);
        printf("%d in decimal\n", RectangleSize);
        printf("%x in hexadecimal\n", RectangleSize);
        printf("%o in octal\n", RectangleSize);
    
        int x;
        for (x = 1; x <= width; x++)
        {
            printf("*");
        }
    
        for (x = 0; x <= length - 2; x++)
        {
            x = 0;
    
            for (; x == 0; x++)
            {
                printf("*");
            }
    
            for (; x <= width - 2; x++)
            {
                printf(" ");
            }
    
            for (; x == width; )
            {
                printf("*\n");
                for (x = 1; x <= width; x++)
                {
                    printf("*");
                }
            }
        }
    
        return 0;
    }
    I have taken the liberty of fixing several typo errors related to the braces. These were hard to spot before proper indentation was introduced. Remember to #include <stdio.h> since you use scanf and printf. You should also check the return value of scanf, but do not worry about that for now.

    Now, one problem you still have appears to be the reuse of x. For the inner loops, use a different variable. I am guessing that you are trying to use for loop as if statements: do not do that, just use if statements.

    That said, I suggest that you have three outer loops: one to print the top border, one to print the middle section, and one to print the bottom border. Only the outer loop that prints the middle section needs to have an inner loop, i.e., to print the spaces. This way, you do not need any if statements.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    23
    So far I have my code down to this. The second outer for loop seems to be the problem. I cant get it to print properly. any ideas?

    Code:
    #include<stdio.h>
    
    int main(void)
    {
    //RectangleSize represents the area of the rectangle(width*length)
        int length, width, RectangleSize;
    
        printf("Enter an integer for length:");
        scanf("%d", &length);
    
        printf("Enter an integer for width:");
        scanf("%d", &width);
    
        RectangleSize = length*width; 
        printf("The area of your rectangle is: %d\n", RectangleSize);
        printf("%d in decimal\n", RectangleSize); 
        printf("%x in hexadecimal\n", RectangleSize);
        printf("%o in octal\n", RectangleSize);
    
    //for loops used to print a rectangle 
        int x, y;
        for(x=1; x<=width; x++)
        {
            printf("*");
        }
        
        for(x=0; x<=length-2; x++)
        {
            y=0;
            for(;y==0; y++)
            {    
                printf("*");   
                   }
    
            for(;y<=width-2; y++)
            {    
                printf(" ");
            }
    
            for(;y==width;)
            {    
                printf("*\n");
                  }         
        }
    
    /*    for(x=1; x<=width; x++)
        {
            printf("*");    
        }
       */
        return 0;
    
    }
    Last edited by titaniumnuke; 01-28-2015 at 12:44 PM.

  4. #4
    Registered User
    Join Date
    Oct 2014
    Posts
    23

    For loops to create shapes

    HI, I need to create a rectangle using only for loops. The code prints the rectangle but it is missing its right side. Here's my code.

    Code:
    #include<stdio.h>
    
    int main(void)
    {
    //RectangleSize represents the area of the rectangle(width*length)
        int length, width, RectangleSize;
    
        printf("Enter an integer for length:");
        scanf("%d", &length);
    
        printf("Enter an integer for width:");
        scanf("%d", &width);
    
        RectangleSize = length*width; 
        printf("The area of your rectangle is: %d\n", RectangleSize);
        printf("%d in decimal\n", RectangleSize); 
        printf("%x in hexadecimal\n", RectangleSize);
        printf("%o in octal\n", RectangleSize);
    
    //for loops used to print a rectangle 
        int x, y;
        for(x=1; x<=length; x++)
        {
            printf("*");
        }
    
        for(x=0; x<=width-2; x++)
        {
            y=0;
            printf("\n");
            for(;y==0; y++)
            {    
                printf("*");   
                   }
    
            for(;y<=length-2; y++)
            {    
                printf(" ");
            }
    
            for(;y==length;)
            {    
                printf("*");
               }         
        }
        printf("\n");
    
        for(x=1; x<=length; x++)
        {
            printf("*");    
        }

    The ouput of the code for 9 length and 12 width.

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

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    At the end of this loop,

    Code:
            for(;y<=length-2; y++)
            {    
                printf(" ");
            }
    y = length-1;

    So in the next loop,

    Code:
            for(;y==length;)
            {    
                printf("*");
               }
    y==length is never true.

    Also, notice the rectangle is 13 high, not 12. This problem, like the first, is also related to your loop conditions, "x <= width-2, and "y <= length-2", and the initial values of x and y.

    Generally, when you need a loop to execute n times, you use one of two forms:

    (x = 0; x < n; x++)

    or

    (x = 1; x <= n; x++)

    It's also conventional to use x for horizontal dimensions and y for vertical dimensions.

    -
    Last edited by megafiddle; 01-28-2015 at 08:26 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    titaniumnuke, I have merged your two threads into the first one as it looks like you are basically asking the same thing. It looks like you still need to indent your code better, though it is improving in that aspect somewhat.

    Frankly, you should not start with something as obfuscated as this:
    Code:
    for(;y==0; y++)
    {   
        printf("*");  
    }
    Just print the border:
    Code:
    printf("*");
    If you really want to handle the case of the length or width being less than 2, then first write an if statement if necessary, and only after you're sure it works, then convert it to the ridiculous notion of using a for loop when an if statement is appropriate. Note that you have to consider the top and bottom borders too, if you want to handle such cases, but focus on getting it right with a simple solution for length > 1 and width > 1 first.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ programing , Loops , infinite loops issue
    By Ibrahim Lawleit in forum C++ Programming
    Replies: 15
    Last Post: 07-14-2014, 03:41 PM
  2. c program printing out garbage (arrays and for loops)
    By starbucks10v3r in forum C Programming
    Replies: 27
    Last Post: 11-20-2013, 01:23 AM
  3. Need help with arrays and printing them in for loops....
    By Alex Coven in forum C Programming
    Replies: 15
    Last Post: 10-17-2013, 07:14 PM
  4. Printing values and their ascii symbol with for loops.
    By mann322 in forum C++ Programming
    Replies: 3
    Last Post: 11-11-2011, 10:10 AM
  5. Loops keep printing 0
    By kaopei in forum C Programming
    Replies: 15
    Last Post: 12-03-2010, 10:17 AM

Tags for this Thread