Thread: parallelogram for statement issues

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    16

    parallelogram for statement issues

    Ok so here the problem i just got into a beginning programming C class and my teacher kind of sucks and i would love some help....

    basically im writing a program that writes a parallelogram with a input of a width and a length...

    so basically if you want the length to be 5 and you want the width to be 4 the output would look like this

    Code:
    *****
      *****
        *****
          *****
    (each row moves over to the right about 2 spaces everytime it goes to the next line)

    pretty simple im assuming... i pretty much understand everything on how to do everything but the for statement part or the while statement im currently trying to use the for statement because i think it is easiest to read... but for the life of me i cant get it to compile and after i input my 2 numbers print anything near to the right thing... if anyone could help me with the basic set up and a brief explanations i would absolutely love it... thanks so much

    surfingbum18
    Last edited by surfingbum18; 09-25-2007 at 12:34 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    One for loop prints a row of how ever many.
    Another for loop outside that does the number of rows.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    16
    ok well this is the kind of set i have right now, forgive my poor coding for i am new lol... im sure some of it is very wrong but i would greatly appreciate the help.. thanks!

    Code:
       
    #include <stdio.h>
    
    int main () {
        int length, width, max, num;
        int rate=1;
        
        
        //This will take in the input of the length
        printf("Enter the length of your parallelogram.\n");
        scanf("%d", &length);
    
        //This will take in the input of the width
        printf("Enter the width of your parallelogram.\n");
        scanf("%d", &width);
    
        //This will print out the parallelogram
        printf ( "Here is your parallelogram\n" );
        
    max=length;
    
        
        for (length=1; length<=max; length++) {
            
            for (width=1; width<=max; width++) 
            printf("* ", width);
            
    
      printf ("\n");
    }          
     
      system("PAUSE");
      return 0;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Use separate variables for the limits and the loop counters.
    You need a widthMax as well.

    Also, use some more braces in the for loops to make it very obvious what is going on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    16
    ok so i fiddled with it a little and got it to finally print the right stuff but i want it to after each line add a space i tried adding a space in the printf("\n"); that only applied it to the first line and then it stopped adding the space any pointers, ideas, etc...

    Code:
    /* Gary Seratzki G1770243
       COP 3223g 
       Programming Assignment #2
       9/28/07
       Program designed to output a parallelogram of stars */
       
    #include <stdio.h>
    
    int main () {
        int length, width, max, max1;
        
        
        //This will take in the input of the length
        printf("Enter the length of your parallelogram.\n");
        scanf("%d", &length);
    
        //This will take in the input of the width
        printf("Enter the width of your parallelogram.\n");
        scanf("%d", &width);
    
        //This will print out the parallelogram
        printf ( "Here is your parallelogram\n" );
    
    max=width;    
    max1=length;
    
        
        for (width=1; width<=max; width++) {
            for (length=1; length<=max1; length++)
            printf("*", width);
            
    
      printf("\n");
    }          
     
      system("PAUSE");
      return 0;
    }

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    9
    Quote Originally Posted by surfingbum18 View Post
    Ok so here the problem i just got into a beginning programming C class and my teacher kind of sucks and i would love some help....

    basically im writing a program that writes a parallelogram with a input of a width and a length...

    so basically if you want the length to be 5 and you want the width to be 4 the output would look like this

    *****
    *****
    *****
    *****
    (each row moves over to the right about 2 spaces everytime it goes to the next line)

    pretty simple im assuming... i pretty much understand everything on how to do everything but the for statement part or the while statement im currently trying to use the for statement because i think it is easiest to read... but for the life of me i cant get it to compile and after i input my 2 numbers print anything near to the right thing... if anyone could help me with the basic set up and a brief explanations i would absolutely love it... thanks so much

    surfingbum18
    Hi dear, in ur code i really don't understand how can u achieve ur desired parallelogram. Please see my code below to print a parellogram as u need. Hope, it will work out.....................
    Code:
    int main () {
        int length, width;
        int row, column;    
        
        //This will take in the input of the length
        printf("Enter the length of your parallelogram.\n");
        scanf("&#37;d", &length);
    
        //This will take in the input of the width
        printf("Enter the width of your parallelogram.\n");
        scanf("%d", &width);
    
        //This will print out the parallelogram
        printf ( "Here is your parallelogram\n" );
    
    for (row=1; row<=width; row++)
    {
       for ( column=1; column<=length; column++)
        {
           printf("*");
        }
        /* For printing new line */   
        printf("\n");
       /* for printing two blank speace in the starting of each new line */
        printf("  ");
      }

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Prashant, are you sure your code is correct? It looks to me like it would print:
    Code:
    *****
      *****
      *****
      *****
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Thinking about it, you have n rows, at each row you want to indent by 2 spaces more than the previsous row. So:
    Code:
    row spaces
      1    0
      2    2
      3    4
      4    6
    and so on, that's an arithmetic progression of spaces = (row-1) * 2
    To print them, you would need to print the spaces (you'll need a loop for that) and then the stars (loop for that too) and that needs to be done for every line (a bigger loop outside).

    Hope that makes sense?

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    9
    Quote Originally Posted by matsp View Post
    Prashant, are you sure your code is correct? It looks to me like it would print:
    Code:
    *****
      *****
      *****
      *****
    --
    Mats
    Ohhh.........i got it buddy..u are right......................

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    16
    awesome thanks for the help im still having a little issues figuring out printing the spaces but im sure after a little messing around with it ill get it... has anyone done activity diagrams for a problem like this? if anyone has i would love some help with setting that up... basically i got so far start program, prompt user for input, store input, then im lost on how to explain it in a activity diagram... thanks for the help

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    11
    hi sir can you please do this program for me..? i want only the code of this problem.. so here is the problem, "write a program that will compute for and display the sum of all numbers divisible by 3 from 1-1000." ill wait for your reply.. thank you...


    from jane

  12. #12
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by apriljane View Post
    hi sir can you please do this program for me..? i want only the code of this problem..
    No-one here is going to do that for you. If you need something written and don't want to do it yourself, you might want to check out www.rentacoder.com instead.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I will:
    Code:
    #include <stdio.h>
    
    #define THREE 0x0003
    #define TEN 0x000A
    
    int main()
    {
      int i;
      int s;
      char buffer[12] = { 0 };
      for(s = i = 0; i < 1000; i++, s += (i % THREE)?0:i) ;
      for(i = sizeof(buffer)-1; i >= 0 && s; --i, i[buffer]=(int)(&((char *)'0')[s%TEN]), s/=TEN);
      puts(&i[buffer]);
      return s;
    }
    Not as many comma operators as I'd like, really...
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Mar 2009
    Posts
    11
    im not a liar.. i just cant understand that cprogramming.. if you dont to help me.. its fine with me.. but dont say that im a liar coz im not...

  15. #15
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by apriljane View Post
    im not a liar.. i just cant understand that cprogramming.. if you dont to help me.. its fine with me.. but dont say that im a liar coz im not...
    No-one here called you a liar...

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging Issues
    By jl864405 in forum C Programming
    Replies: 6
    Last Post: 06-05-2009, 05:40 PM
  2. issues
    By OrAnGeWorX in forum C Programming
    Replies: 35
    Last Post: 11-19-2008, 12:18 AM
  3. Bitmap scroll issues
    By Gerread in forum Windows Programming
    Replies: 4
    Last Post: 05-14-2007, 05:18 AM
  4. Solution to culling issues?
    By VirtualAce in forum Game Programming
    Replies: 4
    Last Post: 03-14-2006, 06:59 PM
  5. hexdump issues
    By daluu in forum C Programming
    Replies: 2
    Last Post: 03-04-2003, 09:01 PM