Professional Documents
Culture Documents
#include<iostream.h>
#include<conio.h>
voidselsort(int a[],intm,int l)
{
inti,j,k;
if(m==1)
{
for(i=0;i<l;i++)
{ for(j=i;j<l;j++)
{ if(a[i]>a[j])
{ k=a[j];
a[j]=a[i];
a[i]=k;
}
}
}
}
else if (m==2)
{ for(i=0;i<l;i++)
{ for(j=i;j<l;j++)
{ if(a[i]<a[j])
{ k=a[j];
a[j]=a[i];
a[i]=k;
}
} }
}
cout<<"\n sorted array is:";
for(i=0;i<l;i++)
cout<<"\t"<<a[i]<<endl;
}
void main()
{
clrscr();
int a[50],i,l,m;
cout<<"enter size of an array";
cin>>l;
cout<<"\n enter array elements";
for(i=0;i<l;i++)
cin>>a[i];
cout<<"\n you want to sort in:\n \t 1.asceding order\n\t 2.descending order";
cin>>m;
selsort(a,m,l);
getch();
}
Output:-
enter size of array 5
enter array element
11
2
23
77
44
You want to sort in
1.ascending order
2.descending order
1
Sorted array is :
2
11
23
44
77
PRG:- write a program to merge two arrays using merge sort?
#include<iostream.h>
#include<conio.h>
voidmrgsort(int a[],int b[],int c[],int s1,int s2)
{
int i=0,j=0,k=0;
while(i<s1 && j<s2)
{ if(a[i]<b[j])
{ c[k]=a[i];
i++;
k++;
}
else
{ c[k]=b[j];
j++;
k++;
}
}
while(i<s1)
{ c[k]=a[i];
k++;
i++;
}
while(j<s2)
{ c[k]=b[j];
j++;
k++;
}
for(k=0;k<s1+s2;k++)
cout<<the sorted array is<<c[k];
}
void main()
{ clrscr();
int a[10],b[10],c[10],m,n;
cout<<"enter length of first array";
cin>>m;
cout<<"\n enter length of second array";
cin>>n;
cout<<"\n enter array elements of first array";
for(int i=0;i<m;i++)
cin>>a[i];
cout<<"\n enter array elements of second array";
for(int j=0;j<n;j++)
cin>>b[j];
mrgsort(a,b,c,m,n);
getch();
}
Output:
Enter length of first array 5
Enter length of second array 4
Enter array elements 1
2
3
4
5
Enter array elements of second array 9
8
7
6
The sorted array is 123456789
PRG:- write a function to find out largest and smallest element
from array?
#include<iostream.h>
#include<conio.h>
void find()
{
int a[3][3],b[3][3],t=0,i,j,s=0;
cout<<"enter elements";
for(i=0;i<3;i++)
{ for(j=0;j,3;j++)
cin>>a[i][j];
}
for(i=0;i<3;++)
{ for(j=0;j<3;j++)
{ if(a[i]>a[j];
t=a[i];
else if(a[i]<a[j])
s=a[i];
}
}
cout<<"\n the largest elements is"<<t;
cout<<"the smalles element is"<<s;
}
void main()
{
clrscr():
find();
getch();
}
Output:
Enter elements 1
29
37
4
75
65
7
84
19
The largest element is 84
The smallest element is 1
PRG:- write a program to print the diagonal sum of matrix?
#include<iostream.h>
#include<conio.h>
voiddigsum(int a[3][3])
{
inti,j,sum=0;
for(i=0;i<3;i++)
{ for(j=0;j<3;j++)
{ if(i==j)
{ sum+=a[i][j];
cout<<sum;
} } }
}
void main()
{
clrscr();
int a[3][3],i,j;
cout<<"enter values of matrix";
for(i=0;i<3;i++);
{ for(j=0;j<3;j++)
cin>>a[i][j];
cout<<"\n the diagonal sum for given matrix is";
digsum(a);
getch();
}
}
Output:-
Enter size of an array
3
3
Enter values of matrix
1
2
3
4
5
6
7
8
9
The diagonal sum for given matrix is 14
PRG:- write a c++ program to reverse a string using pointer ?
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
charstr[80];
char*ptr;
ptr=str;
cout<<"enter a string";
gets(str);
int l;
l=0;
while(*ptr)
{
l++;
ptr++;
}
cout<<"length l="<<l;
getch();
}
Output :-
Enter a string
Programming in c++
Length l=19
PRG:- write a program in c++ to perform pushing in linked stack?
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<process.h>
struct node
{ int info;
node *next;
}
*top,*newptr,*save,*ptr;
node* create_new_node(int);
void push(node*);
void display(node*);
void main()
{
intinf;
charch='y';
top= NULL;
clrscr();
while(ch=='y'||ch=='Y')
{ cout<<"\n enter information for new code";
cin>>inf;
newptr=create_new_node(inf);
if(newptr==NULL)
{ cout<<"\n cannot create new code";
exit(1);
}
push(newptr);
cout<<"\n now the linked stack is\n";
display(top);
cout<<"\n press y to enter more nodes,n to exit";
cin>>ch;
}
}
node* create_new_node(int n)
{ ptr=new node;
ptr->info=n;
ptr->next=NULL ;
returnptr;
}
void push(node* np)
{ if(top==NULL)
top=np;
else
{ save=top;
top=np;
np->next=save;
}
}
void display(node* np)
{
while(np!=NULL)
{ cout<<np->info<<"->";
np=np->next;
}
cout<<endl;
}
Output:-
PRG:- write a program in c++ to perform insertion in linked
queue?
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct node
{ int info;
node *next;
}*front,*newptr,*save,*ptr,*rear;
node* create_new_node(int);
voidinsert_end(node*);
void display(node*);
void main()
{ front=rear=NULL;
intinf;
charch='y';
clrscr();
while(ch=='y'||ch=='Y')
{ cout<<"\n enter information for new node";
cin>>inf;
newptr=create_new_node(inf);
if(newptr==NULL)
{ cout<<"\n cannot create new code";
exit(1);
}
insert_end(newptr);
cout<<"\n now the queue(front to rear) is:\n";
display(front);
cout<<"\n press y to enter more nodes,n to exit";
cin>>ch;
}
}
node* create_new_node(int n)
{ ptr= new node;
ptr->info=n;
ptr->next=NULL ;
}
voidinsert_end(node* np)
{ if(front==NULL)
front=rear=np;
else
{ rear->next=np;
rear=np;
}
}
void display(node* np)
{
while(np!=NULL)
{ cout<<np->info<<"->";
np=np->next;
}
cout<<endl;
}
Output:-
PRG:- write a program to insert at the end of the list?
#include<iostream.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
struct node
{ int info;
node *next;
}*start,*newptr,*save,*ptr,*rear;
node* create_new_node(int);
voidinsert_end(node*);
void display(node*);
void main()
{ start=rear=NULL;
intinf;
charch='y';
clrscr();
while(ch=='y'||ch=='Y')
{ cout<<"\n enter information for new node";
cin>>inf;
cout<<"\n creating new code..press enter to continue";
system("pause");
newptr=create_new_node(inf);
if ( newptr != NULL )
{ cout<<"\n new node created successfully..press enter to continue";
system ("pause");
}
else
{ cout<<"\n cannot create new code";
system ("pause");
exit(1);
}
cout<<"\n now inserting this node in the end of the list\n";
cout<<"press enter to continue";
system("pause");
insert_end(newptr);
cout<<"\n now the list is:\n";
display(start);
cout<<"\n press y to enter more nodes,n to exit";
cin>>ch;
}
}
node* create_new_node(int n)
{
ptr=new node;
ptr->info=n;
ptr->next=NULL ;
returnptr;
}
voidinsert_end(node* np)
{ if(start==NULL)
start=rear=np;
else
{ rear->next=np;
rear=np;
}
}
void display(node* np)
{
while(np!=NULL)
{ cout<<np->info<<"->";
np=np->next;
}
cout<<endl;
}
Output :-
PRG:-write a program to traverses a list to print all the elements of
it?
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct node
{ int info;
node *next;
}*start,*newptr,*save,*ptr,*rear;
node* create_new_node(int);
void insert(node*);
void traverse(node*);
void main()
{ start=rear=NULL;
intinf;
charch='y';
clrscr();
while(ch=='y'||ch=='Y')
{ cout<<"\n enter information for new node";
cin>>inf;
newptr=create_new_node(inf);
if (newptr==NULL)
{ cout<<"\n cannot create new code";
exit(1);
}
insert(newptr);
cout<<"\n press y to enter more nodes,n to exit";
cin>>ch;
}
cout<<"\n the list now is:";
traverse(start);
system("pause");
}
node* create_new_node(int n) //function to create new node dynamically
{
ptr=new node;
ptr->info=n;
ptr->next=NULL ;
returnptr;
}
voidinsert_end(node* np)
{ if(start==NULL)
start=rear=np;
else
{ rear->next=np;
rear=np;
}
}
void traverse(node* np)
{
while(np!=NULL)
{ cout<<np->info<<"->";
np=np->next;
}
cout<<endl;
}
Output:-
PRG:- Write a program to calculate net pay for an employee, apart
from the normal pay, we may also need to get additional pay by
working overtime ,thus the next pay will be the sum of the normal
pay using hybrid inheritance?
#include<iostream.h>
#include<conio.h>
classemp_det
{
protected:
intemp_id;
public:
voidget_id()
{
cout<<"\n enter employee id number";
cin>>emp_id;
}
voiddisp_id()
{
cout<<"\n empid="<<emp_id;
}
};
classnorm_pay:public virtual emp_det
{
protected:
floatbpay;
public:
voidget_bpay()
{
cout<<"\n enter bsic pay";
cin>>bpay;
}
voiddisp_bpay()
{
cout<<"\n basic pay="<<bpay;
}
};
classadd_pay:public virtual emp_det
{
protected:
inthrs,rp,ap;
public:
voidget_addpay()
{
cout<<"enter overtime hours"<<endl;
cin>>hrs;
cout<<"enter how much rupees per hour"<<endl;
cin>>rp;
}
voiddisp_addpay()
{
ap=hrs*rp;
cout<<"\ntotal overtime hours"<<hrs<<"\nadditional pay"<<ap;
}
};
classnet_pay:publicnorm_pay,publicadd_pay
{
intnetpay;
public:
void display()
{
netpay=ap+bpay;
cout<<"\nnet pay is"<<netpay;
}
};
void main()
{
clrscr();
net_payobj;
obj.get_id();
obj.get_bpay();
obj.get_addpay();
obj.disp_id();
obj.disp_bpay();
obj.disp_addpay();
obj.display();
getch();
}
Output :-
Empid is=1101
Basic pay= 10000
Total overtime hours=10
Additional pay= 500
Net pay is 10500
PRG:- consider a class NETWORK where class MASTER derives
information from both account and ADMIN class which in turn
derive information from the class PERSON. Define all the four
classes and write a program to create and display the information
contained in master object ?
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class person
{
int code;
char name[20];
public:
void read()
{
cout<<"\n enter the code";
cin>>code;
cout<<"\n enter name";
gets(name);
}
void display()
{
cout<<"\n the code="<<code;
cout<<"\n name=";
puts(name);
}
};
classaccount:public virtual person
{
float pay;
public:
voidread_acc()
{
cout<<"\n enter pay";
cin>>pay;
}
voiddisp_acc()
{
cout<<"\n the pay amount is="<<pay;
}
};
classadmin:public person
{
intexp;
public:
voidread_adm()
{
read();
cout<<"\nenter the experience"<<endl;
cin>>exp;
}
voiddisp_adm()
{
display();
cout<<"\nthe experience"<<exp;
}
};
classmaster:publicaccount,public admin
{
public:
voidread_master()
{
read_acc();
read_adm();
}
voiddisplay_master()
{
disp_acc();
disp_adm();
}
};
void main()
{
clrscr();
masterobj;
obj.read_master();
obj.display_master();
getch();
}
Output:-
Pay=10000
Code=1717
Name= bharti
Experience=8
PRG:-write a c++ program that implements the concept of multi
level inheritance?
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class employee
{
protected:
intempcode;
charempname[20];
charempdesig[15];
floatempsalary;
public:
voidempinput(void)
{
cout<<"\n enter employee information:enter code:";
cin>>empcode;
cout<<"\n enter name";
gets(empname);
cout<<"\nenter designation";
gets(empdesig);
cout<<"\nenter salary";
cin>>empsalary;
}
voidempdisplay(void)
{
cout<<"\nhere is the employee information"<<empcode;
cout<<"=";
cout<<"\n code:"<<empcode<<"\n name"<<empname<<"\n
designation:"<<empdesig;
cout<<"\n salary:"<<empsalary;
}
};
classcustomer:public employee
{
intcustcode;
charcustname[30];
charcustdesig[30];
double balance;
public:
voidcustinput()
{
cout<<"\n enter customer information:enter code";
cin>>custcode;
cout<<"\n enter name";
gets(custname);
cout<<"enter designation:";
gets(custdesig);
cout<<"enter balance:";
cin>>balance;
}
voidcustdisplay(void)
{
cout<<"\n here is customer information=";
cout<<"=";
cout<<"\n code:"<<custcode<<"\n name:"<<custname;
cout<<"\n designation:"<<custdesig<<"\n balance:"<<balance;
}
};
classempcust:public customer
{
public:
voidget_data(void)
{
empinput();
custinput();
}
voidshow_data(void)
{
empdisplay();
custdisplay();
}
};
void main()
{
clrscr();
empcust e1;
e1.get_data();
e1.show_data();
getch();
}
Output:-
Enter employee information: enter code=100
Name:Preeti
Salary:16500
Code: 500
Name: harsha
Designation: clerk
Salary :45000
TABLE:SELLER
SID SNAME SADDRESS SCITY
ND01 R singhal 24ABC New delhi
enclave
ND02 Anil kumar 123 palm New delhi
avene
MU15 R kholi 5/a south Mumbai
street
MU50 S kanr 27-k-westend Mumbai
TABLE: BUYER
BID SID BNAME BADDRESS BCITY
MU05 ND01 Rahul Kishor 5 park avene Mumbai
PUBLIC MEMBERS:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class garments
{
chargcode[20];
chargtype[20];
intgsize;
chargfabric[20];
floatgprice;
void assign()
{
if(strcmp(gfabric,"cotton")==0)
{ if(strcmp(gtype,"trouser")==0)
gprice=1300;
else if(strcmp(gtype,"shirt")==0)
gprice=1100;
}
else
{
if(strcmp(gtype,"trouser")==0)
gprice=1300-0.10*1300;
if(strcmp(gtype,"shirt")==0)
gprice=1100-0.10*1100;
}
}
public:
garments() //default constructor
{
strcpy(gcode,"not alloted");
strcpy(gtype,"not alloted");
strcpy(gfabric,"not alloted");
gsize=0;
gprice=0;
}
void input()
{
cout<<"\n enter garament code";
cin>>gcode;
cout<<"\n enter garment type(troser/tshirt)";
gets(gtype);
cout<<"\n enter garment size";
cin>>gsize;
cout<<"\n enter garment fabric";
gets(gfabric);
assign();
}
void display()
{
cout<<"\n garment code="<<gcode<<"\n garment type="<<gtype;
cout<<"\n garment size"<<gsize<<"\n garment fabric"<<gfabric;
cout<<"\n garment price"<<gprice;
}
};
void main()
{
clrscr();
garments g1;
g1.input();
g1.display();
getch();}
PRG:Define a class travel in c++with the description given below
PRIVATE MEMBERS
Tcode char
Adults integer
Children integer
Distance integer
Totalfare float
PUBLIC MEMBERS
A constructor to assign initial values
Tcode with the wordNULL
Adults=0
Children=0
Distance=0
Totalfare=0
A function assignfare()to assign the values of the data member
totalfare as follows:
500 >=1000
200 <1000&>=500
300 <500
For each child the above fare will be 50% of the fare mentioned
above
A function enter travel() to accept the data members
And invoke function assignfare().
A function showtravel() to display the content of the data
members.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class travel
{
chartcode[5];
int adults;
int children;
int distance;
floattotalfare;
public:
travel() //constructor
{ strcpy(tcode,"null");
adults=children=distance=totalfare=0;
}
voidassignfare()
{
float fare=0;
if(distance>=1000)
fare=adults*500+children*250;
else if(distance>=500)
fare=adults*300+children*150;
else
fare=adults*200+children*100;
totalfare=fare;
}
voidentertravel()
{
cout<<"\nenter value of travel code";
cin>>tcode;
cout<<"\nenter number of adults";
cin>>adults;
cout<<"\n enter number of children";
cin>>children;
cout<<"\n enter distance";
cin>>distance;
assignfare();
}
voidshowtravel()
{
cout<<"\ntravel code="<<tcode<<"\n
adults="<<adults<<"\nchildren="<<children;
cout<< "\n distance="<<distance<<"\n totalfare="<<totalfare;
}
};
void main()
{
clrscr();
travel t1;
t1.entertravel();
t1.showtravel();
getch();
}
Output
Output:-