+ 2

Why is it not work?

#include <iostream> using namespace std; void swap(int x,int &y) int temp; temp=x; x=y; y=temp; int main() { int a=100; int b=200; cout <<"before swap , value of a "<<a<<endl; swap(a,b); cout <<"after swap , value of a "<<a<<endl; cout <<"after swap , value of b "<<b<<endl; return 0; }

17th May 2022, 12:07 PM
Sangeetha Santhiralingam
Sangeetha Santhiralingam - avatar
12 Respostas
+ 3
Yes by my smallmistake
17th May 2022, 12:15 PM
Sangeetha Santhiralingam
Sangeetha Santhiralingam - avatar
+ 3
#include <iostream> using namespace std; void wap(int x,int &y) {int temp; temp=x; x=y; y=temp; } int main() { int a=100; int b=200; cout <<"before swap , value of a "<<a<<endl; wap(a,b); cout <<"after swap , value of a "<<a<<endl; cout <<"after swap , value of b "<<b<<endl; return 0; } function name is swap in that code now see function name is wap
17th May 2022, 12:27 PM
Sangeetha Santhiralingam
Sangeetha Santhiralingam - avatar
+ 2
Close the swap function.. edit: Sangeetha Santhiralingam oh. you don't have opening brace also... void swap( int x, int &y) { //add it //function body.. } //add this int main() { .. }
17th May 2022, 12:09 PM
Jayakrishna 🇼🇳
17th May 2022, 12:41 PM
Sangeetha Santhiralingam
Sangeetha Santhiralingam - avatar
+ 1
😑 okay
17th May 2022, 12:10 PM
Sangeetha Santhiralingam
Sangeetha Santhiralingam - avatar
+ 1
why is there a need to define swap? "The function std::swap() is a built-in function in the C++ Standard Template Library (STL) which swaps the value of two variables." you can use swap without having to define it yourself.😁 #include <iostream> using namespace std; int main() { int a = 5; int b = 50; cout << "Before swap:\n"; cout << "a = " << a << endl; cout << "b = " << b << endl; swap(a, b); cout << "After swap:\n"; cout << "a = " << a << endl; cout << "b = " << b << endl; return 0; }
17th May 2022, 12:21 PM
Bob_Li
Bob_Li - avatar
+ 1
Pass both <x> and <y> by reference void swap( int& x, int& y )
17th May 2022, 12:27 PM
Ipang
+ 1
What is your expected outcome by your code? Your code only swaps y value with x. x remains same so x, y have same values. edit: code is fine now.Sangeetha Santhiralingam
17th May 2022, 12:31 PM
Jayakrishna 🇼🇳
+ 1
Sangeetha Santhiralingam I guess it is for practice or an assignment. Make your own tools instead of using tools in the toolbox.
17th May 2022, 12:32 PM
Bob_Li
Bob_Li - avatar
+ 1
me too i have that problems
17th May 2022, 2:23 PM
ZUBAIRU ABUBAKAR
0
Hello dear
18th May 2022, 10:44 PM
Archer
Archer - avatar
0
Gekjcx
19th May 2022, 9:42 AM
Baydaa Sadaani