• Post Reply Bookmark Topic Watch Topic
  • New Topic

print java number pattern 2  RSS feed

 
T devillers
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

There another pattern which i'm trying to solve  . There is hardly any kind of help for such kind of patterns . Please guys kindly help .
My code for this pattern sucks . As i'm kind struck how to reverse columns alternatively .

Regards.

TS




Print Pattern

1
2  9
3  8  10
4  7  11  14
5  6  12  13 15   
  





 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the 1 supposed to be two columns off to the right? [edit]It isn't off to the right any more. Let's assume it is supposed to be above the 2.[/edit]
Do you have an algorithm for printing numbers boustrophedon like that? Start by working out what the values of all the numbers are, and a formula for calculating them.
 
T devillers
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
T devillers wrote:Hi guys,

There another pattern which i'm trying to solve  . There is hardly any kind of help for such kind of patterns . Please guys kindly help .
My code for this pattern sucks . As i'm kind struck how to reverse columns alternatively .

Regards.

TS




Print Pattern

           1
   2  9
   3  8  10
   4  7  11  14
   5  6  12  13 15   
  







Please note '1' is at row =1 and column =1 (left top corner above 2) . I don't know post preview is showing something else than my original postings . And where is edit post button ? Sorry i'm kind of new user here .
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Is the 1 supposed to be two columns off to the right?

No I don't think it is as the OP had the same alignment issue in an earlier thread. Whilst you were typing your post I fixed the alignment problem.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
T devillers wrote:
I don't know post preview is showing something else than my original postings

The display problem is because you have a mix of spaces and tabs before the numbers in each row. If you eliminate all the spaces and tabs it displays correctly. And yes I had the same problem with the preview showing something different to the actual post.
 
Jeremy Hutto
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first thing you need to do is fix the code so that the output is

1
2  6
3  7  10
4  8  11  13
5  9  12  14 15   

let me know when you finished it so I can tell you the second step
 
T devillers
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy Hutto wrote:The first thing you need to do is fix the code so that the output is

Here is the code for program you have asked .



but i have done little code improvement for pattern 2 as well

 
Jeremy Hutto
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So now you need to find a way to alternate the columns.

What variable in your program do you think you can use that will be able to check weather or not that pattern is in a new column?
 
Jeremy Hutto
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see that you already have a way to check for even or odd, but for some reason I'm getting a lot of syntax errors when I try to use the code
 
Jeremy Hutto
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I fixed all of the syntax errors and I'm not able to see that numberPattern1 is working great, numberPattern2 doesn't seem to be doing the trick
The problem is because you didn't use numberPattern1 as the base for numberPattern2

You need to keep this line of code



It's very important.

I want you to add the even or odd checker to numberPattern1

it should look something like this when its done.

 
Jeremy Hutto
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made an error in the code above, I was confused because I solved this problem using the first line as even.

In this code the first line is odd, so the even line being the second is what needs to be changed to reverse the order

here's updated code on what you need to add to numberPattern1 to get the desired results.
 
T devillers
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy Hutto wrote:I made an error in the code above, I was confused because I solved this problem using the first line as even.

In this code the first line is odd, so the even line being the second is what needs to be changed to reverse the order

here's updated code on what you need to add to numberPattern1 to get the desired results.


Still not able to apply logic . I have wasted 3 hours on this . 
 
Jeremy Hutto
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want you to see if you can get the program to output something like this


Printing Number Pattern
k:1 j:1 i:1   
k:2 j:1 i:2----l: 6 j: 2 i: 2   
k:3 j:1 i:3----l: 7 j: 2 i: 3----k:10 j:3 i:3   
k:4 j:1 i:4----l: 8 j: 2 i: 4----k:11 j:3 i:4----l: 13 j: 4 i: 4   
k:5 j:1 i:5----l: 9 j: 2 i: 5----k:12 j:3 i:5----l: 14 j: 4 i: 5----k:15 j:5 i:5   

also use this to check if the number is odd or even.

if( (j & 1)!=0)
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy Hutto wrote:. . .  syntax errors when I try to use the code
You have since realised that the == and != operators have a precedence higher than &, so you need an extra pair of (). You need to evaluate & before ==/!=. Otherwise you get & with the left operand an integer and the right operand a boolean, and those are incorrect types.
 
Jeremy Hutto
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was also that there were a bunch of space characters that eclipse didn't recognize.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!