Advertisement

function issues

Started by January 26, 2003 06:23 PM
8 comments, last by pandabear114 21 years, 9 months ago
i had a program to swap variables, which was working fine but when i tried to put it into functions it went crazy i dont even know what is going on anymore please help :< #include int x, y; void input(int x,int y) { cout << "Enter X\n"; cin >> x; cout << "\nEnter Y\n"; cin >> y; } void swap(int x,int y) { x ^= y; y ^= x; x ^= y; return; } void output(int x,int y) { cout << "\n\nX is now " << x << "\nY is now " << y << "\n"; return; } int main() { input(x,y); swap(x,y); output(x,y); return 0; }
You''re swapping copies of the integers. To swap the integers themselves, swap''s parameters will need to be pointers or references.
Alternatively, #include <algorithm> and use std::swap instead.
Advertisement
Beer Hunter is, of course, right. Beyond that, I rather wonder how good that swapping algorithm is ... the XOR method is often described as overly clever. It is not extensible to anything but basic types, for instance, and won''t work for cases when x == y.
Well, that was the first thing I saw, anyway. Upon re-reading it, I see some others.
Try this:

  #include <algorithm>#include <iostream>using namespace std;void input(int& x, int& y){  cout << "Enter X" << endl;  cin >> x;  cout << "Enter Y" << endl;  cin >> y;}void output(int x, int y){  cout << "\n\nX is now " << x << "\nY is now " << y << endl;}int main(){  int x, y;  input(x, y);  swap(x, y);  output(x, y);}  
quote: Original post by Miserable
and won''t work for cases when x == y.

Sure it will...


Update GameDev.net system time campaign: ''date ddmmHHMMYYYY''
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
Correction : it doesn't work when x and y are the same variable - which may happen when variable aliasing is involved. A robust function would essentially do a no-op. But here the code becomes :

x ^= x; // zeroes x
x ^= x; // zeroes x
x ^= x; // zeroes x

Given that flaw, the fact that it only works with basic types and that it is no faster than using a temporary, this 'trick' is strongly advised against.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

[edited by - Fruny on January 27, 2003 4:07:03 AM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
Directly from std::swap

quote:


  template<class _Ty> inline	void swap(_Ty& _Left, _Ty& _Right)	{	// exchange values stored at _Left and _Right	_Ty _Tmp = _Left;	_Left = _Right, _Right = _Tmp;	}  


end quote:

Notice the pointers.


____________________________________________________________
Try RealityRift at www.planetrift.com


[edited by - MichaelT on January 27, 2003 4:21:55 AM]
No no no no! :)
MichaelT-

Not to be picky, but I think you mean references (not pointers )... just don't want anyone to get confused!

-Glyph

[edited by - Glyph on January 28, 2003 10:28:26 AM]
Both references and pointers can be used to implement a swap algorithm. Check out Sam''s Teach Yourself C++ in 21 days for the code.

Brendan
Brendan"Mathematics is the Queen of the Sciences, and Arithmetic the Queen of Mathematics" -Gauss
quote: Original post by Glyph
MichaelT-

Not to be picky, but I think you mean references (not pointers )... just don''t want anyone to get confused!

-Glyph

[edited by - Glyph on January 28, 2003 10:28:26 AM]


Quite right. ;-)


____________________________________________________________
Try RealityRift at www.planetrift.com
No no no no! :)

This topic is closed to new replies.

Advertisement