posted 1 year ago
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:
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:
posted 1 year ago
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.
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.
Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.— Junilu
[How to Ask Questions] [How to Answer Questions]
posted 1 year ago
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?
I'm a beginner in java so I'm not sure what to change to make it an "8".
Any Suggestions for the code?
posted 1 year ago
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 1based index for your loops rather than the idiomatic Java 0based 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 1based indices. That is:
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 1based index for your loops rather than the idiomatic Java 0based 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 1based indices. That is:
Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.— Junilu
[How to Ask Questions] [How to Answer Questions]
posted 1 year ago
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.
Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.— Junilu
[How to Ask Questions] [How to Answer Questions]
mooooooo ..... tiny ad ....
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
