Source:: Aim: To Search An Element in An Array Using Linear Search
Source:: Aim: To Search An Element in An Array Using Linear Search
Source:
#include <iostream>
using namespace std;
int main()
{
int fl,x,n,a[20],i;
cout<<"Enter Size:";
cin>>n;
cout<<"Enter "<<n<<" numbers:";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter search element:";
cin>>x;
fl=-1;
for(i=0;i<n;i++)
if(a[i]==x)
fl=i;
if(fl==-1)
cout<<"Number not found!\n";
else
cout<<"Number found at position "<<fl+1<<endl;
return 0;
}
Output:
Aim: To search an element in an array using binary search
Source:
#include<iostream>
using namespace std;
int bin_sr(int a[], int n, int x)
{
int l=0,u=n-1,m;
while(l<=u)
{
m=(l+u)/2;
if(a[m]==x)
return m+1;
else if(x>a[m])
l=m+1;
else
u=m-1;
}
return -1;
}
int main()
{
int fl,x,n,a[20],i;
cout<<"Enter Size:";
cin>>n;
cout<<"Enter "<<n<<" numbers in ascending order:";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter search element:";
cin>>x;
fl=bin_sr(a,n,x);
if(fl==-1)
cout<<"Number not found!\n";
else
cout<<"Number found at position "<<fl<<endl;
return 0;
}
Output:
Aim: To sort an array using linear sort
Source:
#include<iostream>
using namespace std;
int main()
{
int n,a[20],m,t,i,j;
cout<<"Enter Size:";
cin>>n;
cout<<"Enter "<<n<<" numbers:";
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n-1;i++)
{
m=i;
for(j=i+1;j<n;j++)
if(a[j]<a[m])
m=j;
t=a[m];
a[m]=a[i];
a[i]=t;
}
cout<<"Sorted Array:\n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
}
Output:
Aim: To sort an array using bubble sort
Source:
#include<iostream>
using namespace std;
int main()
{
int n,a[20],m,t,i,j;
cout<<"Enter Size:";
cin>>n;
cout<<"Enter "<<n<<" numbers:";
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<(n-1);i++)
{
for(j=0;j<(n-i-1);j++)
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
cout<<"Sorted Array:\n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
}
Output:
Aim: To sort an array using insertion sort
Source:
#include<iostream>
using namespace std;
int main()
{
int n,a[20],m,t,i,j;
cout<<"Enter Size:";
cin>>n;
cout<<"Enter "<<n<<" numbers:";
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
{
t=a[i];
m=i;
while(m>0 && t<a[m-1])
{
a[m]=a[m-1];
m--;
}
a[m]=t;
}
cout<<"Sorted Array:\n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
}
Output:
Aim: To sort an array using merge sort
Source:
#include<iostream>
using namespace std;
int t[20];
void msort(int a[], int l, int m, int u)
{
int i=l,j=m+1,k=l;
while(i<=m && j<=u)
{
if(a[i]<a[j])
t[k++]=a[i++];
else
t[k++]=a[j++];
}
while(i<=m)
t[k++]=a[i++];
while(j<=u)
t[k++]=a[j++];
for(i=l;i<=u;i++)
a[i]=t[i];
}
void merger(int a[], int l, int u)
{
int m;
if(l<u)
{
m=(l+u)/2;
merger(a,l,m);
merger(a,m+1,u);
msort(a,l,m,u);
}
}
int main()
{
int n,a[20],m,t,i,j;
cout<<"Enter Size:";
cin>>n;
cout<<"Enter "<<n<<" numbers:";
for(i=0;i<n;i++)
cin>>a[i];
merger(a,0,n-1);
cout<<"Sorted Array:\n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
}
Output:
Aim: To sort an array using quick sort
Source:
#include<iostream>
using namespace std;
int part(int a[], int p, int r)
{
int x=a[r],i=p-1,j,t;
for(j=p;j<r;j++)
if(a[j]<=x)
{
i++;
t=a[j];
a[j]=a[i];
a[i]=t;
}
i++;
t=a[i];
a[i]=a[j];
a[j]=t;
return i;
}
void quickr(int a[], int p, int r)
{
int m;
if(p<r)
{
m=part(a,p,r);
quickr(a,p,m-1);
quickr(a,m+1,r);
}
}
int main()
{
int n,a[20],m,t,i,j;
cout<<"Enter Size:";
cin>>n;
cout<<"Enter "<<n<<" numbers:";
for(i=0;i<n;i++)
cin>>a[i];
quickr(a,0,n-1);
cout<<"Sorted Array:\n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
return 0;
}
Output:
Aim: To print a set of strings in the given pattern using 2D array
Source:
#include<iostream>
using namespace std;
int main()
{
int i,j,l[5],m;
char a[5][20];
cout<<"Enter 5 words:\n";
for(i=0;i<5;i++)
cin>>a[i];
m=0;
for(i=0;i<5;i++){
for(j=0;a[i][j]!='\0';j++);
l[i]=j;
if(j>m)
m=j;
}
cout<<"Pattern:\n";
for(i=0;i<m;i++)
{
for(j=0;j<5;j++)
{
if(i<l[j])
cout<<a[j][i]<<" ";
else
cout<<" ";
}
cout<<endl;
}
return 0;
}
Output:
Aim: To print a set of strings in the given pattern using point
Source:
#include<iostream>
#include<malloc.h>
using namespace std;
int main()
{
int i,j,l[5],m;
char *a[5],*p;
cout<<"Enter 5 words:\n";
for(i=0;i<5;i++)
{
a[i]=(char*)malloc(20*sizeof(char));
cin>>a[i];
}
m=0;
for(i=0;i<5;i++){
for(j=0,p=a[i];*p!='\0';j++,p++);
l[i]=j;
if(j>m)
m=j;
}
cout<<"Pattern:\n";
for(i=0;i<m;i++)
{
for(j=0;j<5;j++)
{
if(i<l[j])
cout<<a[j][i]<<" ";
else
cout<<" ";
}
cout<<endl;
}
return 0;
}
Output:
Aim: To print a set of strings in the given pattern using 2D array
Source:
#include<iostream>
#include<malloc.h>
using namespace std;
int main()
{
int i,j,l[5],m;
char a[5][20];
cout<<"Enter 5 words:\n";
for(i=0;i<5;i++)
{
cin>>a[i];
}
m=0;
for(i=0;i<5;i++){
for(j=0;a[i][j]!='\0';j++);
l[i]=j;
if(j>m)
m=j;
}
cout<<"Pattern:\n";
for(i=0;i<m;i++)
{
for(j=0;j<5;j++)
{
if(i>=(m-l[j]))
cout<<a[j][i-m+l[j]]<<" ";
else
cout<<" ";
}
cout<<endl;
}
return 0;
}
Output:
Aim: To print a set of strings in the given pattern using pointers
Source:
#include<iostream>
#include<malloc.h>
using namespace std;
int main()
{
int i,j,l[5],m;
char *a[5],*p;
cout<<"Enter 5 words:\n";
for(i=0;i<5;i++)
{
a[i]=(char*)malloc(20*sizeof(char));
cin>>a[i];
}
m=0;
for(i=0;i<5;i++){
for(j=0,p=a[i];*p!='\0';j++,p++);
l[i]=j;
if(j>m)
m=j;
}
cout<<"Pattern:\n";
for(i=0;i<m;i++)
{
for(j=0;j<5;j++)
{
if(i>=(m-l[j]))
cout<<a[j][i-m+l[j]]<<" ";
else
cout<<" ";
}
cout<<endl;
}
return 0;
}
Output:
Aim: To demonstrate the use of friend functions.
Source:
#include<iostream>
#include<math.h>
using namespace std;
class DB;
class DM
{
float m,cm;
public:
void disp()
{
cout<<"Distance: "<<(int)m<<" m(s) "
<<(int)cm<<" cm(s)\n";
}
DM()
{
m=cm=0;
}
void getd(float x, float y)
{
m=x;
cm=y;
}
friend DB addinch(DM,DB);
friend DM addcm(DM,DB);
};
class DB
{
float ft,inch;
public:
void disp()
{
cout<<"Distance: "<<(int)ft<<" ft "
<<(int)inch<<" inch(s)\n";
}
DB()
{
ft=inch=0;
}
void getd(float x, float y)
{
ft=x;
inch=y;
}
friend DB addinch(DM,DB);
friend DM addcm(DM,DB);
};
DB addinch(DM x, DB y)
{
DB z;
z.inch=fmod((y.inch+(x.m*100+x.cm)/2.54),12);
z.ft=y.ft+(y.inch+(x.m*100+x.cm)/2.54)/12;
return z;
}
DM addcm(DM x, DB y)
{
DM z;
z.cm=fmod((x.cm+(y.ft*12+y.inch)*2.54),100);
z.m=x.m+(x.cm+(y.ft*12+y.inch)*2.54)/100;
return z;
}
int main()
{
DB x;
DM y;
float a,b;
int ch;
cout<<"Enter distance A: (m cm)\n";
cin>>a>>b;
y.getd(a,b);
cout<<"Enter distance B: (ft inch)\n";
cin>>a>>b;
x.getd(a,b);
cout<<"Final Distance Format?\n1.m cm\n2.ft inch\n"
<<"\nEnter Choice:";
cin>>ch;
cout<<"\nFinal ";
if(ch==1)
{
DM z=addcm(y,x);
z.disp();
}
else
{
DB z=addinch(y,x);
z.disp();
}
return 0;
}
Output:
Aim: To demonstrate the use of template.
Source:
#include<iostream>
#include<math.h>
using namespace std;
template<class T>
T dist(T x1,T y1,T x2, T y2)
{
return sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}
int main()
{
cout<<"\nDistance between integer coordinates (1,3) (5,4) :"
<<dist(1,3,5,4);
cout<<"\nDistance between decimal coordinates (1.2,2.3) (4.1,5.9): "
<<dist(1.2,2.3,4.1,5.9)<<endl;
return 0;
}
Output:
Aim: To implement single inheritance.
Source:
#include<iostream>
#include<math.h>
using namespace std;
class A
{
int x;
protected:
int y;
public:
void getd(int a, int b)
{
x=a;
y=b;
}
void printd()
{
cout<<"Members of A: "<<x<<" "<<y<<endl;
}
};
class B: public A
{
int a,b;
public:
void getdata(int m, int n)
{
getd(m*2, n-3);
a=m+n+y;
b=m+n-y;
}
void disp()
{
printd();
cout<<"Memebers of B: "<<a<<" "<<b<<endl;
}
};
int main()
{
B d;
d.getdata(10,15);
d.disp();
return 0;
}
Output:
Aim: To implement multilevel inheritance.
Source:
#include<iostream>
#include<math.h>
using namespace std;
class A
{
int x;
protected:
int y;
public:
void getd(int a, int b)
{
x=a;
y=b;
}
void printd()
{
cout<<"Members of A: "<<x<<" "<<y<<endl;
}
};
class B: public A
{
int a,b;
public:
void getdata(int m, int n)
{
getd(m*2, n-3);
a=m+n+y;
b=m+n-y;
}
void disp()
{
printd();
cout<<"Memebers of B: "<<a<<" "<<b<<endl;
}
};
class C: public B
{
int l;
public:
void input(int a, int b)
{
l=a%b;
getdata(a*b,a+b);
}
void output()
{
cout<<"Members of C: "<<l<<endl;
disp();
}
};
int main()
{
C d;
d.input(10,15);
d.output();
return 0;
}
Output:
Aim: To implement multiple inheritance.
Source:
#include<iostream>
#include<math.h>
using namespace std;
class A
{
int x;
protected:
int y;
public:
void getd(int a, int b)
{
x=a;
y=b;
}
void printd()
{
cout<<"Members of A: "<<x<<" "<<y<<endl;
}
};
class B
{
int a,b;
public:
void getdata(int m, int n)
{
a=m+n;
b=m-n;
}
void disp()
{
cout<<"Memebers of B: "<<a<<" "<<b<<endl;
}
};
class C: public A,public B
{
int l;
public:
void input(int a, int b)
{
l=a%b;
getdata(a*b,a+b);
getd(a,b);
}
void output()
{
cout<<"Members of C: "<<l<<endl;
disp();
printd();
}
};
int main()
{
C d;
d.input(10,15);
d.output();
return 0;
}
Output:
Aim: To demonstrate the use of pointer to functions.
Source:
#include<iostream>
using namespace std;
int small(int a,int b)
{
return a<b?a:b;
}
int large(int a,int b)
{
return a>b?a:b;
}
int select(int (*fn)(int,int),int x,int y)
{
return fn(x,y);
}
int main()
{
int m,n;
int (*p)(int,int);
cout<<"enter two integers: ";
cin>>m>>n;
int high=select(large,m,n);
p=small;
int low=select(p,m,n);
cout<<"Greatest Number:"<<high
<<"\nSmallest Number:"<<low;
return 0;
}
Output:
Aim: To demonstrate overloading of + operator using class
member function.
Source:
#include<iostream>
using namespace std;
class complex
{
float re,img;
public:
void getd (float x,float y)
{
re=x;
img=y;
}
complex operator +(complex);
void disp()
{
cout<<re;
if(img>0)
cout<<" + "<<img<<"i";
else
cout<<" - "<<-img<<"i";;
}
};
complex complex::operator +(complex c)
{
complex temp;
temp.re=re+c.re;
temp.img=img+c.img;
return temp;
}
main()
{
complex c1,c2,c3;
c1.getd(3.1,-4.9);
c2.getd(4.5,5.6);
c3=c1+c2;
cout<<"+ operator overloaded:\n";
c1.disp();
cout<<" + ";
c2.disp();
cout<<" = ";
c3.disp();
cout<<endl;
return 0;
}
Output:
Aim: To demonstrate operator overloading of ^ using friend
function.
Source:
#include<iostream>
using namespace std;
class ABC
{
int a,b;
public:
void getd (int x,int y)
{
a=x;
b=y;
}
friend ABC operator ^(ABC,ABC);
void disp()
{
cout<<a<<" "<<b;
}
};
ABC operator ^(ABC a, ABC b)
{
ABC t;
t.a=a.a^b.a;
t.b=a.b^b.b;
return t;
}
main()
{
ABC c1,c2,c3;
c1.getd(10,34);
c2.getd(43,56);
c3=c1^c2;
cout<<"^ operator overloaded using friend function:\n";
cout<<"C1: ";
c1.disp();
cout<<"\nC2: ";
c2.disp();
cout<<"\nC3: ";
c3.disp();
cout<<endl;
return 0;
}
Output:
Aim: To demonstrate the use of static members.
Source:
#include<iostream>
using namespace std;
class test
{
int code;
static int count;
public:
void setcode()
{
code=++count;
}
void showcode()
{
cout<<"object number: "<<code<<endl;
}
static void showcount()
{
cout<<"count: "<<count<<"\n";
}
};
int test::count;
int main()
{
test t1,t2;
t1.setcode();
t2.setcode();
test::showcount();
test t3;
t3.setcode();
test::showcount();
t1.showcode();
t2.showcode();
test::showcount();
t1.showcode();
t2.showcode();
t3.showcode();
return 0;
}
Output:
Aim: To concatenate two strings.
Source:
#include<iostream>
#include<string.h>
using namespace std;
class str
{
char *s;
int len;
public:
str()
{
len=0;
s=NULL;
}
str(char *p)
{
len=strlen(p);
s= new char[len+1];
strcpy(s,p);
}
friend str operator+(str,str);
void show()
{
cout<<"String: "<<s<<endl;
}
};
str operator+(str x,str y)
{
str z;
z.len=x.len+y.len;
z.s=new char[z.len+1];
strcpy(z.s,x.s);
strcat(z.s,y.s);
return z;
}
int main()
{
char s1[50],s2[50];
cout<<"Enter string 1:\n";
cin>>s1;
cout<<"Enter string 2:\n";
cin>>s2;
str a(s1),b(s2),c;
cout<<"Final ";
c=a+b;
c.show();
return 0;
}
Output:
Aim: To demonstrate type conversion.
Source:
#include<iostream>
using namespace std;
class A
{
int code;
int qty;
float price;
public:
A(int a,int b,float c)
{
code=a;
qty=b;
price=c;
}
void putdata()
{
cout<<"Code: "<<code<<endl;
cout<<"Qty: "<<qty<<endl;
cout<<"Value: "<<price<<endl;
}
int getcode()
{
return code;
}
int getitems()
{
return qty;
}
float getprice()
{
return price;
}
operator float()
{
return (qty*price);
}
};
class B
{
int code;
float value;
public:
B()
{
code=0;
value=0;
}
B(int x,float y)
{
code=x;
value=y;
}
void putdata()
{
cout<<"Code: "<<code<<"\n";
cout<<"Value: "<<value<<endl;
}
B(A p)
{
code=p.getcode();
value=p.getitems()*p.getprice();
}
};
int main()
{
A s1(100,5,140.0);
B d1;
float total;
total=s1;
d1=s1;
cout<<"Product details Type A:"<<endl;
s1.putdata();
cout<<"\nTotal Stock value: "<<total<<endl;
cout<<"\nProduct details Type B:"<<endl;
d1.putdata();
return 0;
}
Output:
Aim: To implement addition and subtraction of complex numbers
using class.
Source:
#include<iostream>
using namespace std;
class complex
{
float re,img;
public:
complex(float a,float b)
{
re=a;
img=b;
}
complex()
{
re=0;
img=0;
}
complex add(complex c1,complex c2)
{
complex c;
c.re=c1.re+c2.re;
c.img=c1.img+c2.img;
return c;
}
complex subtract(complex c1,complex c2)
{
complex c;
c.re=c1.re-c2.re;
c.img=c1.img-c2.img;
return c;
}
void display()
{
cout<<"Complex Number: "<<re;
if(img>0)
cout<<" + "<<img<<"i"<<endl;
else
cout<<" - "<<-img<<"i"<<endl;
}
};
int main()
{
float x,y;
cout<<"Enter real and imaginary parts of complex 1:\n";
cin>>x>>y;
complex c1(x,y);
cout<<"Enter real and imaginary parts of complex 2:\n";
cin>>x>>y;
complex c2(x,y);
complex ca,cs;
cout<<"After addition:\n";
ca=ca.add(c1,c2);
ca.display();
cout<<"After subtraction:\n";
cs=cs.subtract(c1,c2);
cs.display();
return 0;
}
Output:
Aim: To demonstrate the use of virtual function.
Source:
#include<iostream>
#include<math.h>
using namespace std;
class shape
{
public:
virtual float area()
{
return 0.0;
}
virtual float perimeter()
{
return 0.0;
}
virtual void getdata()
{
}
};
class point : public shape
{
int x,y;
public:
void getdata()
{
cout<<"Enter Coordinates: ";
cin>>x>>y;
}
};
class line : public shape
{
int length;
public:
void getdata()
{
cout<<"Enter length of the line: ";
cin>>length;
}
float perimeter()
{
return length;
}
};
class triangle : public shape
{
int s1,s2,s3;
float s;
public:
void getdata()
{
cout<<"Enter the sides of triangle: ";
cin>>s1>>s2>>s3;
}
float area()
{
s=(s1+s2+s3)/2;
return sqrt(s*(s-s1)+s*(s-s2)+s*(s-s3));
}
float perimeter()
{
return (s1+s2+s3);
}
};
class square : public shape
{
int s;
public:
void getdata()
{
cout<<"Enter the side of square: ";
cin>>s;
}
float area()
{
return (s*s);
}
float perimeter()
{
return (4*s);
}
};
class rectangle : public shape
{
int l,b;
public:
void getdata()
{
cout<<"Enter length and breadth: ";
cin>>l>>b;
}
float area()
{
return (l*b);
}
float perimeter()
{
return (2*(l+b));
}
};
class circle : public shape
{
int r;
public:
void getdata()
{
cout<<"Enter radius: ";
cin>>r;
}
float area()
{
return 3.14*r*r;
}
float perimeter()
{
return 2*3.14*r;
}
};
int main()
{
point p;
line l;
triangle t;
square sq;
rectangle r;
circle c;
shape *s1;
int ch;
do{
cout<<"\n\n1.POINT\n2.LINE\n3.TRIANGLE\n4.SQUARE\n5.RECTANGLE\n6.CIRCLE\n"
<<"7.EXIT\nEnter choice:";
cin>>ch;
switch(ch)
{
case 1: s1=&p;
s1->getdata();
cout<<"Area of point: "<<s1->area()<<endl;
cout<<"Perimeter of point: "<<s1->perimeter()<<endl;
break;
case 2: s1=&l;
s1->getdata();
cout<<"Area of line: "<<s1->area()<<endl;
cout<<"Perimeter of line: "<<s1->perimeter()<<endl;
break;
case 3: s1=&t;
s1->getdata();
cout<<"Area of triangle: "<<s1->area()<<endl;
cout<<"Perimeter of triangle: "<<s1->perimeter()<<endl;
break;
case 4: s1=&sq;
s1->getdata();
cout<<"Area of square: "<<s1->area()<<endl;
cout<<"Perimeter of square: "<<s1->perimeter()<<endl;
break;
case 5: s1=&r;
s1->getdata();
cout<<"area of rectangle is: "<<s1->area()<<endl;
cout<<"perimeter of rectangle is: "<<s1->perimeter()<<endl;
break;
case 6: s1=&c;
s1->getdata();
cout<<"area of circle is: "<<s1->area()<<endl;
cout<<"perimeter of circle is: "<<s1->perimeter()<<endl;
break;
case 7: break;
default: cout<<"INVALID CHOICE\n";
}
}while(ch!=7);
return 0;
}
Output:
Aim: To implement function overloading.
Source:
#include<iostream>
using namespace std;
int vol(int s)
{
return s*s*s;
}
int vol(int l, int b, int h)
{
return l*b*h;
}
float vol(float r, int h)
{
return 3.14*r*r*h;
}
int main()
{
cout<<vol(10)<<"\n"<<vol(2.5,8)
<<"\n"<<vol(100,75,15)<<endl;
return 0;
}
Output: