CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Hybrid View

  1. #1
    Join Date
    Feb 2004
    Posts
    31

    problem displaying patterns correctly using for loops

    problem is i need to write 4 methods to display 4 different designs.

    -all asterisks need to be printed by a single statement:
    Code:
    System.out.print('*')
    - the statement:
    Code:
    System.out.println()
    is used to move to the next line

    -
    Code:
    System.out.print(' ')
    can be used for the last 2 patterns which is what im having trouble with.

    -cant be any other output statements in the program. The last 2 patterns require that each line begin with an appropriate # of blank spaces.

    these are the designs:
    Code:
    a. 
    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    **********
    
    b.
    
    **********
    *********
    ********
    *******
    ******
    *****
    ****
    ***
    **
    *
    
    c.
    **********
     *********
      ********
       *******
        ******
         *****
          ****
           ***
            **
             *
    
    d.
             *
            **
           ***
          ****
         *****
        ******
       *******
      ********
     *********
    **********

    I have a and b, just cant get c and d. here is my code.

    Code:
    public class Assignment1
    {
    
       public static void methodA ()
       {
               for (int a=11; a>0; a--)
                  {
                      for (int b=a; b<11; b++)
                          System.out.print("*");
                          System.out.println();
                   }
    
                   
               System.out.println("");
       }
    
       public static void methodB ()
       {
            for (int z=1; z<11; z++)
                  {
                      for (int x=z; x<11; x++)
                          System.out.print("*");
                          System.out.println();
                   }                                                                                               
       }
       public static void methodC ()
       {
          
    
    
       }
    
       public static void methodD ()
       {
         
         
         
    
       }
    
       public static void main (String[] args)
       {
          Assignment1.methodA();
          Assignment1.methodB();
          Assignment1.methodC();
          Assignment1.methodD();
       }
    
    }
    any help with c and d would be great. I keep getting infinite loops
    Attached Files Attached Files
    Last edited by cjard; February 4th, 2005 at 04:45 AM. Reason: c and d dont appear correctly because of the way html works.. putting it in a php or code block preserves the leading spaces (as if it were code)

  2. #2
    Join Date
    Jan 2004
    Posts
    131

    Re: problem displaying patterns correctly using for loops

    For c, think about how many spaces are at the beginning of the line... and what your current iterative position in the loop is... for example, iteration 0 (the first one) has 0 spaces at the front. Iteration 1 has... well, you get the idea.

    Another hint would be to think: "two loops within a loop." One printing spaces, the other printing asterisks.

    I hope that helps!
    Power Macintosh G4/500 PCI
    OS X 10.5.4 • 1024MB RAM • 120GB x 3 • Pioneer DVR-111D CD-RW/DVD-RW • Radeon 7000 PCI x 2, dual 17" Displays

    http://www.jeffhoppe.com

  3. #3
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: problem displaying patterns correctly using for loops

    here's an algorithm for C:

    x=10, y=1
    print X spaces
    print Y asterisk
    decrement X, increment Y
    go back to the start


    you should be able to work out D.

    you CAN do this either with 2 loops inside a loop, or with just one loop inside a loop. using one loop is harder, as it requires an IF test. the algorithm for that would be:
    "when printing character number N, if N is greater than a certain number M, then we print asterisks, otherwise we print spaces"
    and naturally, you decrease M each time to your program switches from spaces to asterisks one character sooner..


    cat.skinWays() > 1
    Last edited by cjard; February 4th, 2005 at 04:52 AM.
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  4. #4
    Join Date
    Feb 2004
    Posts
    31

    Re: problem displaying patterns correctly using for loops

    thanks for the help..especially the algorythim. However, why would i want decrement X (space) when i need more spaces as it goes down the page?

    Anyway, here is what i got for "c." and it doesnt work. Im not sure what i did wrong. Output looks like a rectangle.

    Code:
       public static void methodC ()
       {
          for (int c=1; c<11; c++)
          {
              for(int d=10; d>1; d--)
              System.out.print(' ');
              
               for (int e=1; e<11; e++)
               System.out.print("*");
               System.out.println();
            
            
            }
    
    
       }

    thanks for fixing my first post cjard.

  5. #5
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104

    Re: problem displaying patterns correctly using for loops

    actually, i gave you a bit of misinformation..

    the algorithm i gave was for D, not C.

    go back, read about it and think about it.. if you had read and thought a lot about it, you'd have seen my mistake in writing C, instead of D


    also, as for why it doesnt work.. well, your code DOESNT implement my algorithm.
    to prove it to yourself,make my algorithm into comments:

    //x=10, y=1
    //print X spaces
    //print Y asterisk
    //decrement X, increment Y
    //go back to the start


    now you try to put this algorithm into the code you wrote! the comment should go above the line of code that carries out the step in the algorithm!

    remember that even real advanced programmers write algorithms in ENGLISH before they translate to java.. you grew up thinking in english, and that hasnt changed. you cant speak java yuet, so dont try to think in it. If you speak french (for example) think about HOW you speak it; you build the sentence you want to say in your head in ENGLISH, then you translate to FRENCH and speak it. your programming should be no different
    Last edited by cjard; February 6th, 2005 at 04:34 PM.
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  6. #6
    Join Date
    Feb 2004
    Posts
    31

    Re: problem displaying patterns correctly using for loops

    yea i did figure that was for d, but didnt sat exactly that cause i figured i was wrong. That is why i said:

    However, why would i want decrement X (space) when i need more spaces as it goes down the page?

    anyway, i always try to use algorithms, it's just they never seem to help me.

    I just dont understand them right i guess cause this is how i see it.
    Code:
     public static void methodC ()
       {
          //x=10, y=1
         int x=10;
         int y=1;
    //print X spaces
       System.out.print(' ');
    //print Y asterisk
        System.out.print('*');
    //decrement X, increment Y
      x--;
      y++;
    //go back to the start
      ????
       }
    see and there arent even any for loops in there. i have no clue. Thank you for helping.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured