Sign in with
Sign up | Sign in
Your question

Integer Swapping

Last response: in Applications
Share
March 26, 2012 2:05:48 AM

ok so I have

#include <iostream>
#include <cmath>
#include <stdio.h>

using namespace std;

void swap ( int &x, int &y);


int main()

{
int number;
cout << "x is ";
cin >> number;
int x = number;

cout << "y is ";
cin >> number;
int y = number;

swap( x, y);
cout << "Swapped the first number is now(" << x << ")and the second is now (" << y << ")";

return 0;
}

and i am just trying to get it to swap them with no avail any and all help would be greatly appreciated

More about : integer swapping

March 26, 2012 9:50:19 AM

you don't know how to do this? this is programing 101 in any language and any school...
You need an iplemetration of the swap function?
m
0
l
a b L Programming
March 26, 2012 11:36:46 AM

Before you write any code, think about your design and what you are trying to accomplish. This exercise is easy to do only if you understand what happens when you do a variable assignment. Get out the pen and paper, draw some boxes representing X and Y with their values inside and try to work out how you're going to get them to swap without losing either value.

Alternatively, you could probably Google for a simplistic swap implementation, but there's no point writing a function that you don't understand. If you don't understand variable assignments (a fundamental concept) then you'll never progress past the most basic programming.
m
0
l
Related resources
March 27, 2012 9:20:54 PM

you are right if I dont understand then I wont really know but just learning the basics for me has been easier by seeing examples I have been reading forum after forum looking for info on what type of form to use for certain issues but even then it is hard to understand but some one was even saying to use temp values but that is for user input but ok after many hours of research this is what I have found out

#include <iostream>
using std::cout;

void swap(int& i, int& j)
{
int t = i;
i = j;
j = t;
}

int main() {
int a = 1, b = 2;
cout << "Before. a: " << a << ", b: " << b << "\n";
swap(a, b);
cout << "After. a: " << a << ", b: " << b << "\n";

system("pause");
}

It works and even pauses and shows results but however I want to be able to have a user input for the numbers how would I go about this?
m
0
l
a b L Programming
March 28, 2012 12:40:19 AM

You could do it the same as you did earlier, by using cin rather than hard coding the values for a and b.

Do you understand why you need that third temporary variable in the swap function?
m
0
l
March 28, 2012 8:09:52 AM

randomizer said:
You could do it the same as you did earlier, by using cin rather than hard coding the values for a and b.

Do you understand why you need that third temporary variable in the swap function?



This is a very important question, because if you don't understand that, then you are just not cut out to be a programmer. In every school and programming class if you don't know the awnser to this, you should automaticaly fail that class.
m
0
l
April 5, 2012 3:51:49 PM

I need the 3rd or temp variable because the two variables cannot be the same number or it will not work so I assign x to one then y to 2 then I have to sign x or y to 3 and switch the integers
m
0
l
April 12, 2012 5:42:26 AM

and p.s. I am in coding 101 lol
m
0
l
!