Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need Help get output  RSS feed

 
Maneesh Weerasooriya
Greenhorn
Posts: 3
Java Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to print like
10 20
20 10

can change between // //
without use new int
without change other lines


 
khadar valli
Greenhorn
Posts: 26
Java MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need to apply basic sum and subtraction here to swap the variables like
1. add both variables ans assign it to first variable. x=x+y;
2.subtract the second value from that total and assign it to second. y=x-y;
3. subtract the second value from the total again and assisgn to first . x=x-y;

Problem solved.
if x=10 and y=20 then
x=x+y----------> x= 10+20--->x=30
y=x-y----------> y=30-20 ---> y=10
x=x-y-----------> x=30-10 ----> x=20
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
khadar valli wrote:. . . Problem solved. . . .
Disagree. That solution is more of a party trick. The solution with exclusive or is definitely a party trick only.

Welcome to the Ranch I added code tags to your post; you should always use the tags. Doesn't it look better. What follows is a good object‑oriented solution:-…but I won't show you the workings of the IntSwapper class. You will have to write your own. If you search you will find there is a standard idiom for swapping two values like that, the same code can be used in Java┬«, C, C++ and C#.
 
Maneesh Weerasooriya
Greenhorn
Posts: 3
Java Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm new to here.Thanks for reply.
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a pleasure Have you found the standard method for swapping yet?
 
Maneesh Weerasooriya
Greenhorn
Posts: 3
Java Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no
 
Liutauras Vilda
Marshal
Posts: 4634
316
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Imagine you have 3 cups: red, green and blue.

Two cups are filled up with some content and one cup is empty:
Red with milk
Green with water
Blue is empty


What would you do in order to have milk in Green cup and water in Red?
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Weerasooriya wrote:no
I am surprised; I found it in a few seconds by searching the web.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The solution with exclusive or is definitely a party trick only.

Not sure I agree completely. For one thing it's pretty much optimally fast, and for a method like List.set() that needs to return a "previous" value, it's actually quite elegant IMO:But I certainly wouldn't make a habit of it.

Winston
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But that only works for integer primitives and booleans. The solution I am thinking of works for all types of value.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie. That solution is more of a party trick. The solution with exclusive or is definitely a party trick only.[/quote wrote:
But your solution violates the instructions that no new ints be created. Presumably this assignment was meant specifically to discover how XOR of something similar can be used. Your point, however, is well taken - one should never use clever tricks that can make the code difficult to maintain.
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:. . . your solution violates the instructions that no new ints be created. . . .
I didn't notice that bit. Agree with what you said about clever tricks, however.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!