Program that creates a parallelogram?

This is a discussion on Program that creates a parallelogram? within the C Programming forums, part of the General Programming Boards category; Hello, I need help with figuring out how to create a program that creates a parallelogram made out of star ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    244

    Program that creates a parallelogram?

    Hello,
    I need help with figuring out how to create a program that creates a parallelogram made out of star characters (such as:'*'). This program is suppose to test my 'loop' skills.

    The program is suppose to ask the user the height and width of the parallelogram and then display the parallelogram on the screen.

    Right now, the program is displaying a square but NOT a parallelogram. I am having trouble with incorporating spaces into my loop.

    For example: If I wanted to create a loop that simply prints out the letter: X with no spaces then creates a new line, then prints out X again with ONE space, then creates a new line, then prints out the letter X again with TWO spaces, then three spaces, then four, etc.....




    Can someone please help?




    Code:
    int main () {
    
       int z, i, n, height, width;
       printf("Please enter the height of the parallelogram.\n");
          scanf("%d", &height);
       printf("Please enter the width of the parallelogram.\n");
          scanf("%d", &width);
       printf("\n\n\n");   
          
       for(i=0; i < height; i++){
          for(n=1; n < width; n++)
             printf("*");
          printf("*");
    
       printf("\n");
     
    
    
       
    }
    Last edited by matthayzon89; 05-08-2010 at 02:14 PM.

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    Read up on the * modifier in fmtstr conversions.

    Basically you can use something like printf("%*s",i,""); in your loop which will tell printf to print a blank string of width specified by i.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Oh okay!
    Thanks for the info, I was trying sooo many different tricks but was unable to accomplish this. Thanks again!

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    I was trying to find information about fmtstr with out much luck:/

    I tried including ***printf("%*s",i,"")**** in my code and it is still not working well:/


    Code:
    #include <stdio.h>
    
    
    int main () {
    
       int i, n, height, width;
       char z;
       printf("Please enter the height of the parallelogram.\n");
          scanf("%d", &height);
       printf("Please enter the width of the parallelogram.\n");
          scanf("%d", &width);
       printf("\n\n\n");   
          
       for(i=0; i < height; i++){
          for(n=1; n < width; n++)
             printf("*");
          printf("*");
       for(z=0; z < width; z++)
       printf("%*s",z,"");
       printf("\n");
     
    
    
       
    }
    
                
       
       
       system("PAUSE");
       return 0;
       
    }

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    You still only need 2 for loops, 1 for height 1 to actually print out the line of *. So if you have a for loop such as this:

    Code:
    for(i=0; i<height;i++) {
        printf("%*s",i,"");
        for(k=0;k<width;k++)
            putchar('*');
        putchar('\n');
    }
    The above is all you need to create a parallelogram. So the first printf statement prints the spaces, starting with 0 spaces, then 1 space, then 2 space BEFORE printing the line of *******s after which it prints another \n to start the next line...you have some excess printf calls in your version.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Thanks man! I finally got it~!

    I appreciate it.

    P.S
    I used printf instead of putchar lol just out of curiousity, why putchar instead of printf? they both work in this case...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Check if a program creates prt scrs
    By Livijn in forum C# Programming
    Replies: 6
    Last Post: 09-11-2008, 05:43 AM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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