Thread: Printing a hexagon using loops?

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

    Printing a hexagon using loops?

    Hi everyone. I need help getting my program to print a hexagon made of asterisks. I can get the program to print a solid hexagon, but I need a hollow hexagon. Can anyone help me out?

    Code:
    #include<stdio.h>
    
    int main(void)
    {
        int length, i=0, j=0, k, l;
        printf("Please enter the length for the sides of the hexagon:");    
        scanf("%d", &length);
    
        for(i = 1, k=length, l=2*length-1; i<length; i++, k--, l++)
        {
            for(j = 0; j < 3*length; j++)
            {
                if(j>=k && j<=l)
                {
                    printf("*");
                }
    
                else
                    printf(" ");
            }
                    printf("\n");
        }
                
        for(i = 0, k=1, l=3*length-2; i<length; i++, k++, l--)
            {
                for(j=0; j<3*length;j++)
                {
                    if(j>=k && j<=l)
                        printf("*");
                    else
                        printf(" ");
                }
                printf("\n");
            }
            
    
    }
    It looks like this:
    Printing a hexagon using loops?-pic1-png

    but it needs to look like this:
    Printing a hexagon using loops?-pic2-png
    Last edited by titaniumnuke; 02-03-2015 at 09:27 PM.

  2. #2
    Registered User
    Join Date
    Oct 2014
    Posts
    23
    bump

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do you understand how your current code works? It should just be a matter of printing spaces instead of asterisks at certain parts of your code.
    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

  4. #4
    Registered User
    Join Date
    Oct 2014
    Posts
    23
    I think I need to chang one of my loops but I can't figure it out.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You already have the shape though, if the loop was very wrong, the shape would be very wrong. I think you need to realize where the stars are in your shape so you can fix the code. You are supposed to print way more spaces than stars.

  6. #6
    Registered User
    Join Date
    Oct 2014
    Posts
    23
    I guess the problem im having is printing the stars in the right spots. Could anyone help me out?

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well it's mostly a problem for the if(). Consider that the current character you need to print, whether it is a space or a star, means that you are either on a boundary line or you aren't. When you compare your loop variable to the boundaries you will know when you are supposed to print a star.

  8. #8
    Registered User
    Join Date
    Oct 2014
    Posts
    23
    This is my new code. The top and bottom of the hexagram are the only part that is still missing. Can someone please help? I've been working on this for a very long time.
    Code:
    #include<stdio.h>
     
    int main(void)
    {
        int length, i=0, j=0, k, l;
        printf("Please enter the length for the sides of the hexagon:");   
        scanf("%d", &length);
          
        // for(i=0; i<length; i++)
        //{
        //     printf("*");
        //    }
        //
    
        for(i = 1, k=length, l=2*length-1; i<length; i++, k--, l++)
        {
        for(j = 0; j < 3*length; j++)
            {
            if(j==k || j==l)
                {
                    printf("*");
            
                }
     
                else
                    printf(" ");
            }
                    printf("\n");
              
        }
                 
        for(i = 0, k=1, l=3*length-2; i<length; i++, k++, l--)
            {
                for(j=0; j<3*length;j++)
                {
                    if(j==k || j==l)
                {
                    printf("*");
            
                }
     
                else
                    printf(" ");
            }
                    printf("\n");
            }
             
     
    }

    output:Printing a hexagon using loops?-pic3-png

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The first and last lines are different and I think you wrote part of it before. It's when you begin or end the outer loops that the behavior is different.
    Code:
    if (j==k || j==l || (i == 1 && j >= k && j <= l)
    Try that.
    The other one the i will be length - 1

  10. #10
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    question, why are you using printf if you're not adding any parameters? just use puts instead for speed
    Last edited by awsdert; 02-04-2015 at 04:12 AM. Reason: grammer mistake

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It's not likely to make a difference, and using puts() in particular is wrong, since the output would include a newline.

  12. #12
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    even a few cycles can make a visible difference but good point on puts(), putchar() is better idea for those few cycles

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    But the program is I/O bound anyway; it can only be as fast as it takes the computer to flush the output stream. That might not happen until the program closes, or maybe when an \n is pushed to stdout.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing a rectangle using for loops.
    By titaniumnuke in forum C Programming
    Replies: 5
    Last Post: 01-28-2015, 10:46 PM
  2. 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
  3. 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
  4. Loops keep printing 0
    By kaopei in forum C Programming
    Replies: 15
    Last Post: 12-03-2010, 10:17 AM

Tags for this Thread