Wednesday, October 26, 2016

Bubble Sort Using Function Template

Code:

#include<iostream>
using namespace std;
float a[100];
template<class T>
void sort(T a[],int n)
{
int c=n-1;
T k;
for(int j=0;j<=c;j++)
for(int i=0;i<n-j-1;i++)
{
if(a[i]>a[i+1])
{
k=a[i];
a[i]=a[i+1];
a[i+1]=k;
}
}
}
main()
{
int n,i;
cout<<"ENTER THE NO OF ELEMENTS";
cin>>n;
cout<<"ENTER THE ELEMENTS";
for(i=0;i<n;i++)
cin>>a[i];
sort(a,n);
printf("AFTER SORTING\n");
for(i=0;i<n;i++)
{
cout<<a[i];
cout<<"\n";}
}
Output:

Minimum Element In An Array with Anonymous Datatype:)

Following Program Find Minimum Element Using Function Template:

#include<iostream>
using namespace std;
template<class T>
T min(T a[],int n)
{
T min=a[0];
for(int i=0;i<n;i++)
if(min>a[i])
{
min=a[i];
}
return min;
}
main()
{   int n,a[100];
cout<<"Enter The No of Elements\n";
    cin>>n;
    cout<<"Enter The Elements\n";
    for(int i=0;i<n;i++)
    cin>>a[i];
    cout<<"Minimum Value is: "<<min(a,n);
}
Output:

Swap two Numbers Of Anonymous Datatypes using Function Template:)

Following Program Swap Two Numbers Using Function Template:

#include<iostream>
using namespace std;
template<class T>
void swapp(T &x,T &y)
{
T a;
a=x;
x=y;
y=a;
}
main()
{
int a,b;
cout<<"Enter A ";
cin>>a;
cout<<"Enter B ";
cin>>b;
swapp(a,b);
cout<<"\nAfter Swapping";
cout<<"\nA="<<a;
cout<<"\nB="<<b;
}
Output:

Overloading Function Template :)

When we overload a function template by defining function with same signature but with any particular type then if called function is an exact match of the normal function then it is called.
else the template function is called.
Following Program Illustrates The Concept:

#include<iostream>
using namespace std;
template<class T>
void display(T x)
{
cout<<"\nTemplate Function\n";
cout<<"Value Passed: "<<x;
}
void display(int x)
{
cout<<"\nNormal Function\n";
cout<<"Value Passed: "<<x;
}
main()
{
display(5);
display('N');
}

Output:


Template Class With Multiple Parameters and Default Data Types :)

Following Program Illustrate The Use Of Template Class With Multiple Parameters:

#include<iostream>
using namespace std;
template<class T1=int,class T2=int>
class sample
{
T1 a;
T2 b;
public:
sample(T1 x,T2 y)
{
a=x;
b=y;
}
void display()
{
cout<<"\nA="<<a<<"\nB="<<b;
cout<<"\n********************";
}
};
main()
{
sample<int,float>a(12,76.23);//passing int and float
    a.display();
sample<float,int>b(56.7,76);//passing float and int
b.display();
sample<float>c(15.7,12);//passing one type and other is default
c.display();
sample<>d('a',13);//default types used
d.display();
}

Output:


Insertion Sort

Following Program Sort The Array With Insertion Sort

Insertion Sort: In this the elements are inserted in same array by comparing it with previously inserted elements.

#include<iostream>
using namespace std;
main()
{
int a[100],n,i;
cout<<"Enter The No Of Elements in Array ";
cin>>n;
cout<<"Enter The Elements\n";
for(i=0;i<n;i++)
cin>>a[i];
int k;
for(k=1;k<n;k++)
{ int j=k-1;    //for comparing all elements with present element
int temp=a[k];
while(j>0 && temp<a[j])
{
a[j+1]=a[j];
j--;
}
a[j]=temp;
}
cout<<"Sorted List Is ";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
}
Special Thank To Ms.Naina For This Program

Output:


Tuesday, October 25, 2016

Dot Product Of Vectors With Template Class

By using Template Class We Can do Generic Programming that is we can avoid the writing of same code for different datatypes.
Following Program Illustrate This Concept:
#include<iostream>
using namespace std;
template<class T>
class vector
{ T v[3];
public:
vector()
{ cout<<"Enter The Components of vector\n";
for(int i=0;i<3;i++)
cin>>v[i];
}
T operator*(vector &b)
{ T sum=0;
for(int i=0;i<3;i++)
sum+=v[i]*b.v[i];
return sum;
}
void display()
{ cout<<"Components of Vector Are ";
for(int i=0;i<3;i++)
cout<<v[i]<<"\t";
cout<<"\n";
}
};
main()
{ vector<typename>v1,v2;   //type name any type for which we want to do product
v1.display();
v2.display();
cout<<"V1*V2="<<v1*v2;
}

Output: