Results 1 to 20 of 20
Like Tree1Likes
  • 1 Post By jim829

Thread: Help with patterns using nested loops

  1. #1
    TheRockIng is offline Member
    Join Date
    Feb 2015
    Posts
    19
    Rep Power
    0

    Default Help with patterns using nested loops

    I have to make a pattern that is of a diamond shape such as this:

    Java Code:
                 1
               1 2 3
            1 2 3 4 5
               1 2 3
                 1
    I wrote this ( sorry if for the variable name, this is just a small part of my program and I'm trying to get the pattern right so I didn't mind my variables at the moment):

    Java Code:
    public class NewFile{
    
     public static void main(String []args){
       int k = 0;
       for (int i=1 ; i<=5 ; i++)
        {
         { for (int h=2 ; h >= i ; h--)
               System.out.print(" ");
          for (int j=1  ; j<= i + k ; j++)
             System.out.print(j);
          for (int w=2 ; w>= i; w--)
             System.out.print(" ");
    
             }
             k++;
             System.out.println();}
      }
    }
    My output is the following:

    Java Code:
         1  
        123 
       12345
      1234567
     123456789
    I realize I should divide the code into a lower and upper triangle using two loops. However, I don't know how to break the first part. I did find the "trend" but I don't see how to implement it.
    Last edited by TheRockIng; 03-23-2015 at 05:14 PM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Help with patterns using nested loops

    Is that diamond example you give really correct? It seems like you're missing two rows.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    TheRockIng is offline Member
    Join Date
    Feb 2015
    Posts
    19
    Rep Power
    0

    Default Re: Help with patterns using nested loops

    yeah it is the right pattern, it skips ths 1234. That was what it asked me to do :)

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Help with patterns using nested loops

    Are you permitted to use spaces? Your attempt does not include them. And imo, it does not make sense that you skip 1 2 3 4. Of course, skipping 12, and then 1234 makes sense for fixed-width fonts.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    TheRockIng is offline Member
    Join Date
    Feb 2015
    Posts
    19
    Rep Power
    0

    Default Re: Help with patterns using nested loops

    you are right! i have to skip 1 2 ( thank you for noticing, I copied it wrong).

    Also what do you mean by using spaces? you mean in my s.o.p?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Help with patterns using nested loops

    Quote Originally Posted by jim829 View Post
    And imo, it does not make sense that you skip 1 2 3 4.
    That surprises me; did you expect any regularity in the pattern given a few previous samples? That'd be incomplete induction and never expect that to be always true ... ;-)

    kind regards,

    Jox (<-- unexpected?)
    Build a wall around Donald Trump; I'll pay for it.

  7. #7
    Juan2114 is offline Member
    Join Date
    Feb 2015
    Posts
    5
    Rep Power
    0

    Default Re: Help with patterns using nested loops

    This does the diamon pattern, as you can see is quite easy, no need to overcomplicate :)

    Java Code:
    /**
     * Created with eclipse 23/03/2015 11:13:56 a. m.
     * @Author Juan Sebastian Quiceno <Juan.2114@hotmail.com>
     */
    public class DiamonShape {
    
    	public static void main(String[] args) {
    		boolean increasing = true;
    		for (int i = 0; true;) {
    			if (i >= 4)
    				increasing = false;
    			
    			if (i <= 0 && !increasing) 
    				break;
    			
    			if (increasing)
    				i++;
    			else 
    				i --;
    			
    			String space = "*****";
    			space = space.substring(0, space.length() - i);
    			
    			System.out.print(space);
    			
    			for (int number = 1; number <= i; number++) {
    				System.out.print(number + " ");
    			}
    			
    			System.out.println();
    		}
    	}
    
    }

  8. #8
    TheRockIng is offline Member
    Join Date
    Feb 2015
    Posts
    19
    Rep Power
    0

    Default Re: Help with patterns using nested loops

    I don't think I paid attention to it because I was writing it down on a piece of paper to find the pattern. However, if anyone can answer, my question still stands.

    How do I break the code at the line that I want?

    For instance, in the output that I showed up there ( my own), it was done with a fix variable. So how would I stop it if I used "n" as a variable? Would I just need to use a "break" statement? there is a pattern that I noticed but it seems hard to code it ( at least for me) in my code:

    For 2 rows ( I.e: 1 then 123), it reaches the number 3 for i=2. For 3 rows, reaches the number 5 for i=3. And for 4 rows, the number 7 will occur in i=4. So there is a trend but it seems hard to code ( at least i don't see how I could )

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Help with patterns using nested loops

    Quote Originally Posted by JosAH View Post
    That surprises me; did you expect any regularity in the pattern given a few previous samples? That'd be incomplete induction and never expect that to be always true ... ;-)

    kind regards,

    Jox (<-- unexpected?)
    Student to professor: What is the next number in the sequence 1,3,5,7,9?
    Professor: 192839
    Student: What sequence is that!?
    Professor: The one that has 1,3,5,7,9, followed by 192839.

    Regards,
    Jim
    DarrylBurke likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Help with patterns using nested loops

    Spoon feeding solutions is a big taboo on this forum. How will the OP learn to do this if you simply provide the code?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Help with patterns using nested loops

    Quote Originally Posted by TheRockIng View Post
    you are right! i have to skip 1 2 ( thank you for noticing, I copied it wrong).

    Also what do you mean by using spaces? you mean in my s.o.p?
    By spaces I meant 1 2 3 instead of 123.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Help with patterns using nested loops

    Its clear what you want to achieve, but this is clearly a homework assignment and it is not yet clear what demands your code needs to meet. I guess you are supposed to do this by using a for-loop, but is there perhaps a limit on the number of for-loops that you can use? Are you allowed / do you know how to use methods? Even if it is only static methods?

    Also, can you use JDK classes in your code, such as the Math class?
    Last edited by gimbal2; 03-23-2015 at 05:57 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    TheRockIng is offline Member
    Join Date
    Feb 2015
    Posts
    19
    Rep Power
    0

    Default Re: Help with patterns using nested loops

    Well it is a homework assignment ( good eyes for it I guess).

    1) I don't need to be spoon fed the solution, I can figure out stuff myself, I just want pointers because I am really stuck ( I got all my other patterns and this one has been going on for about 2 days ).

    2) I can use the amount of loops I want. I'd rather optimize what I use because, what is the point of learning how to code if I do it using so many useless command ( which I am doing, I am aware of! I try to get the loop right and see what can be changed).

    3) The last specification I have is that the person has to enter a value for the number of rows ( 1-9)

    4) Lastly the pattern changes whether or not the entered value for the number of rows is odd or even.

    I think I covered it all. :)

  14. #14
    TheRockIng is offline Member
    Join Date
    Feb 2015
    Posts
    19
    Rep Power
    0

    Default Re: Help with patterns using nested loops

    Quote Originally Posted by jim829 View Post
    By spaces I meant 1 2 3 instead of 123.

    Regards,
    Jim
    Doesn't specify if I can use or not. And I'm guessing you asked that because of the initial message. I think I just put spaces to be more clear. My bad!

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Help with patterns using nested loops

    Quote Originally Posted by TheRockIng View Post
    Well it is a homework assignment ( good eyes for it I guess).

    1) I don't need to be spoon fed the solution, I can figure out stuff myself, I just want pointers because I am really stuck ( I got all my other patterns and this one has been going on for about 2 days ).
    Excellent attitude!
    Quote Originally Posted by TheRockIng View Post
    2) I can use the amount of loops I want. I'd rather optimize what I use because, what is the point of learning how to code if I do it using so many useless command ( which I am doing, I am aware of! I try to get the loop right and see what can be changed).
    Don't worry about optimization or efficient code. That comes with experience. Just focus on getting it done.

    Quote Originally Posted by TheRockIng View Post
    4) Lastly the pattern changes whether or not the entered value for the number of rows is odd or even.
    Can't help you there since we don't know what the pattern is. But we can help with logic and syntax mistakes.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  16. #16
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Help with patterns using nested loops

    Indeed, so what would the diamond look like if you inputted 4 as the "size"? I'm guessing like this:

    Java Code:
       1
      1 2
     1 2 3
    1 2 3 4
     1 2 3
      1 2
       1
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  17. #17
    TheRockIng is offline Member
    Join Date
    Feb 2015
    Posts
    19
    Rep Power
    0

    Default Re: Help with patterns using nested loops

    hahaha no!

    it would look like this
    Java Code:
            1
           123
           123
            1

  18. #18
    TheRockIng is offline Member
    Join Date
    Feb 2015
    Posts
    19
    Rep Power
    0

    Default Re: Help with patterns using nested loops

    So if anyone is still willing to help after the topic diverged so much, I got this for a fix input of 5:
    Java Code:
     
    public class PatternChose {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		int k = 0, h=2, c=0;
    	       for (int i=1 ; i<=5 ; i++)
    	        {
    	         { for (int u=2 ; u >= i ; u--)
    	               System.out.print(" ");
    	                h--;
    	          for (int j=1  ; j<= i + k ; j++)
    	             System.out.print(j);
    	          for (int w=2 ; w>= i; w--)
    	             System.out.print(" ");
    	             
    	             }
    	             k++;
    	             System.out.println();
    	             if (h<0)
    	                 break;}
                  for (int i =2 ; i>=1 ; i--){
                	  { for (int l=3 ; l>+i ; l--)
                		  System.out.print(" ");
                	     for (int m= 0 ; m <=i - c ; m++)
                	    	 System.out.print( m + 1);
                	  }
                	  c++;
                	  System.out.println();
                	  
                  }
    	}
    
    }
    which gives you an output of
    Java Code:
      1
     123
    12345
     123
      1
    However, when I decide to let the user decide the number of row, I get myself stuck with the following code:

    Java Code:
    import java.util.Scanner;
    
    
    public class PatternChose {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		int k = 0, h=2, c=0, row=1;
    		 Scanner userInput = new Scanner(System.in);
    	     System.out.println("Please input your value");
    	    	 int newUserInput = userInput.nextInt();
    	       for (int i=1 ; i<=newUserInput ; i++)
    	        {
    	         { for (int u=newUserInput - row ; u >= i ; u--)
    	               System.out.print(" ");
    	                h--;
    	          for (int j=1  ; j<= i + k ; j++)
    	             System.out.print(j);
    	          for (int w=2 ; w>= i; w--)
    	             System.out.print(" ");
    	             
    	             }
    	             k++;
    	             System.out.println();
    	             if (h<0)
    	                 break;}
                  for (int i =2 ; i>=1 ; i--){
                	  { for (int l=3 ; l>+i ; l--)
                		  System.out.print(" ");
                	     for (int m= 0 ; m <=i - c ; m++)
                	    	 System.out.print( m + 1);
                	  }
                	  c++;
                	  row++;
                	  System.out.println();
                	  
                  }
    	}
    
    }
    What happened is that for the fix variable 5, I decided to let the loop be dictated by the spaces to the left of the characters and to break the sequence when that space reached 0 to then create a lower triangle. However, I can't seem to find a sequence for that pattern if I use a variable "n" as it will just approach infinity with time. any suggestion?
    Last edited by TheRockIng; 03-23-2015 at 09:06 PM.

  19. #19
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Help with patterns using nested loops

    Quote Originally Posted by TheRockIng View Post
    So if anyone is still willing to help after the topic diverged so much
    That's an odd thing to say. It never diverged, it was an epic struggle to get you to provide details necessary to be able to actually help you.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  20. #20
    TheRockIng is offline Member
    Join Date
    Feb 2015
    Posts
    19
    Rep Power
    0

    Default Re: Help with patterns using nested loops

    well epic struggle is a bit extensive to say. To be fair there was a few posts about whether or not what someone asked was useful + 1 person just writing a code that didn't help at all, but anyway.

Similar Threads

  1. Nested FOR loops
    By wishmaster in forum New To Java
    Replies: 1
    Last Post: 04-21-2014, 07:56 PM
  2. Display Patterns use Nested Loops
    By LittleJava in forum New To Java
    Replies: 15
    Last Post: 02-16-2013, 12:58 PM
  3. Nested loops, and backwards patterns
    By Evildoer in forum New To Java
    Replies: 5
    Last Post: 11-24-2012, 07:38 AM
  4. Using Nested Loops
    By son012189 in forum New To Java
    Replies: 6
    Last Post: 10-02-2011, 05:30 AM
  5. Replies: 20
    Last Post: 03-22-2011, 04:02 AM

Tags for this Thread

Posting Permissions

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