Anda di halaman 1dari 29

CPP LAB PROGRAMS

1 Write A menu driven program to perform following operations on matrix :


a) Sum of rows
b) Sum of columns
c) Exit

PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<process.h>
class sum
{
int a[10][10],m,n,i,j,rsum,csum;
public:
void input();
void rowsum();
void colsum();
};
void sum::input()
{
cout<<"enter the order of matrix";
cin>>m>>n;
cout<<"enter array elements";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
}
void sum::rowsum()
{
for(i=0;i<m;i++)
{
rsum=0;
for(j=0;j<n;j++)
{
rsum=rsum+a[i][j];
}
cout<<"the row"<<i<<"sum="<<rsum;
}
}
void sum::colsum()
{
for(j=0;j<n;j++)
{
csum=0;
for(i=0;i<m;i++)
{
csum=csum+a[i][j];
}
cout<<"the col"<<j<<"sum="<<csum;
}

}
void main()
{
int ans;
sum s;
s.input();
while(1)
{
cout<<"press 1. for rsum 2. for csum 3. for exit";
cin>>ans;
switch(ans)
{
case 1: s.rowsum();
break;
case 2: s.colsum();
break;
case 3: exit(0);
break;
default: cout<<"invalid choice";
}
}
}

OUTPUT:

2.Write amenu driven program which allows user to perform following function
a)To accept Name & Mark
b)Finding Highest Marks.
c)Finding Lowest Marks.
d) Display All Marks.
PROGRAM:
#include<iostream.h>
#include<process.h>
#include<conio.h>
#include<iomanip.h>
class marks
{
int sub1,sub2,high,low,a[10],n,i;
char name[20];
public:
void input();
void highest();
void lowest();
void display();
};
void marks :: input()
{
cout<<"enter the name";
cin>>name;
cout<<"enter nor of subs"<<endl;
cin>>n;
cout<<"enter the marks"<<endl;
for(i=0;i<n;i++)
cin>>a[i];
}
void marks :: highest()
{
high=0;
for(i=0;i<n;i++)
if(a[i]>high)
{
high=a[i];
}
cout<<"highest marks is "<<high<<endl;
}
void marks :: lowest()
{
low=100;
for(i=0;i<n;i++)
{
if(a[i]<low)
{
low=a[i];
}
}
cout<<"lowest marks is"<< low<<endl;
}
void marks :: display()
{
cout<<name<<endl;
for(i=0;i<n;i++)
cout<<a[i]<<endl;
}
void main()
{
clrscr();
marks m;
int ans;
m.input();
while(1)
{
cout<<endl<<"press 1 highest.2 lowest. 3 output .4 exit.";
cin>>ans;
switch (ans)
{
case 1: m.highest();
break;
case 2: m.lowest();
break;
case 3 : m.display();
break;
case 4: exit(0);
default: cout<<"invalid choice";
}
}
}

OUTPUT:

3.Write a menu driven program to perform following function using class:


a) TO store 10- Employee Details
private :
emp_no of type integer.
emp_name of type string
emp_salary type floating point.
Public:
a) Input()---to get employee details
show ()--- to display employee details
b) Display employee details in tabular form
c) Exit

PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<process.h>
class employe
{
int empno;
char empname[30];
float empsalary;
public:
void input();
void show();
void display();
};
void employe :: input()
{
cout<<"enter the employe number";
cin>>empno;
cout<<"enter the employe name";
cin>>empname;
cout<<"enter the employe salary";
cin>>empsalary;
}
void employe :: show()
{
cout<<empno<<setw(5);
cout<<empname<<setw(5);
cout<<empsalary<<endl;
}
void employe :: display()
{

cout<<empno<<"\t"<<empname<<"\t"<<empsalary<<"\t"<<endl;
}
void main()
{
int ans,i;
employe e[10];
while(1)
{
cout<<"press 1.input 2.show 3.display 4.exit";
cin>>ans;
switch (ans)
{
case 1 : for(i=0;i<3;i++)
e[i].input();
break;
case 2 : for(i=0;i<3;i++)
e[i].show();
break;
case3: cout <<" empno \t empname \t
empsalary "<< endl;
for(i=0;i<3;i++)
e[i].display();
break;
case 4: exit(0);
default: cout<<"invalid choice";
}
}
}

OUTPUT:

4.Define a class flight in c++ with following description :


[classes and objects]
Private Members:
 Flight _no-of type integer.
 Destination -of type string.
 Distance - of type float.
 Fuel_of the type float.
A member function CALFUEL() to calculate the value of fuel
as per the following criteria.

Distance Fuel
<> 1000 5000
>1000 &>2000 1100
>2000 2200

Public Members:
 A function freed info() to allow user to enter values
for flight.
Number destination distance & call function calfuel()
to calculate the quantity of fuel.
 A function showinfo() to allow user to view the
content of all the Data members
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class flight
{
int fno;
char destini[20];
float distance,fuel;
public:
void info()
{
cout <<"enter the flight nor";
cin>>fno;
cout<<"enter the destination";
cin>>destini;
cout<<"enter the distance";
cin>>distance;
}
void calc()
{
if(distance<=1000)
fuel=500;
else if(distance>1000 && distance<=2000)
fuel=1100;
else
fuel=2200;
}
void showinfo()
{
cout<<"flight nor="<<setw(5)<<fno<<endl;
cout<<"destination="<<setw(5)<<destini<<endl;
cout<<"distance="<<setw(5)<<distance<<endl;
cout<<"fuel="<<setw(5)<<fuel;
}
};
void main()
{
clrscr();
flight f;
cout<<sizeof(f);
f.info();
f.calc();
f.showinfo();
getch();
}

OUTPUT:
5. Create a class student with the following :
Private:
S.RO - of the type integer.
S.NAME-of the type string.
FEES. of the type float.
Public:
To read and display data using pointers to objects.

PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class student
{
int rno;
char name[20];
float fee;
public:
void read();
void display();
};
void student :: read()
{
cout<<"enter the rno of student";
cin>>rno;
cout<<"enter the name of student"<<endl;
cin>>name;
cout<<"enter the fee of student"<<endl;
cin>>fee;
}
void student::display()
{
cout<<"rno="<<rno<<"name="<<name<<"fee="<<fee;
}
void main()
{
student s,*s1;
s1=&s;
s1->read();
s1->display();
getch();
}

OUTPUT:
6. DEMONSTRATE MEMORY OCCUPIED BY BASE AND DERIVED CLASS
OBJECTS BY IMPLEMRNTING MULTILEVEL INHERITANCE

// PROGRAM TO IMPLEMENT MULTILEVEL INHERITANCE.


#include<iostream.h>
#include<conio.h>

class person
{
char name[20];
protected:
char address[30];
public:
int phone;
};

class student:public person


{
int roll;
char stream[20];
};
class c_student:public student
{
int sub_code;
protected:
char s_college[20];
public:
char book_name[20];
int book_code;
};

void main()
{
clrscr();
cout<<"========================================================";
cout<<"\t\tDisplaying size of base and derived class object";
cout<<"\n=======================================================";
person p;
student s;
c_student s1;
cout<<"\n\n\tSize of person class: "<<sizeof(person);
cout<<"\n\n\tSize of student class: "<<sizeof(student);
cout<<"\n\n\tSize of c_student class: "<<sizeof(c_student);
cout<<"\n\n\tSize of object of person class: "<<sizeof(p);
cout<<"\n\n\tSize of object of student class: "<<sizeof(s);
cout<<"\n\n\tSize of object of person class: "<<sizeof(s1);
getch();
}

7. Write a based on user choice program to perform the following search operations :
a) Linear search
b) Binary search
c) Display all the elements.
d) Exit.
PROGRAM:
#include<iostream.h>
#include<process.h>
#include<conio.h>
#include<iomanip.h>
class search
{
int a[100],b,e,m,n,i,ele,pos;
public:
void linearsearch();
void binarysearch();
void output();
};
void search::linearsearch()
{
cout<<"enter the array size";
cin>>n;
cout<<"enter the array elements"<<endl;
for(i=0;i<n;i++)
cin>>a[i];
cout<<"enter the search element";
cin>>ele;
pos=-1;
for(i=0;i<n;i++)
{
if(a[i]==ele)
{
pos=i;
break;
}
}
output();
}

void search::binarysearch()
{
cout<<"enter the array size";
cin>>n;
cout<<"enter the array elements in sorted format"<<endl;
for(i=0;i<n;i++)
cin>>a[i];
cout<<"enter the search element";
cin>>ele;
pos=-1;
b=0,e=n-1;
while(b<=e)
{
m=(b+e)/2;
if(a[m]==ele)
{
pos=m;
break;
}
if(ele<a[m])
e=m-1;
else
b=m+1;
}
output();
}
void search::output()
{
if(pos>=0)
cout<<ele<<"found at pos"<<pos;
else
cout<<"element not found";
}
void main()
{
int ans;
search s;
cout<<"press 1 for linear search,press 2 for binary search, press 3 for exit";
cin >>ans;
switch(ans)
{
case 1: s.linearsearch();
break;
case 2: s.binarysearch();
break;
case 3: exit(0);
default:"invalid choice";
}
getch();
}
OUTPUT:

8. Write a based on use choice program to perform do the following


operations using Array:
a) insert an element into array at a given position .
b) Delete an element from an array from a given position.
c) Display All elements.
d) Exit.

PROGRAM:
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
#include<process.h>
class ak
{
int a[100],n,i,ele,pos;
public:
void input()
{
cout<<"enter array size";
cin>>n;
cout<<"enter array elements";
for(i=0;i<n;i++)
cin>>a[i];
}
void insertion()
{
cout<<"enter ele and pos to insert";
cin>>ele>>pos;
for(i=n-1;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=ele;
n++;
}
void deletion()
{
cout<<"enter pos for delete";
cin>>pos;
for(i=pos;i<=n-1;i++)
a[i]=a[i+1];
n--;
}
void display()
{
cout<<"array elements are";
for(i=0;i<n;i++)
cout<<a[i];
}
};
void main()
{
int ans;
ak e;
e.input();
while(1)
{
cout<<"enter '1' for insert"<<endl;
cout<<"enter '2' for delete"<<endl ;
cout<<"enter '3' for display"<<endl ;
cout<<"enter '4' to exit"<<endl;
cin>>ans;
switch(ans)
{
case 1: e.insertion();
break;
case 2:e.deletion();
break;
case 3:e.display();
break;
case 4: exit(0);
default:cout<<"invalid entery";
}
}
}

OUTPUT:
9.Write a menu driven program to perform the following sorting operations

a) Enter elements into array.


b) Insertion sort
c) Display all the elements.
d) Exit.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class insertion
{
int a[10],i,n,j,temp;
public:
void input();
void process();
void display();
};
void insertion::input()
{
cout<<"enter the array size";
cin>>n;
cout<<"enter array elements";
for(i=0;i<n;i++)
cin>>a[i];
}
void insertion::process()
{
for(i=1;i<n;i++)
{
j=i;
while(j>=1)
{
if(a[j]<a[j-1])
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
j--;
}
}
}
void insertion::display()
{
cout<<"array after sorting";
for(i=0;i<n;i++)
cout<<a[i]<<endl;
}
void main()
{
clrscr();
insertion i;
i.input();
i.process();
i.display();
getch();
}
OUTPUT:

10. Write a menu driven program to sort the elements of array using bubble sort:

a) Enter elements into array


b) Binary sort.
c) Display all elements
d) Exit

PROGRAM:

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class bubble
{
int a[10],i,n,j,temp;
public:
void input();
void process();
void display();
};
void bubble::input()
{
cout<<"enter the array size";
cin>>n;
cout<<"enter array elements";
for(i=0;i<n;i++)
cin>>a[i];
}
void bubble::process()
{
for(i=1;i<n;i++)
for(j=0;j<n-i;j++)
{
if(a[j]<a[j-1])
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
}
}
void bubble::display()
{
cout<<"array after sorting";
for(i=0;i<n;i++)
cout<<a[i]<<endl;
}
void main()
{
clrscr();
bubble b;
b.input();
b.process();
b.display();
getch();
}

OUTPUT:

11. Write a menu driven program to perform following operations on stack using array:

a)push elements to stack

b)pop element from stack

c)print elements of stack.

PROGRAM:
#include<iostream.h>

#include<conio.h>

#include<process.h>

#define max 3
class stack

private:

int a[max],top;

public:

stack()

top=-1;

void push(int item);

void pop();

void print();

};

void stack :: push(int item)

if(top== max-1)

cout<<"sorry stack is full"<<endl;

top++;

a[top]=item;

cout<<item <<"is successfully pushed"<<endl;

void stack :: print()

{
if(top!= -1)

cout<<"stack contains"<<endl;

for(int i=0;i<=top;i++)

cout<<a[i]<<endl;

else

cout<<"stack is empty"<<endl;

void stack :: pop()

if(top== -1)

cout<<"sorry stack is empty";

else

int ele=a[top];

top--;

cout<<ele<<"is successfully popped";

void main()

stack s;
int choice,item;

while(1)

cout<<"\n 1. push \n 2.pop \n 3.print \n 4.exit"<<endl;

cout<<"enter your choice "<<endl;

cin>>choice;

switch(choice)

case 1: {

cout<<"enter the item ";

cin>>item;

s.push(item);

break;

case 2: {

s.pop();

break;

case 3: {

s.print();

break;

case 4: {

cout<<"thank you... visit again";

exit(0);
}

default : cout<<"invalid choice";

output:

12. Write a menu driven program to perform following operations using function
overloading:

a)Find area of square.

b)Find area of rectangle.

c)Find area of triangle.

d)Exit.

PROGRAM:
#include<iostream.h>

#include<conio.h>

#include<process.h>

#include<math.h>

class funoverload

public:

int area(int a)
{

return (a* a);

int area(int a,int b)

return(a*b);

double area(int a,int b,int c)

double s;

s=(a+b+c)/2.0;

return(sqrt(s*(s-a)*(s-b)*(s-c)));

};

void main()

funoverload f;

int choice, a, b, c;

while(1)

cout<<"\n 1. find area of square \n 2.find area of rectagle \n 3.find area of


triangle\n 4. exit"<<endl;

cout<<"enter your choice "<<endl;

cin>>choice;
switch(choice)

case 1: {

cout<<"enter a value ";

cin>>a;

cout<<"area is"<<f.area(a);

break;

case 2: {

cout<<"enter two sides";

cin>>a>>b;

cout<<"area is"<<f.area(a,b);

break;

case 3:{

cout<<"entre three sides";

cin>>a>>b>>c;

cout<<"area is"<<f.area(a,b,c);

break;

case 4: {

cout<<"thank you... visit again";

exit(0);

default : cout<<"invalid choice";

}
}

output:

13. Write a menu driven program to perform following operations on stack using array:

a)push elements to stack

b)pop elements from stack

c)print elements of stack.

d)exit

#include<iostream.h>

#include<conio.h>

#include<process.h>

#define max 3

class stack

private:

int a[max],top;

public:

stack()

top=-1;

}
void push(int item);

void print();

void pop( );

};

void stack :: push(int item)

if(top== max-1)

cout<<"sorry stack is full"<<endl;

top++;

a[top]=item;

cout<<item <<"is successfully pushed"<<endl;

void stack :: pop( )

if(top== -1)

cout<<"sorry stack is empty"<<endl;

item=stack[top];

top--;

cout<<item <<"is successfully deleted"<<endl;

}
void stack :: print()

if(top!= -1)

cout<<"stack contains"<<endl;

for(int i=0;i<=top;i++)

cout<<a[i]<<endl;

else

cout<<"stack is empty"<<endl;

void main()

stack s;

int choice,item;

while(1)

cout<<"\n 1. push \n 2.print \n 3. pop \n 4.exit"<<endl;

cout<<"enter your choice "<<endl;

cin>>choice;

switch(choice)

case 1: {

cout<<"enter the item ";

cin>>item;
s.push(item);

break;

case 2: {

s.print();

break;

case 3:{

s.pop( );

break;

case 4: {

cout<<"thank you... visit again";

exit(0);

default : cout<<"invalid choice";

output:
14. Write a program to perform the following operation on queue using linked list:

a)Enqueue

b)Print elements of queue.

c)Exit.

// PROGRAM TO PERFORM ENQUEUE AND DEQUEUE.


#include<iostream.h>
#include<conio.h>
#include<process.h>

struct node
{
int data;
node *next;
}*front,*rear;

class queue
{
public:
queue()
{
front =NULL;
rear=NULL;
}
void enqueue(int a);
void display();
};

void queue::enqueue(int a)
{
node *t=new node;
t->next=NULL;
t->data=a;
if(front==NULL)
front=rear=t;
else
{
rear->next=t;
rear=t;
}
}
void queue::display()
{
node *t=new node;
t=front;
clrscr();
cout<<"\n\n\t QUEUE\n\n\t";
while(t!=NULL)
{
cout<<t->data<<" ";
t=t->next;
}
getch();
}

void main()
{
queue q;
int ans1,ans2;
char ch;
do
{
clrscr();
cout<<"\n\t QUEUES"
<<"\n\n\t1. Add nodes"
<<"\n\n\t2. View list"
<<"\n\n\t3. Exit";
cin>>ans1;
switch(ans1)
{
case 1:
clrscr();
cout<<"\n\t ADDING NODE";
do
{
cout<<"\n\t Enter queue element : ";
cin>>ans2;
q.enqueue(ans2);
cout<<"\n\t Do you wish to add more (y/n) ? : ";
cin>>ch;
}
while(ch=='y'||ch=='Y');
break;

case 2:
q.display();
break;
case 3:
exit(0);
break;
}
}
while(ans1!=3);
}

Anda mungkin juga menyukai