Jumat, 08 Juli 2011

To swap the numbers using the concept of function template

ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the template class.
STEP 3: Declare and define the functions to swap the values.
STEP 4: Declare and define the functions to get the values.
STEP 5: Read the values and call the corresponding functions.
STEP6: Display the results.
STEP 7: Stop the program.
PROGRAM:
01#include<iostream.h>
02#include<conio.h>
03 
04template<class t>
05 
06void swap(t &x,t &y)
07{
08   t temp=x;
09   x=y;
10   y=temp;
11}
12 
13void fun(int a,int b,float c,float d)
14{
15   cout<<"\na and b before swaping :"<<a<<"\t"<<b;
16   swap(a,b);
17   cout<<"\na and b after swaping  :"<<a<<"\t"<<b;
18   cout<<"\n\nc and d before swaping :"<<c<<"\t"<<d;
19   swap(c,d);
20   cout<<"\nc and d after swaping  :"<<c<<"\t"<<d;
21}
22 
23void main()
24{
25    int a,b;
26    float c,d;
27    clrscr();
28    cout<<"Enter A,B values(integer):";
29    cin>>a>>b;
30    cout<<"Enter C,D values(float):";
31    cin>>c>>d;
32    fun(a,b,c,d);
33    getch();
34}

Output:
Enter A, B values (integer): 10 20
Enter C, D values (float): 2.50 10.80
A and B before swapping: 10 20
A and B after swapping: 20 10
C and D before swapping: 2.50 10.80
C and D after swapping: 10.80 2.50

Tidak ada komentar:

Posting Komentar