Results 1 to 6 of 6
  1. #1
    Evildoer is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default Nested loops, and backwards patterns

    I have an assignment to to print wither 1 & 4 or 2 & 3 these patterns. I chose 1 & 4

    Java Code:
    #1:
    
    1
    
    1  2
    
    1  2  3
    
    1  2  3  4
    
    1  2  3  4  5
    
    1  2  3  4  5  6
    
    #2:
    
    1  2  3  4  5  6
    
    1  2  3  4  5
    
    1  2  3  4
    
    1  2  3
    
    1  2
    
    1
    
    #3:
    
                   1
    
                1  2
    
             1  2  3
    
          1  2  3  4
    
       1  2  3  4  5
    
    1  2  3  4  5  6
    
    #4:
    
    1  2  3  4  5  6
    
       1  2  3  4  5
    
          1  2  3  4
    
             1  2  3
    
                1  2
    
                   1
    Here is my code so far, and I can't for life of me get the last to print right. Please don't tell me the answer, help me understand what I am doing wrong.

    Java Code:
    // Print Pattern 1 title
    System.out.println("\n#1 \n");
            
    // Set Size of pattern
    int lines = 6;
            
    //Nested For loop
    for (int row = 1; row <= lines; row++){ 
            for (int col = 1; col <= row; col++) 
                System.out.print (col + "  " ); 
            System.out.println();
            System.out.println(); 
        } 
            
    //Print Pattern 4 title
    System.out.println("\n#4 \n");
            
    //Loops for Pattern 4
    for (int row = lines; row >= (lines - 6 + 1); row--){
         System.out.print(" ");
    }
    for (int col = 1; col <= lines; col++){
    System.out.print(col +  "  ");
    }
            System.out.println();
    }                 
    }
    Thanks!
    Last edited by Evildoer; 03-19-2012 at 01:24 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Nested loops, and backwards patterns

    Look at the desired output line by line. Then look at what is printed on a line and see what the relationship is between the number/position of the line and the number of spaces printed on the line and the number of numbers.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default Re: Nested loops, and backwards patterns

    I can see you understand the problem related to 4th pattern. You need sufficient number of blank spaces at the begining of row, where necesarry, and then correct number of numbers at the end.

    Let's take a look at the part where you want to print blank spaces:

    Java Code:
    //Loops for Pattern 4 
    for (int row = lines; row >= (lines - 6 + 1); row--){
          System.out.print(" "); 
    }
    Here you have variable row and in this case it's value is value of lines which is 6.

    Java Code:
    int row = lines;  // row = 6
    The next part will always give 1:

    Java Code:
    (lines - 6 + 1);  // (6 - 6 + 1) = 1
    So basicly, you have a for loop which will print blank spaces while counting down from 6 to 1. Result: six blank spaces in a row.

    For the second loop it does nothing else but print numbers from 1 to 6 with blank spaces between:

    Java Code:
    for (int col = 1; col <= lines; col++){ 
    System.out.print(col +  "  "); 
    }
    Everything is printed in one line since you used row to represent blank spaces instead of lines i.e. there is no for loop that counts and breaks lines.

    So let's remeber how it should looks like:

    Java Code:
    1 2 3 4 5 6
      1 2 3 4 5
        1 2 3 4
          1 2 3
            1 2
              1
    Can you notice this pattern: row 1 - blanks 0; row 2 - blanks 1; row 3 - blanks 2; row 4 - blanks 3 ...

    What you need here is one variable for counting lines (row),
    one for counting columns (col),
    and I suggest one for counting blank spaces (space).

    Your code could look like this:

    Java Code:
    for (each line)  // rows from 1 to 6
        for (each space necessary)  // hint: remember the pattern, loop space from 1 and if it is lower then
                                               // current row you have to print space
            print blank space
        for (rest number of columns)  // count how meny columns are left 
            print number accordingly
        print new line
    I suppose your requirement is to use only for loops so this is only one of possible sollutions.
    Last edited by milovan; 03-19-2012 at 05:59 AM.

  4. #4
    Nazarius is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Re: Nested loops, and backwards patterns

    Hello every one,

    I got the same problem as Evildoer, especialy number 4. Could anyone just give the answer? I had sit for more than two hours to solve it :(, but I guess I have to give up my self now..Thanks in advance!

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Nested loops, and backwards patterns

    I got the same problem as Evildoer, especialy number 4. Could anyone just give the answer?
    Seriously? First off, don't hijack someone else's thread. Secondly, we're not your personal code farm. If you expect to be handed answers as easy as this one for the rest of your programming career, give up now and stop wasting everyone's time.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Nested loops, and backwards patterns

    Enough with meaninglessly bringing this dead thread to the top of the listings. jou, please go through Guide For New Members and BB Code List - Java Programming Forum .

    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Nested Loops Help!
    By spendogw in forum New To Java
    Replies: 2
    Last Post: 03-16-2012, 05:20 AM
  2. Using Nested Loops
    By son012189 in forum New To Java
    Replies: 6
    Last Post: 10-02-2011, 06:30 AM
  3. Replies: 20
    Last Post: 03-22-2011, 05:02 AM
  4. nested for loops
    By Implode in forum New To Java
    Replies: 4
    Last Post: 09-01-2009, 09:47 AM
  5. Nested loops?
    By gabriel in forum New To Java
    Replies: 4
    Last Post: 08-06-2007, 05:51 PM

Posting Permissions

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