• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Print Pattern Unseccesfull

 
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The outcome should be this:
             1            
           1 2 1              
         1 2 3 2 1              
       1 2 3 4 3 2 1                    
     1 2 3 4 5 4 3 2 1                    
   1 2 3 4 5 6 5 4 3 2 1                    
 1 2 3 4 5 6 7 6 5 4 3 2 1                
1 2 3 4 5 6 7 8 7 6 5 4 3 2 1

But the eight turned into a zero:

             1            
           1 2 1              
         1 2 3 2 1              
       1 2 3 4 3 2 1                    
     1 2 3 4 5 4 3 2 1                    
   1 2 3 4 5 6 5 4 3 2 1                    
 1 2 3 4 5 6 7 6 5 4 3 2 1                
1 2 3 4 5 6 7 0 7 6 5 4 3 2 1

Here's the code:


 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are using the remainder operator at line 18. And since size equals eight, and the range of possible remainders, when something is divided by eight, is from zero to seven, it is not possible to get an eight.

Henry
 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't really know what to change it to...
Can you give something I could change it to?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Normally when you want the last digit of a number N, you just use the remainder when you divide that number by 10.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lindsey,

To find a pattern, it's often easier if you start with a few concrete examples first. Then you examine these examples and look for both similarities and differences. Then you try to generalize these similarities and differences in such a way that any other example will fit into that generalization. That's how you find a pattern.  The basic rule of patterns is that you need at least three concrete examples.

So, going through your figure, take a few lines (maybe the first 3 or 4 lines) and try to answer these questions:

1. How many spaces do you have to print before printing the first number?
2. How many numbers do you have to print? (count)
3. What number do you start with? (lower bound)
4. What number do you end with? (upper bound)

I'll answer these for the first three lines:

Line 1:
1. Print 7 spaces, then the number 1
2. 1 number printed
3. start with 1
4. end with 1

Line 2:
1. Print 6 spaces, then the number 1
2. 3 numbers printed
3. start with 1
4. end with 2
5. go back down to 1

Line 3:
1. Print 5 spaces, then the number 1
2. 5 numbers printed
3. start with 1
4. end with 3
5. go back down to 1

Continue this exercise until you see the pattern emerging.  You'll soon realize that each set of questions/answers represents one iteration of an outer loop.  You should also be able to find where one or more inner loops will be represented. Look for relationships between successive lines and repetitive things. If something is consistently increasing, how much did it increase by from the previous line?  If it's consistently decreasing, how much did it decrease by? This will give you an idea of what the increment size will be for your loop variables.

This is really just an exercise in algorithm development.  An algorithm is a logical and systematic way of solving a problem and your task is to figure out what that is.
 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much everyone.
I'm a beginner in java so I'm not sure what to change to make it an "8".
Any Suggestions for the code?
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you could write a kludge and force your program to do what you need it to do, or you could do it right, step back, and rethink your approach and come up with the correct algorithm. I will try to give you hints as to how to do the latter.

As you have seen, the formulae you are using, (row + col) % size and (row + size) % col, work for all but one case. This is unfortunate because it leads you to believe that you're almost there when in fact what happened is that you stumbled upon something that's fundamentally wrong but actually works most of the time.

Another unfortunate thing about this problem is that it's better solved using a 1-based index for your loops rather than the idiomatic Java 0-based index.

To help you out, because I know it can get pretty frustrating to keep banging your head against a wall after a while, I'll give you the outline of my working solution.


So, you need to figure out how to first indent the line, then print consecutive numbers up, then print consecutive numbers down.  Again, the biggest hint I can give you is that it's easier to solve this with 1-based indices. That is:

 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you look closely at the PowerOf2Triangle solution you came up with, it's actually on the right track and basically the same solution pattern can be used for this problem.  Look at the comments I inserted in your code.

reply
    Bookmark Topic Watch Topic
  • New Topic