473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

query regarding swap function

1>
#include<iostream>
using namespace std;

Expand|Select|Wrap|Line Numbers
  1. void swap(int& i, int& j)
  2.  {
  3.     int tmp = i;
  4.     i = j;
  5.     j = tmp;
  6. cout<<"The value after swap of x and y is"<<i<<" "<<j<<endl;
  7.  
  8. }
  9. int main()
  10. {
  11. int x, y;
  12. cout<<"enter the value of x and y"<<endl;
  13. cin>>x>>y;
  14. swap(x,y);
  15. cout<<"The value after swap of x and y is"<<x<<" "<<y<<endl;
  16. }
2>
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. main()
  4.     {
  5.         int a,b;
  6.         //void swap(int a,int b);
  7.         cout<<"enter the value of a and b"<<endl;
  8.         cin>> a>>b;
  9.         swap (a,b);
  10.         cout<<"The value after swap is"<<a<<":"<<b<<endl;
  11.     }
  12. void swap(int& x, int& y)
  13.     {
  14.         x=x+y;
  15.         y=x-y;
  16.         x=x-y;
  17.         cout<<"The value after swap is"<<x<<"\t"<<y<<endl;
  18.     }
i have written this two simple program. but the problem is that in the first case both the cout is being printed but is the second case the cout inside the defination of the swap function is not printer. can any one please let me understand why? pzzzzzzzzzzzz
Mar 19 '07 #1
3 1642
chella
51
1>
#include<iostream>
using namespace std;

void swap(int& i, int& j)
{
int tmp = i;
i = j;
j = tmp;
cout<<"The value after swap of x and y is"<<i<<" "<<j<<endl;

}
int main()
{
int x, y;
cout<<"enter the value of x and y"<<endl;
cin>>x>>y;
swap(x,y);
cout<<"The value after swap of x and y is"<<x<<" "<<y<<endl;
}

2>
#include <iostream>
using namespace std;
main()
{
int a,b;
//void swap(int a,int b);
cout<<"enter the value of a and b"<<endl;
cin>> a>>b;
swap (a,b);
cout<<"The value after swap is"<<a<<":"<<b<<endl;
}
void swap(int& x, int& y)
{
x=x+y;
y=x-y;
x=x-y;
cout<<"The value after swap is"<<x<<"\t"<<y<<endl;
}
i have written this two simple program. but the problem is that in the first case both the cout is being printed but is the second case the cout inside the defination of the swap function is not printer. can any one please let me understand why? pzzzzzzzzzzzz

Hi Narmada,
Can u tell me which compiler u r using? I have executed it in UNIX C++ complier. It works fine.

In the program u have posted i don't know y u have used the formal parameters as int& x,int& y.

Here is the code which i have executed,

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. main()
  3. {
  4. int a,b;
  5. void swap(int a,int b);
  6. cout<<"enter the value of a and b"<<endl;
  7. cin>> a>>b;
  8. swap (a,b);
  9. cout<<"The value after swap is"<<a<<":"<<b<<endl;
  10. }
  11. void swap(int x, int y)
  12. {
  13. x=x+y;
  14. y=x-y;
  15. x=x-y;
  16. cout<<"The value after swap is"<<x<<"\t"<<y<<endl;
  17. }
Regards,
Chella
Mar 19 '07 #2
arne
315 Expert 100+
1>
#include<iostream>
using namespace std;

void swap(int& i, int& j)
{
int tmp = i;
i = j;
j = tmp;
cout<<"The value after swap of x and y is"<<i<<" "<<j<<endl;

}
int main()
{
int x, y;
cout<<"enter the value of x and y"<<endl;
cin>>x>>y;
swap(x,y);
cout<<"The value after swap of x and y is"<<x<<" "<<y<<endl;
}

2>
#include <iostream>
using namespace std;
main()
{
int a,b;
//void swap(int a,int b);
cout<<"enter the value of a and b"<<endl;
cin>> a>>b;
swap (a,b);
cout<<"The value after swap is"<<a<<":"<<b<<endl;
}
void swap(int& x, int& y)
{
x=x+y;
y=x-y;
x=x-y;
cout<<"The value after swap is"<<x<<"\t"<<y<<endl;
}
i have written this two simple program. but the problem is that in the first case both the cout is being printed but is the second case the cout inside the defination of the swap function is not printer. can any one please let me understand why? pzzzzzzzzzzzz

The reason is that your 2nd program uses the swap() function of the STL, since your function is not in scope at the time of the call. Try to completely comment out your swap function and things will still be swapped :)

The first version works, since you define your function swap() before the call. So it's in scope.
Mar 19 '07 #3
The reason is that your 2nd program uses the swap() function of the STL, since your function is not in scope at the time of the call. Try to completely comment out your swap function and things will still be swapped :)

The first version works, since you define your function swap() before the call. So it's in scope.
thanks a lot
Mar 19 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Steve Hill | last post by:
Hi, suppose I have a vector allocated on the heap. Can I use a temporary (stack based vector) and swap to clear it, or is this unsafe. e.g. vector<int> *v; vector<int> tmp; v->swap(tmp); // is...
2
by: ma740988 | last post by:
So I'm reading the C++ coding standards(Shutter & Andrei), more specifically item 56. There's a statement: "Prefer to provide a nonmember swap function in the same namespace as your type when...
0
by: Paul Hsieh | last post by:
"Paul D. Boyle" <boyle@laue.chem.ncsu.edu> wrote: > There was a recent thread in this group which talked about the > shortcomings of fgets(). I decided to try my hand at writing a > replacement...
12
by: Eugen J. Sobchenko | last post by:
Hi! I'm writing function which swaps two arbitrary elements of double-linked list. References to the next element of list must be unique or NULL (even during swap procedure), the same condition...
2
by: Keith C. Perry | last post by:
Ok, I've tried a number of things here and I know I'm missing something but at this point my head is spinning (i.e. lack of sleep, too much coffee, etc...) My environment is PG 7.4.3 on Linux...
9
by: ma740988 | last post by:
Consider: # include <vector> # include <iostream> # include <cstdlib> # include <ctime> bool ispow2i ( double n ) {
28
by: Jess | last post by:
Hello, It is said that if I implement a "swap" member function, then it should never throw any exception. However, if I implement "swap" non- member function, then the restriction doesn't...
4
by: George2 | last post by:
Hello everyone, The following swap technique is used to make assignment operator exception safe (means even if there is exception, the current object instance's state is invariant). It used...
11
by: Dennis Jones | last post by:
Hi all, 1) Let's say you have two char 's of the same size. How would you write a no-fail swap method for them? For example: class Test { char s; void swap( Test &rhs ) {
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.