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
3 1642
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, - #include <iostream.h>
-
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;
-
}
Regards,
Chella
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
by: ma740988 |
last post by:
Consider:
# include <vector>
# include <iostream>
# include <cstdlib>
# include <ctime>
bool
ispow2i ( double n )
{
|
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...
|
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...
|
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 )
{
|
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
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |