• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with number arrangement :(  RSS feed

 
Amit Raj Ps
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am trying to execute a program with the following number arrangement.

3 2 1
3 2
3

i have used "i" for rows and j for columns.



The output i got is

D:\Amit\Java\New folder>javac Demo.java

D:\Amit\Java\New folder>java Demo
321
21
  1

i spoke to my trainer regarding the same, and he told me to swap j with space and vice versa.

}

The new output i got is:

D:\Amit\Java\New folder>javac Demo.java

D:\Amit\Java\New folder>java Demo

3
32

The second code doesn't make much of a sense, since i have used the if condition to print a integer when i+j<=4,  or else use space.

Would really appreciate your help, thank you.

 
Omkar Shetkar
Ranch Hand
Posts: 100
2
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inner for loop needs to be checked.

It is more to do with logic than Java.



Of course, you can think of how to remove extra space appended at the end of each line. 
 
Amit Raj Ps
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Omkar Shetkar wrote:Inner for loop needs to be checked.

It is more to do with logic than Java.



Of course, you can think of how to remove extra space appended at the end of each line. 




Thanks for the reply, can you please explain the meaning of " for (int j = 3; j >= i; j--) " and S.O.P(j + " ") , there are few program executed in my class with j>=i , having hard time understanding the logic.

I have asked couple of my friends , to make me understand the j>=i , all failed to make me understand the logic
 
Omkar Shetkar
Ranch Hand
Posts: 100
2
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To understand the meaning of this program, better take pen and paper.
Write down the control flow of the program.

For eg,
------------------
i                  j
------------------
1                 3
                   2
                   1
                       <-- Now j >=i is TRUE, hence skips inner loops
2                 3
                   2
                      <-- Now j >=i is TRUE, hence skips inner loops
3                 3
                      <-- Now j >=i is TRUE, hence skips inner loops
                 


Console
-----------
3 2 1
3 2
3


This way you can understand a computer program and its logic.

 
Amit Raj Ps
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Omkar Shetkar wrote:To understand the meaning of this program, better take pen and paper.
Write down the control flow of the program.

For eg,
------------------
i                  j
------------------
1                 3
                   2
                   1
                       <-- Now j >=i is TRUE, hence skips inner loops
2                 3
                   2
                      <-- Now j >=i is TRUE, hence skips inner loops
3                 3
                      <-- Now j >=i is TRUE, hence skips inner loops
                 


Console
-----------
3 2 1
3 2
3


This way you can understand a computer program and its logic.



WoW thanks a lot Omkar, you made it look so easy. Now i know what exactly it means ( kinda slow learner) .
However, when i tried to execute the code which you have given, it worked like a charm. But when i tried to give spacing, i hit a road block again. Can you tell me what am i doing wrong. And sorry for being such a pain



this is the output i am getting

D:\Amit\Java\New folder>java Demo
3 2 1
2
 
Omkar Shetkar
Ranch Hand
Posts: 100
2
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say write the logic again on pen and paper to figure out the issue.
This will surely will give you a clue.
To give you some hint, you want to have SPACE only when j is less than i.



  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!