#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0

    [HOMEWORK] Using Loops


    Write an application that displays the following patterns
    separately, one below the other. Use any loops you like to generate the patterns. All asterisks (*) should be
    printed by a single statement of the form
    System.out.print( '*' ); which causes the asterisks to
    print side by side. A statement of the form
    System.out.println(); can be used to move to the next
    line. A statement of the form System.out.print( ' ' ); can
    be used to display a space for the last two patterns.
    There should be no other output statements in the
    program. [Hint: The second pattern requires that each
    line begin with an appropriate number of blank spaces.]


    a.

    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    **********

    b.

    **********
    *********
    ********
    *******
    ******
    *****
    ****
    ***
    **
    *
  2. #2
  3. Headless Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,977
    Rep Power
    9647
    If anybody didn't catch that, OP tried to post
    Code:
    a. 
    
    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    **********
    
    b. 
    
    **********
     *********
      ********
       *******
        ******
         *****
          ****
           ***
            **
             *
    I'm going to assume you forgot to ask "Any hints as to how I do this?" and that you have every intention of writing the code by yourself.

    First part:
    There are 10 lines. How do you count from 1 to 10?
    Each line has X stars. How do you count from 1 to X?

    The second part is exactly the same except there's a third (yes, third) loop. It's a lot like the one used to print the *s.


    Write something, give it a shot, and if you have problems post what you wrote and a description of what's wrong.

    Comments on this post

    • Hugh of Borg agrees
    • Yawmark agrees
    • mrider agrees
    • Wetmelon agrees
    • EsoxRick agrees
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0
    Whoops, I posted something up at the top originally, i guess i pasted over it when i put in the assignment. Yes, I'm working on it myself and not expecting anyone to do it for me, just looking for pointers. For part A, I'm guessing that I want to recall the previous line somehow, just unsure of how to do it. I'm also completely lost on how to remove an asterisk from each line in B.

    Thanks in advance
  6. #4
  7. Headless Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,977
    Rep Power
    9647
    Originally Posted by airegin413
    For part A, I'm guessing that I want to recall the previous line somehow, just unsure of how to do it. I'm also completely lost on how to remove an asterisk from each line in B.
    You need to think about it differently. There aren't lines of asterisks - they're lines with some writing. That means you print some stuff, a newline, some more stuff, a newline, and so on.


    Part A:
    You have a bunch of asterisks with a scattering of newlines in between.
    The first line has a * then a \n. The second line has ** then a \n.
    If you count that way, the Xth line has X asterisks and a newline. That's two loops: one to count lines, one to count (and print) asterisks.

    Part B:
    You have a mix of spaces and asterisks, also with newlines in between.
    The first line has 1 space and 1 asterisk. The second line has 2 spaces and 2 asterisks. The Xth line has X spaces and X asterisks.
    It's much like part A except now you need another loop: alongside the one to count lines and to count asterisks, you have one to count spaces.


    For both parts that's a total of 5 loops.
    Yes, there are better ways of doing it, but that's not what the homework is about. It's trying to teach you how to think about this type of problem.

    Question: If you had to describe the output to somebody without pictures, how would you do it?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2010
    Posts
    4
    Rep Power
    0
    okay ive been working on this and im getting close; there is just one thing still confusing me. The output is correctly putting the number of spaces at the beginning of the line, but then it is repeating after each star. I've pasted the output code below. Any idea what I'm doing wrong?

    for (int i =0; i < 10; i++)
    {
    for (int j=10; j > i; j--)
    {
    for(int k=0; k<i+1; k++){
    System.out.print(" ");
    }

    System.out.print('*');

    }
    System.out.print('\n');
    }
  10. #6
  11. Headless Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,977
    Rep Power
    9647
    You nested all three loops. Right now you've told it that it should print a bunch of spaces for each one star rather than a number of spaces followed by a number of stars.

    You're very close. Try putting the spaces loop in a different place.

    Comments on this post

    • Waloower agrees

IMN logo majestic logo threadwatch logo seochat tools logo