1 Menu Driven Program of Array
1 Menu Driven Program of Array
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,A[10][10],B[10][10],n,temp,a;
cout<<"enter 1 for transpose of matrix \n enter 2 for lower triangle\nenter 3 for
upper triangle of matrix";
cout<<"\nenter your choice";
cin>>a;
cout<<"enter the no of elements";
cin>>n;
for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
cin>>A[i][j];}
switch(a)
{
case 1:for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
B[i][j]=A[j][i]; }
cout<<" "<<B[i][j];
}
break;
case 2:for(i=0;i<n;i++)
for(j=0;j<n;j++)
{ B[i][j]=A[i][j]; }
cout<<"lower triangle of matrix";
for (i=0;i<n;i++)
{for(j=0;j<n;j++)
{ if(i>=j)
break;
case 3:for(i=0;i<n;i++)
for(j=0;j<n;j++)
{ B[i][j]=A[i][j];}
cout<<"upper traingle of matrix";
for (i=0;i<n;i++)
{for(j=0;j<n;j++)
{ if(i<=j)
cout<<B[i][j]<< " ";
else
cout<<" ";
}
cout<<"\n";
}
break;
} getch(); }
#include<iostream.h>
#include<stdio.h>
#include<process.h>
int linearsearch(int a[20],int key,int n);
case 2:
cout<<"Enter the elements\n";
for(i=0;i<n;i++){
cin>>a[i];
}
cout<<"Enter the key elememts\n";
cin>>k;
ans=binarysearch(a,k,n);
if(ans!=-1)
cout<<"element\t"<<k<<"\tis found\n";
else
cout<<"Element\t"<<k<<"\tnot found\n";
break;
case 3:
exit(0);
default:
cout<<"Invalid entry\n";
}
}
}
int linearsearch(int a[20],int key,int n)
{
int i;
for(i=0;i<n;i++){
if(key==a[i])
return i;
}
return -1;
}
int binarysearch(int a[20],int key,int n)
{
int high,low,mid;
low=0;
high=n-1;
while(low<=high){
mid=(low+high)/2;
if(a[mid]==key)
return mid;
else if(a[mid]<key)
low=mid+1;
else
high=mid-1;
}
return -1; }
3 selection sort
#include<iostream.h>
void selsort(int[],int);
int main()
{ int a[10],item,n,i,index;
cout<<"enter the no of elements";
cin>>n;
cout<< "\nenter array elements";
for(i=0;i<n;i++)
cin>>a[i];
selsort(a,n);
cout<<"\n the sorted array ";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}
void selsort (int a[],int size)
{ int small,pos,tmp;
for(int i=0;i<size-1;i++)
{small=a[i];
pos=i;
for(int j=i+1;j<size;j++)
{ if (a[j]<small)
{ small=a[j];pos=j;}
}
tmp=a[i];
a[i]=a[pos];
a[pos]=tmp;
cout<<"\n array after pass"<<i+1<<"is:";
for(j=0;j<size;j++)
cout<<a[j]<<" ";} }
4 bubble sort
#include<iostream.h>
void bubblesort(int[],int);
int main()
{ int a[10],item,n,i,index;
cout<<"enter the no of elements";
cin>>n;
cout<< "\nenter array elements";
for(i=0;i<n;i++)
cin>>a[i];
bubblesort(a,n);
cout<<"\n the sorted array ";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}
void bubblesort (int a[],int size)
{ int tmp,ctr=0;
for(int i=0;i<size;i++)
{for(int j=0;j < (size-1)-i;j++)
if (a[j]>a[j+1])
{ tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}
}
for(int k=0;k<size;k++)
cout<<a[k]<<" ";
cout<<endl;
}
Insert sort
#include<iostream.h>
#include<limits.h>
void inssort(int[],int);
int main()
{ int a[10],item,n,i,index;
cout<<"enter the no of elements";
cin>>n;
cout<< "\nenter array elements";
for(i=1;i<=n;i++)
cin>>a[i];
inssort(a,n);
cout<<"\n the sorted array ";
for(i=1;i<=n;i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}
void inssort (int a[],int size)
{ int tmp,j;
a[0]=INT_MIN;
for(int i=1;i<=size;i++)
{ tmp= a[i];
j=i-1;
while (tmp<a[j])
{ a[j+1]=a[j];
j--;
}
a[j+1]=tmp;
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#define pi 3.14
class fn
{
public:
void area(int); //circle
void area(int,int); //rectangle
void area(float ,int,int); //triangle
};
void fn::area(int a)
{
cout<<"Area of Circle:"<<pi*a*a;
}
void fn::area(int a,int b)
{
cout<<"Area of rectangle:"<<a*b;
}
void fn::area(float t,int a,int b)
{
cout<<"Area of triangle:"<<t*a*b;
}
void main()
{
int ch;
int a,b,r;
clrscr();
fn obj;
cout<<"\n\t\tFunction Overloading";
cout<<"\n1.Area of Circle\n2Area of Rectangle\n3.Area of Triangle\n4.Exit\n";
cout<<"Enter your Choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter Radius of the Circle:";
cin>>r;
obj.area(r);
break;
case 2:
cout<<"Enter Sides of the Rectangle:";
cin>>a>>b;
obj.area(a,b);
break;
case 3:
cout<<"Enter Sides of the Triangle:";
cin>>a>>b;
obj.area(0.5,a,b);
break;
case 4:
exit(0);
}
getch();
}
include <iostream.h>
class Line
{
public:
void setLength( double len );
double getLength( void );
Line(); // This is the constructor declaration
~Line(); // This is the destructor: declaration
private:
double length;
};
Line::Line(void)
{
cout << "Object is being created" << endl;
}
Line::~Line(void)
{
cout << "Object is being deleted" << endl;
}
return 0;
}
8 Class
#include <iostream.h>
class Box
{
public:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
int main( )
{
Box Box1;
Box Box2;
// box 1 specification
Box1.height = 5.0;
Box1.length = 6.0;
Box1.breadth = 7.0;
// box 2 specification
Box2.height = 10.0;
Box2.length = 12.0;
Box2.breadth = 13.0;
// volume of box 1
volume = Box1.height * Box1.length * Box1.breadth;
cout << "Volume of Box1 : " << volume <<endl;
// volume of box 2
volume = Box2.height * Box2.length * Box2.breadth;
cout << "Volume of Box2 : " << volume <<endl;
return 0;
}
concept of inheritance. */
#include <iostream.h>
class Rectangle
{
protected:
float length, breadth;
public:
Rectangle(): length(0.0), breadth(0.0)
{
};
};
}
};
int main()
{
cout<<"Enter data for first rectangle to find area.\n";
Area a;
cout<<"Area = "<<a.calc()<<" square meter\n\n";
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
const int len=25;
class employee{
private:
char name[len];
unsigned long enumb;
public:
void getdata()
{ cout<<"enter name:";
gets(name);
cout<<"enter employee no";
cin>>enumb;
}
void putdata()
{ cout<<"name"<<name<<"\t";
cout<<"emp.no"<<enumb<<"\t";
cout<<"basic salary"<<basic;
}
protected:
float basic;
void getbasic()
{cout<<"enter basic:";cin>>basic;}
};