Parallelogram

This is a discussion on Parallelogram within the C Programming forums, part of the General Programming Boards category; Ok so here's my problem I am trying to make a parallelogram with a stars(*). This is what I have ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    33

    Parallelogram

    Ok so here's my problem I am trying to make a parallelogram with a stars(*). This is what I have so far
    Code:
    #include<stdio.h>
    
    int main (){
    
    int numrow, numcols, i,;
    
    printf("Enter the length of your figure:\n");
    scanf("&#37;d", &numcols);
    
    
    printf("Enter the width of your figure\n");
    scanf("%d", &numrow);
    
    
    while(numrow>0)
    for(i=0; i<numcols;i++){
                  for(i=0; i<numcols; i++)
                  {
                   printf("*"); 
                     
                    }
                                        
                       
                       
              printf("\n");
                      
              
                            
            numrow--;
            
             }
    Now that gets the width and length correct but I can input the spaces to make it offset like this.
    *****
    _*****
    __*****
    ___*****

    I know i need a loop to print the spaces but i dont know where to put it or how to word it any help would be great!.

    Thanks
    Last edited by clipsit; 02-05-2008 at 12:58 AM.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    your loops are wrong - first should be on rows, second on colums

    Code:
    for(row=0; row<numrows;row++)
    {
       /* spaces */
       for(col=0;col<row;col++)
          printf(" ");
    
       /* stars */ 
       for(col=0; col<numcols; col++)
       {
             printf("*"); 
       }
    
       /* end of line */              
       printf("\n");
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    uh ? clipsit your code will not get compiled. There is no closing '}' or return 0;

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    33
    Code:
    #include<stdio.h>
    
    int main (){
    
    int numrow, numcols, i;
    
    printf("Enter the length of your figure:\n");
    scanf("%d", &numcols);
    
    
    printf("Enter the width of your figure\n");
    scanf("%d", &numrow);
    
    
    while(numrow>0)
    for(i=0; i<numcols;i++){
                  for(i=0; i<numcols; i++)
                  {
                   printf("*"); 
                     
                    }
                                        
                       
                       
              printf("\n");
                      
              
                            
            numrow--;
            
             }
            
                                       
    
    system("pause");
    return 0;

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    this line
    Code:
    for(i=0; i<numcols;i++){
    does nothing good for you due to duplicated code in the next line
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    33
    this keeps saying syntax error what am I doing wrong?
    Code:
    #include<stdio.h>
    
    int main (){
    
    int numrows, numcols,row,col;
    
    printf("Enter the length of your figure:\n");
    scanf("%d", &numcols);
    
    
    printf("Enter the width of your figure\n");
    scanf("%d", &numrows);
    
    
    for(row=0; row<numrows;row++);
    {
       /* spaces */
       for(col=0;col<row;col++)
          printf(" ");
    
       /* stars */ 
       for(col=0; col<numcols; col++)
       {
             printf("*"); 
       }
    
                                       
    
    system("pause");
    return 0; }

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    33
    I fixed the syntax but now it doesnt print the parallelogram anymore. With that new code it only prints one line of stars

  8. #8
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Its easier for me to write my own code than to try figuring out what someone is trying to do with his/her code. So here is your code, edited, and running. I just commented out what i didn't like and wrote my own piece of code for that.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main ()
    {
    
    int numrows, numcols,row,col,spaces;
    
    printf("Enter the length of your figure:\n");
    scanf("&#37;d", &numcols);
    
    
    printf("Enter the width of your figure\n");
    scanf("%d", &numrows);
    
    
    /*for(row=0; row<numrows;row++);
    {
        spaces 
        for(col=0;col<row;col++)
                printf(" ");
    
        stars  
       for(col=0; col<numcols; col++)
       {
             printf("*"); 
       }
    
                                       
    
    */
    
    for(row=1;row<=numrows;row++)
    {
             printf("\n");
             for(spaces=0;spaces<row-1;spaces++)
                  printf(" ");
             for(col=1;col<=numcols;col++)
                  printf("*");
    }                            
    system("pause");
    return 0;
     }
    Last edited by abh!shek; 02-05-2008 at 02:43 AM.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by clipsit View Post
    this keeps saying syntax error what am I doing wrong?
    Code:
    #include<stdio.h>
    
    int main () {
    
       int numrows, numcols,row,col;
    
       printf("Enter the length of your figure:\n");
       scanf("&#37;d", &numcols);
    
    
       printf("Enter the width of your figure\n");
       scanf("%d", &numrows);
    
    
       for(row=0; row<numrows;row++);  /* <==get rid of this semi-colon */ 
       {
           /* spaces */
           for(col=0;col<row;col++)
              printf(" ");
    
           /* stars */ 
           for(col=0; col<numcols; col++)
              printf("*"); 
    
           printf("\n");
       }   
                                     
    
       system("pause");
       return 0; 
    }
    Just another note: the width of an array is the number of columns, not rows, the length of the array is the number of rows, not columns. No harm done, except the confusion, here.

    Edit: Good one, Vart! We shouldn't assume that he knows he needs a newline at the end of each row, should we?
    Last edited by Adak; 02-05-2008 at 03:11 AM.

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    And where is the endline print-out?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by clipsit View Post
    this keeps saying syntax error what am I doing wrong?
    Code:
    #include<stdio.h>
    #include<stdlib.h> /*for system("pause")*/
    int main (){
    
    int numrows, numcols,row,col;
    
    printf("Enter the length of your figure:\n");
    scanf("&#37;d", &numcols);
    
    
    printf("Enter the width of your figure\n");
    scanf("%d", &numrows);
    
    
    for(row=0; row<numrows;row++);
    {
       /* spaces */
       for(col=0;col<row;col++)
          printf(" ");
    
       /* stars */ 
       for(col=0; col<numcols; col++)
       {
             printf("*"); 
       }
    
    } /*You forgot this brace*/               
    
    system("pause");
    return 0; }
    and of course the logic for printing the parallelogram is wrong.

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    Quote Originally Posted by abk View Post
    and of course the logic for printing the parallelogram is wrong.
    Logic is OK, end line printing is missing
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by vart View Post
    Logic is OK, end line printing is missing
    Yeah! I guess printing the end line is a part of the "logic" ?? I think I should have said "logic is incomplete"

  14. #14
    Registered User
    Join Date
    Feb 2008
    Posts
    33
    Thanks abk that really makes since now. I actually am a brick trying to learn C... appreciate the help!

  15. #15
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by clipsit View Post
    I actually am a brick trying to learn C...
    There is only one brick here in Cboard and that's me. Now just don't steal my signature ok

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. parallelogram for statement issues
    By surfingbum18 in forum C Programming
    Replies: 30
    Last Post: 03-08-2009, 10:14 PM
  2. newbie needs help
    By surfingbum18 in forum C Programming
    Replies: 1
    Last Post: 09-25-2007, 12:48 AM
  3. Parallelogram
    By NavyBlue in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 10-04-2002, 10:30 AM

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