0% found this document useful (0 votes)
39 views

1 Menu Driven Program of Array

The document contains 10 C++ program examples demonstrating various concepts like arrays, searching algorithms, sorting algorithms, function overloading, classes, constructors, destructors, inheritance and public derivation. The programs include: 1) A menu driven array program, 2) Menu driven linear and binary search programs, 3) Selection sort program, 4) Bubble sort program, 5) Insertion sort program, 6) Function overloading example to calculate area of different shapes, 7) Class with constructor and destructor, 8) Simple class to calculate volume of boxes, 9) Inheritance example to calculate area and perimeter of rectangles, 10) Public derivation example to inherit from employee class to manager class.

Uploaded by

madina
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

1 Menu Driven Program of Array

The document contains 10 C++ program examples demonstrating various concepts like arrays, searching algorithms, sorting algorithms, function overloading, classes, constructors, destructors, inheritance and public derivation. The programs include: 1) A menu driven array program, 2) Menu driven linear and binary search programs, 3) Selection sort program, 4) Bubble sort program, 5) Insertion sort program, 6) Function overloading example to calculate area of different shapes, 7) Class with constructor and destructor, 8) Simple class to calculate volume of boxes, 9) Inheritance example to calculate area and perimeter of rectangles, 10) Public derivation example to inherit from employee class to manager class.

Uploaded by

madina
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

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<<"transpose of matrix is:";


for(i=0;i<n;i++)
{ cout<<"\n";
for(j=0;j<n;j++)

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)

cout<<B[i][j]<< " ";


else
cout<<" ";
}
cout<<"\n";
}

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(); }

2 Menu driven program of linear and binary search

#include<iostream.h>
#include<stdio.h>
#include<process.h>
int linearsearch(int a[20],int key,int n);

int binarysearch(int a[20],int key,int n);


int main()
{
int a[20],k,n,ch,ans,i;
cout<<"Enter the total number of elements\n";
cin>>n;
while(1){
cout<<"\nMenu\n";
cout<<"1.linear search\n";
cout<<"2.binary search\n";
cout<<"3.exit\n";
cout<<"Enter your choice\n";
cin>>ch;
switch(ch){
case 1: cout<<"enter the no of elements";
for(i=0;i<n;i++){
cin>>a[i];
}
cout<<"Enter the key elememts\n";
cin>>k;
ans=linearsearch(a,k,n);
if(ans!=-1)
cout<<"element\t"<<k<<"\tis found\n";
else
cout<<"Element\t"<<k<<"\tnot found\n";
break;

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;

cout<<"array after pass"<<i<<"_is:";


for(int k=1;k<=size;k++)
cout<<a[k]<<" ";
cout<<endl;} }

#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();
}

7 Class with consdructor and destructor

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;
};

// Member functions definitions including constructor

Line::Line(void)
{
cout << "Object is being created" << endl;
}
Line::~Line(void)
{
cout << "Object is being deleted" << endl;
}

void Line::setLength( double len )


{
length = len;
}

double Line::getLength( void )


{
return length;
}
// Main function for the program
int main( )
{
Line line;

// set line length


line.setLength(6.0);
cout << "Length of line : " << line.getLength() <<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;

// Declare Box1 of type Box

Box Box2;

// Declare Box2 of type Box

double volume = 0.0;

// Store the volume of a box here

// 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;
}

/* C++ Program to calculate the area and perimeter of rectangles using

concept of inheritance. */

#include <iostream.h>

class Rectangle
{
protected:
float length, breadth;
public:
Rectangle(): length(0.0), breadth(0.0)
{

cout<<"Enter length: ";


cin>>length;
cout<<"Enter breadth: ";
cin>>breadth;
}

};

/* Area class is derived from base class Rectangle. */


class Area : public Rectangle
{
public:
float calc()
{
return length*breadth;
}

};

/* Perimeter class is derived from base class Rectangle. */


class Perimeter : public Rectangle
{
public:
float calc()
{
return 2*(length+breadth);

}
};

int main()
{
cout<<"Enter data for first rectangle to find area.\n";
Area a;
cout<<"Area = "<<a.calc()<<" square meter\n\n";

cout<<"Enter data for second rectangle to find perimeter.\n";


Perimeter p;
cout<<"\nPerimeter = "<<p.calc()<<" meter";
return 0;
}

10 Public derivation of class

#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;}
};

class manager:public employee


{ private:
char title[len];
public:
void getdata()
{
employee::getdata();
getbasic();
cout<<"enter title:";
gets(title);
cout<<"\n";
}
void putdata()
{ employee::putdata();
cout<<"\title:"<<title<<"\n";
}
};
int main()
{
system("cls");
manager m1,m2;
cout<<"manager1\n";m1.getdata();
cout<<"manager2\n";m2.getdata();
cout<<"\t\tmanager1 details\n";m1.putdata();
cout<<"\t\tmanager2 details\n";m2.putdata();
return 0;

You might also like