user8350633
user8350633

Reputation: 11

Swap values of x and y

How can I change the values of x and y in this code?

When I try to do it by x=y, y=x; it changes it to the same numbers.

How can I do this?

How can I do this for 3 values like (x y z)?

#include <iostream>
using namespace std;
int main()
{
    int x=4;
    int y=6;
    cout<<x<<y;
    return 0;
}

I tried this code before but it isn't working the way I want it.

#include <iostream>
using namespace std;
int main()
{
    int x=4;
    int y=6;
    x=y,y=x;
    cout<<x<<y;
    return 0;
}

Upvotes: 0

Views: 5802

Answers (7)

Eitan
Eitan

Reputation: 3

Here is code to swap 2 variables(1 line)

#include <iostream>
using namespace std;
int main()
{
    int x = 4;
    int y = 6;
    cout << x<< y;
    swap(x,y);
    cout << x << y;
    getchar();
    return 0;
}

Here is code to swap 3 variables(1 line)

#include <iostream>
#include <tuple>
using namespace std;
int main()
{
    int x = 4;
    int y = 6;
    int z = 8;
    cout << x<< y<<z;
    tie(x, y, z) = make_tuple(y, z, x);
    cout << x << y << z;
    getchar();
    return 0;
}

Upvotes: 0

Daniel Jodłoś
Daniel Jodłoś

Reputation: 356

There are two options:

  1. Introduce temporary variable:

    int temporary = y; y = x; x = temporary;

  2. Or use std::swap like this std::swap(x, y); (you might need to import <algorithm> or <utility>)

Now why you are getting this error? Let's analyze what you are doing here step by step:

  1. x = y; Give x value of y. So now x is equal to y
  2. y = x; Give y value of x. But wait, x is now equal to y. So nothing changes.

If you still have trouble understanding what you did wrong I suggest taking a piece of paper and following your own code step by step on paper writing state of program at each step.

And one last thing as advice for the future. Please make you questions clear and properly formatted. For example, it totally don't understand your point with 3 values. Please explain what you mean and probably provide some example and then maybe someone will be able to help you.

Upvotes: 1

Jarod42
Jarod42

Reputation: 217145

For 2 variables, you should use std::swap, for more variables, you may use std::tuple:

std::tie(x, y, z) = std::make_tuple(y, z, x);

Demo

Upvotes: 2

Post Self
Post Self

Reputation: 1554

This is because you first set x's value and then copy that value into y. There is a standard library function called std::swap, which should do the job.

You can see an exapmle of it here.

std::swap is defined in the header <algorithm> before C++11 and in <utility> since C++11. So make sure you #include the correct header.

The benefit of using std::swap in C++11 as opposed to having a third temporary variable that you copy the value into, is that std::swap uses std::move and thereby creates no additional copies.


For three numbers you'll have to make your own implementation like this:

#include <iostream>

int main() {
    int x{5}, y{3}, z{2};

    int temp{std::move(x)};
    x = std::move(y);
    y = std::move(z);
    z = std::move(temp);

    std::cout << x << ' ' << y << ' ' << z << '\n';

    return 0;

}

Ideone

Upvotes: 2

moty manelis
moty manelis

Reputation: 1

x=4
y=6

x=y // x=6, y=6

y=x // does nothing

Try using another variable:

#include <iostream>
using namespace std;
int main()
{
    int x=4;
    int y=6;
    int temp=x;
    x=y,y=temp;
    cout<<x<<y;
    return 0;
}

Upvotes: 0

Wasif Ali
Wasif Ali

Reputation: 894

Without using a third variable you can do swapping two variables like this,

#include <iostream>
using namespace std;
int main()
{
    int x=4;
    int y=6;

    x = x + y;
    y = x - y;
    x = x - y;
    cout<<"X = "<<x << "\n"
        <<"Y = "<<y;
    return 0;
}

So the output will be,

X = 6

Y = 4

Upvotes: 0

user6623857
user6623857

Reputation:

try this:

#include <iostream>
using namespace std;
int main()
{
    int x=4;
    int y=6;

    int temp=x;
    x=y;
    y=temp;
    cout<<x<<y;
    return 0;
}

Upvotes: 0

Related Questions