problem displaying patterns correctly using for loops
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: problem displaying patterns correctly using for loops

  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.

  7. #7
    Join Date
    Feb 2004
    Posts
    31

    Re: problem displaying patterns correctly using for loops

    well i got c and d and im not sure how. I wish i knew how to implement algorithims better. Could you show me how your algorithim fits into d because i just dont see it? I just kept changing things around until it worked which is not the way to program, but the only way i know how.

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

  8. #8
    Join Date
    Feb 2005
    Posts
    6

    Re: problem displaying patterns correctly using for loops

    I believe you are trying to code with 'For' loops, a algorythm that prints a series of asterisks to form a pattern.

    In this equation there are two variables, as you well know, 'Asterisk' and 'Space'.

    These variables should be assigned, it will make programming more clearer.

    Therefore, during program initialization, declare -
    public int Star;
    public int Space;
    Star = 1;
    Space = 9;
    Now the For loop should be easy to write.

    For (x = 10; x > 0; x--)
    {
    if (Star < 11)
    {
    System.out.println("*");
    Star = Star++;
    For (x = Space; x > 0; x--)
    {
    System.out.println(" ");
    }
    Space = Space--;
    }
    I have not tested this code, but the basic idea is to keep note of how many stars are being printed with the if condition, the first For is the number of lines you wish to draw, I have chosen ten.

    Finally, we decrease the number of spaces per line run, and increase the number of stars. By switching these increases/decreases around, and also changing the Star and Space variable starting values, we can create different shapes.

    This is not the best way of doing this, however, I'm sure you can now understand at least ONE way to implement the advice in this thread

  9. #9
    Join Date
    Feb 2005
    Posts
    6

    Re: problem displaying patterns correctly using for loops

    Code:
    For (x = Line_No; x > 0; x--)
    {
          For (y = Star; y > 0; y--);
          {
            Print ("*");
          }
          Star = Star++;
          For (z = Space; z > 0; z--)
          {
              Print (" ");
          }
          Space = Space--;
    }
    Sorry, I cannot seem to code a For loop to a depth of 3 without causing severe overprinting of the text.

    It just does not seem to work that way. ??
    Last edited by Blaze; February 7th, 2005 at 06:53 PM.

  10. #10
    Join Date
    Feb 2004
    Location
    USA - Florida
    Posts
    729

    Re: problem displaying patterns correctly using for loops

    Quote Originally Posted by raged789
    I just kept changing things around until it worked which is not the way to program, but the only way i know how.
    Works for me whenever I'm lazy .

    Here's another way of thinking it (this is for C), each line requires a total of 10 characters to be printed. Each line requires a proportion of astericks and spaces that add up to 10. Example, the middle line has 5 astericks and 5 spaces, both combined to give a total of 10 charaters per line. The first line as 0 spaces and 10 astericks giving a total of 10 characters. The last line has 9 spaces and 1 asterick printed, giving a total of 10 characters. The tricky part, of course, is determing the proportions for each line, you're on the right track with the two loops inside the main loop, the loop boundary for the first loop has to print up to current iteration of the main loop, and the second loop has to finish from the current iteration of the main loop to 10 (or however many characters are required per line). I think if you coded your loops to increment rather then decrement, you might see the picture more clearly:
    Code:
    public static void main(String[] args)
    	{	for (int i = 0; i < 10; i++)
    		{	for (int j = 0; j < i; j++)
    				System.out.print(" ");
    			
    			for (int k = i; k < 10; k++)
    				System.out.print("*");
    			
    			System.out.println();
    		}
    	}
    Or another direct way of looking at it, the number of spaces that are required to be printed out is whatever the value of i is (when the program starts, i = 0, so 0 spaces are printed out, when i = 5, 5 spaces need to be printed out, when i = 9, 9 spaces need to be printed out).

    And Blaze, what's with: space = space--; ?
    Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html

  11. #11
    Join Date
    Feb 2005
    Posts
    6

    Re: problem displaying patterns correctly using for loops

    Sorry dude - I mean Space = Space - 1;

    I decided to use two variables called space and star to manipulate the number of asterisks created.

    I think doing it this way has more flexibility in future programming. Such as when wanting to print more than 10 stars, or a series of x, y points.

    I'm a little rusty when it comes to programming - I learned Basic back in the 80's and am more prone to old fashioned styles of code.

  12. #12
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: problem displaying patterns correctly using for loops

    Quote Originally Posted by Blaze
    Sorry dude - I mean Space = Space - 1;
    ...
    I'm a little rusty when it comes to programming - I learned Basic back in the 80's and am more prone to old fashioned styles of code.
    It would be helpful if you could use Java code and Java naming conventions when posting solutions here - alternatively post pseudocode, but please flag it clearly as pseudocode otherwise you risk further confusing an already confused novice...

    The trouble with programmers is that you can never tell what a programmer is doing until it's too late...
    Seymour Cray
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  13. #13
    Join Date
    Feb 2004
    Posts
    31

    Re: problem displaying patterns correctly using for loops

    Quote Originally Posted by cma
    Works for me whenever I'm lazy .

    Here's another way of thinking it (this is for C), each line requires a total of 10 characters to be printed. Each line requires a proportion of astericks and spaces that add up to 10. Example, the middle line has 5 astericks and 5 spaces, both combined to give a total of 10 charaters per line. The first line as 0 spaces and 10 astericks giving a total of 10 characters. The last line has 9 spaces and 1 asterick printed, giving a total of 10 characters. The tricky part, of course, is determing the proportions for each line, you're on the right track with the two loops inside the main loop, the loop boundary for the first loop has to print up to current iteration of the main loop, and the second loop has to finish from the current iteration of the main loop to 10 (or however many characters are required per line). I think if you coded your loops to increment rather then decrement, you might see the picture more clearly:
    Code:
    public static void main(String[] args)
    	{	for (int i = 0; i < 10; i++)
    		{	for (int j = 0; j < i; j++)
    				System.out.print(" ");
    			
    			for (int k = i; k < 10; k++)
    				System.out.print("*");
    			
    			System.out.println();
    		}
    	}
    Or another direct way of looking at it, the number of spaces that are required to be printed out is whatever the value of i is (when the program starts, i = 0, so 0 spaces are printed out, when i = 5, 5 spaces need to be printed out, when i = 9, 9 spaces need to be printed out).

    And Blaze, what's with: space = space--; ?

    thank you, could you put an algorithim for that example you gave me? I would like to see how it goes together.

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

    Re: problem displaying patterns correctly using for loops

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




    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.
    okay.. i'll implement my algorithm, with annotation. everything in italics is something i'm thinking, and i'll put a NUMBER indicating the order in which i thought about it, e.g. 3) I thought about this third..


    Code:
    5) here is where i write the main loop start. READ THE LAST THOUGHT in this code for more info
    this was my FIFTH thought, NOT my first one! start below!
    for(int mainLoopCount = 0; mainLoopCOunt < 10; mainLoopCount++){
    
    //x=10, y=1. 1) i need to make 2 variables for x and y
    int x=10;
    int y=1;
    
    //print X spaces 2) i need to repeatedly print some characters. i'll use a loop
    
    for(int count=0; count < x; count ++){  2a) that should print X number of spaces
      System.out.print(' ');
    }
    
    //print Y asterisk  3) same again, another repetitive thing, another loop
    for(int count=0; count < y; count ++){  3a) that should print Y number of asterisks
      System.out.print('*');
    }
    
    //decrement X, increment Y
    x--;
    y++;
    
    //go back to the start 4) well, the only way we can go back to a certain point in java
    is to either make a loop, or a recursive method call. as we've been told to use loops
    i guess that "go back to the start" means we are at the end of the loop, so i need to make 
    another loop at the top of the code, and end that loop here
    
    } //end of the main loop
    so you see, when implementing an algorithm, we just translate simple english into simple java. you can do everything in this code, as its just writing basic loops. try not to think about it all at once. do a little bit, and integrate it piece by piece
    "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

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center