Swapping 3 integers in C program

In summary, using a temporary variable to hold an integer value while swapping it does not work in C.
  • #1
kthouz
193
0
Hi! i have a problem about swapping three integers in C program.
Usually i use the call by reference for two integers only. Now for three i can't get t.
 
Technology news on Phys.org
  • #2
I usually create a temporary variable to hold a value while I am doing a swap. Would that do the trick?
 
  • #3
Or the old fashion way, more instructions but no temp variable for a swap:

A ^= B
B ^= A
A ^= B

A 3 variable swap is really a rotate.

T = A
A = B
B = C
C = T
 
  • #4
To swap 3 integer consider the following 2 methods:

* divide to conquer: swap 2 by 2
swap A & B
A = B+A
B = A-B
A = A-B

then swap A & C or B & C

* all in one:
let the variables A, B & C have the initial values A0,B0 & C0 respectively
A = A+B+C (A0+B0+C0)
B = A-B-C (A0+B0+C0 - B0 - C0 = A0)
C = A-B-C (A0+B0+C0 - A0 - C0 = B0)
A = A-B-C (A0+B0+C0 - A0 - B0 = C0)

-----------------------------------------------------
Correct me if I am wrong.
http://ghazi.bousselmi.googlepages.com/présentation2
 
  • #5
thanks but i can't really get it!
i was expeting of having at least 5lines. (because number of possible swaps=n!)
indeed we are preparing our exam,the teacher didn't explain it clearly and he just told us to figure it out ourselves. please gimme an full example so that i can see the whole format.
 
  • #6
All possible swaps? There are 6 ways you can order 3 variables.

A B C // no swaps
A C B // swapsecondpair
B A C // swapfirstpair
B C A // swapfirstpair, swapsecondpair
C A B // swapouterpair, swapsecondpair
C B A // swapouterpair

Code:
int A, B, C, T;

void swapfirstpair()
{
    T = A;
    A = B;
    A = T;
}

void swapsecondpair()
{
    T = B;
    B = C;
    C = T;
}

void swapouterpair()
{
    T = A
    A = C
    C = T
}
 
Last edited:
  • #7
tabchouri said:
To swap 3 integer consider the following 2 methods:

* divide to conquer: swap 2 by 2
swap A & B
A = B+A
B = A-B
A = A-B

then swap A & C or B & C

* all in one:
let the variables A, B & C have the initial values A0,B0 & C0 respectively
A = A+B+C (A0+B0+C0)
B = A-B-C (A0+B0+C0 - B0 - C0 = A0)
C = A-B-C (A0+B0+C0 - A0 - C0 = B0)
A = A-B-C (A0+B0+C0 - A0 - B0 = C0)

-----------------------------------------------------
Correct me if I am wrong.
http://ghazi.bousselmi.googlepages.com/présentation2

you can do this more efficiently and without rounding errors by using ^ to the same effect

a ^= b ^ c;
b ^= b ^ c;
c ^= b ^ c;
a ^= b ^ c;

swap the variable names to change the swapping that is performed, you can extend this to n variables afaik... this works because ^ preserves information in the same way as + and -, except that because the operation is its own inverse there is one symbol instead of two, also because it is bitwise there is no precision loss.
 
  • #8
I can't believe people are recommending the triple xor trick. Yes it is clever, but it's pretty bad to use it in a language even as high level as C.

Compiling your code with any optimization at all will cause the compiler to optimize out the temporary variable.

If you're really interested in using assembleresque tricks, you might as well check out Sean Andersen's Bit Twiddling Hacks.
 
  • #9
Jheriko said:
you can do this more efficiently and without rounding errors by using ^ to the same effect

a ^= b ^ c;
b ^= b ^ c;
c ^= b ^ c;
a ^= b ^ c;

swap the variable names to change the swapping that is performed, you can extend this to n variables afaik... this works because ^ preserves information in the same way as + and -, except that because the operation is its own inverse there is one symbol instead of two, also because it is bitwise there is no precision loss.

with + and - there is no precision loss neither, there might be some cyclic overflows in the intermediate values, but the final results will always be correct.

-----------------------------------------------------
Correct me if I am wrong.
http://ghazi.bousselmi.googlepages.com/présentation2
 
  • #10
tabchouri said:
there might be some cyclic overflows in the intermediate values, but the final results will always be correct.
As I recall, the C standard says that (signed) integer overflow results in undefined behavior.
 
  • #11
Hurkyl said:
As I recall, the C standard says that (signed) integer overflow results in undefined behavior.

Probably. But if the computer uses IEEE arithmetic the overflow won't affect the result as tabchouri says. It would be best to comment this, but most modern systems use IEEE... yes?
 
  • #12
I thought IEEE only referred to floating point arithmetic? (In this context)

But yes, most systems will do what you expect it to do. But you shouldn't assume it -- for example, what if someone using some fancy system decides to enable trapping of arithmetic overflow, so as to detect errors? Or you're using some fancy OS that uses trap bits in its integer representations to detect a variety of errors?



I'm always amused by observations such as... signed integer overflow formatting your hard drive is permitted by the C standard. :smile:
 
Last edited:
  • #13
Hurkyl said:
I thought IEEE only referred to floating point arithmetic? (In this context)

Yeah, you're right of course... what is the name for the standard I'm thinking of?

Hurkyl said:
I'm always amused by observations such as... signed integer overflow formatting your hard drive is permitted by the C standard. :smile:

Quite right, and amusing too. :D
 

Related to Swapping 3 integers in C program

1. How do I swap 3 integers in a C program?

To swap 3 integers in a C program, you can use a temporary variable. First, assign the value of the first integer to the temporary variable. Then, assign the value of the second integer to the first integer. Finally, assign the value of the temporary variable to the third integer.

2. What is the syntax for swapping 3 integers in a C program?

The syntax for swapping 3 integers in a C program is as follows:

temp = first;
first = second;
second = temp;

3. Can I swap 3 integers without using a temporary variable?

Yes, you can swap 3 integers without using a temporary variable by using arithmetic operations. For example, if the three integers are a, b, and c, you can do the following:

a = a + b + c;
b = a - (b + c);
c = a - (b + c);
a = a - (b + c);

4. What is the purpose of swapping 3 integers in a C program?

The purpose of swapping 3 integers in a C program is to rearrange the values of the integers in a specific order. This can be useful in sorting algorithms or in other situations where the order of the integers needs to be changed.

5. Can I use a function to swap 3 integers in a C program?

Yes, you can use a function to swap 3 integers in a C program. This can make your code more organized and easier to read. However, the same logic as mentioned in the previous questions would still apply in the function.

Similar threads

Replies
63
Views
3K
  • Programming and Computer Science
2
Replies
49
Views
3K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
4
Views
748
Replies
9
Views
1K
  • Programming and Computer Science
Replies
8
Views
925
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
13
Views
512
  • Programming and Computer Science
Replies
7
Views
3K
Back
Top