Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Mar 2010
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Drawing figure using nested for loops

    Write a Java program in a class named SlashFigure that produces the following output. Use nested for loops to capture the structure of the figure.

    !!!!!!!!!!!!!!!!!!!!!!
    \\!!!!!!!!!!!!!!!!!!//
    \\\\!!!!!!!!!!!!!!////
    \\\\\\!!!!!!!!!!//////
    \\\\\\\\!!!!!!////////
    \\\\\\\\\\!!//////////


    Could anyone help me out with this? Thank you.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    What's the code you currently have?

  • #3
    New Coder
    Join Date
    Mar 2010
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    What's the code you currently have?
    Code:
    for (int i = 0; i < 6; i++) {
     for (int j = 0; j < i*2; j++) {
      System.out.print("\\");
     }
     for (int j = 0; j < 22 - (i*2); j++) {
      System.out.print("!");
     }
     for (int j = 0; j < i*2; j++) {
      System.out.print("/");
     }
     System.out.print("");
    }

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Okay, not bad. First replace that last out call with System.out.println();. All you need is a linefeed there, and then it'll place each iteration on a line of its own.

    The reduction is still off. Its the counter though, given what you have you need to subtract 22 - (i * 4) since you reduce by four during each iteration (two from the left, and two from the right). That should give you the correct result.

    A future modification you could consider is handling this mathematically instead. So instead I want to filter down to nothing in only 4 rows instead of 6 rows. How many would I have to subtract per line in order to get to this result? And same idea with columns, I want to show 50 now and filter it down using only 5, so how many would I subtract from there. And so forth. So keep that in mind for a potential future modification for it.

  • Users who have thanked Fou-Lu for this post:

    itsalltech (07-11-2012)

  • #5
    New Coder
    Join Date
    Mar 2010
    Posts
    22
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you! It worked! We've been given the next assignment, which asks us to modify the code. Could you help with this too? Thank you!

    Modify the SlashFigure program from the previous exercise to produce a new program SlashFigure2 that uses a global constant for the figure's height. The previous output used a constant height of 6. Here is the output for a constant height of 4:

    size 4

    !!!!!!!!!!!!!!
    \\!!!!!!!!!!//
    \\\\!!!!!!////
    \\\\\\!!//////

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    This is precisely what I was referring to for filtering down mathematically.

    Calculate the difference between the two resulting diagrams. Take note of how many !'s are in the first and last line, and find the common ratio to that of the number of !'s in the last line. That can be reversed to calculate how many are required for the first line (and the replacement value for 22).


  •  

    Posting Permissions

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