Results 1 to 10 of 10
  1. #1
    jeffrey is offline Member
    Join Date
    Jul 2009
    Posts
    7
    Rep Power
    0

    Talking how do you do this?

    excuse me guys.
    im kinda new to java and trying to learn.
    i find it likely nice to know something about this.
    so a friend of mine tried me and asked me to make this.

    "a program that exchanges the values of 4 variables: a, b, c, d. The output must be:
    the new value of a is the previous value of d,
    the new value of b is the previous value of a,
    the new value of c is the previous value of b, and,
    the new value of d is the previous value of c.

    Use only 4 variables in your program to come up with the answer. "

  2. #2
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Singly Linked List

    Hi Jeffrey.

    I drawed the references on a page and it looks like a cyclic list of pointers between 4 objects. So I'd use a circular singly linked list, that is, a singly linked list such that the next element of tail is the head. Add the numbers to this list. Then, by traversing the list from the second position from the head, you will rotate the list by one element.

    I don't know if you would consider this list as the 5th variable or not. Anyways, it should work. Welcome to Java Forums. ;)
    Last edited by tim; 07-07-2009 at 10:09 AM.
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  3. #3
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default

    I can give you way to solve it : if you want to switch between A and B , but you can use only two variable (A and B) then you solve it like that: (assuming A,B numbers, real or integer)
    ( let say A=10, B=300)
    A=A+B (A=310)
    B=A-B (B=10)
    A=A-B (A=300)

    Now expend it for four variable . Good luck

  4. #4
    jeffrey is offline Member
    Join Date
    Jul 2009
    Posts
    7
    Rep Power
    0

    Default

    i see.
    now i understand.
    thx,
    ^_^

  5. #5
    IEEE is offline Member
    Join Date
    Jul 2009
    Posts
    2
    Rep Power
    0

    Default

    hey that can be simply achieve by following sets of assingment
    a=d;
    b=a;
    c=b;
    d=c;

  6. #6
    itaipee is offline Member
    Join Date
    Dec 2008
    Posts
    99
    Rep Power
    0

    Default

    a=d;
    b=a;
    c=b;
    d=c;
    nice every variable get the value of "d"

  7. #7
    mann is offline Member
    Join Date
    Jul 2009
    Posts
    4
    Rep Power
    0

    Default

    why didnt you take a paper and draw as tim did ?

  8. #8
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Hey guys. :)
    Quote Originally Posted by nancy0903 View Post
    jave, so hard
    Java isn't hard really. If you have a nice thick book, all the time in the world and a passion for it, then it's easy!
    Quote Originally Posted by mann View Post
    why didnt you take a paper and draw as tim did ?
    I agree. A piece of paper may be low tech, but it goes a long way. ;) I like itaipee's solution the most. Mine's too, computer sciency. :p
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  9. #9
    javamadd is offline Member
    Join Date
    Jul 2009
    Posts
    4
    Rep Power
    0

    Default i hope this solves it

    the following works .. but i m not able 2 figure out how the additional 1 is creeping inside :P

    import java.util.Scanner;
    public class Swap {


    public static void main(String[] args) {
    System.out.println("Enter ABCD");
    Scanner scanboy = new Scanner(System.in);
    int A = scanboy.nextInt();
    int B = scanboy.nextInt();
    int C = scanboy.nextInt();
    int D = scanboy.nextInt();
    System.out.println("A B C and D are " + A + " " + B + " " + C + " " + D ++);
    A = A+B+C+D;
    B = B+C+D;
    C= C+D;
    D = D;
    D = C-D;
    System.out.println(" value of D is " + D);
    C = B - C;
    System.out.println(" value of C is " + C);
    B = A - B;
    System.out.println(" value of B is " + B);

    A = A - (B + C + D)-1;
    System.out.println(" value of A is " + A);
    }

    }

  10. #10
    hero is offline Member
    Join Date
    Jul 2009
    Posts
    20
    Rep Power
    0

    Default

    If you used CODE-tags it would be alot nicer to read.
    [CODE]Your code goes here.[/CODE]

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •