Вы находитесь на странице: 1из 47

Program: Invokes a function to increase the basic salary of an

employee if the employee has experience of 10 or more years


Solution:

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
struct Employee
{ int empno;
char name[25];
float basic;
float experience; };
void display(Employee*emp);
void increase(Employee*emp);
void main()
{ clrscr();
Employee mgr,*eptr;
cout<<"\n\t\t\tEnter Employee No...:";
cin>>mgr.empno;
cout<<"\n\t\t\tEnter Name:-";
gets(mgr.name);

cout<<"\n\t\t\tEnter basic pay:-";


cin>>mgr.basic;
cout<<"\n\t\t\tEnter Experience(in years):-";
cin>>mgr.experience;
eptr= &mgr;
cout<<"\n\t\t\tEmployee Details before increase()...";
display(eptr);
increase(eptr);
cout<<"\n\nEmployee Details after increase()";
display(eptr);
getch();
}
void display(Employee*emp)
{ int len=strlen(emp->name);
cout<<"\nEmloyee NO...:"<<emp->empno;
cout<<"\nName:";
cout.write(emp->name,len);
cout<<"\tBasic:"<< emp->basic;
cout<<"\tExperience:"<<emp->experience<<"Years\n"; }
void increase(Employee*emp)
{ if(emp->experience>=10)
emp-> basic+=200; }

Program: To display the details of the salesman who has made the
maximum total sales.
Solution:
#include<iostream.h>
#include<string.h>
#include<conio.h>
class Salesman
{
char name[10];
float q1_sal,q2_sal,q3_sal,q4_sal,tot_sal;
public:
Salesman()
{
strcpy(name," ");
q1_sal=q2_sal=q3_sal=q4_sal=tot_sal=0;
}
void getdata(char*s,float i,float j,float k,float l)
{
strcpy(name,s);
q1_sal=i;q2_sal=j;q3_sal=k;q4_sal=l;
}
void calc_tot()

{
tot_sal=q1_sal+q2_sal+q3_sal+q4_sal;}
char*get_name() { return name;}
float get_q1() { return q1_sal;}
float get_q2() { return q2_sal;}
float get_q3() { return q3_sal;}
float get_q4() { return q4_sal;}
float get_tot() { return tot_sal;}
Salesman*max_sal(Salesman*S)
{
if(!S)S=this;
else
{
float f1,f2;
f1=S->get_tot();
f2=this->get_tot();
if(f1<f2)
S=this;
}
return S;
}
};

Salesman*sp;
void printit(Salesman*sp)
{
cout<<"\n\t\t\tSalesman with Maximum Sales\n\n";
cout<<"NAME:";
char*ss=sp->get_name();
cout.write(ss,11).put('\n');
cout<<"\n\tTotal Sales:"<<sp->get_tot()<<"\n";
}
void main()
{
clrscr();
Salesman Raman,Sita,Vedant,Anubhav,Bina;
float q1,q2,q3,q4;
sp=&Raman;
cout<<"Enter sales in four quarters for EMPLOYEE 1:";
cin>>q1>>q2>>q3>>q4;
Raman.getdata("EMPLOYEE 1",q1,q2,q3,q4);
Raman.calc_tot();
sp=Raman.max_sal(sp);
cout<<"\nEnter sales in four quarters for EMPLOYEE 2:";
cin>>q1>>q2>>q3>>q4;

Sita.getdata("EMPLOYEE 2",q1,q2,q3,q4);
Sita.calc_tot();
sp=Sita.max_sal(sp);
cout<<"\nEnter sales in four quarters for EMPLOYEE 3:";
cin>>q1>>q2>>q3>>q4;
Vedant.getdata("EMPLOYEE 3",q1,q2,q3,q4);
Vedant.calc_tot();
sp=Vedant.max_sal(sp);
cout<<"\nEnter sales in four quarters for EMPLOYEE 4:";
cin>>q1>>q2>>q3>>q4;
Anubhav.getdata("EMPLOYEE 4",q1,q2,q3,q4);
Anubhav.calc_tot();
sp=Anubhav.max_sal(sp);
cout<<"\nEnter sales in four quarters for EMPLOYEE 5:";
cin>>q1>>q2>>q3>>q4;
Bina.getdata("EMPLOYEE 5",q1,q2,q3,q4);
Bina.calc_tot();
sp=Bina.max_sal(sp);
printit(sp);
getch();
}

Program: Linear Search In Array


Solution:

#include<iostream.h>
#include<conio.h>
int Lsearch(int[],int,int);
void main()
{
clrscr();
int AR[50],ITEM,N,index;
cout<<"Enter desired array size(max.50):....";
cin>>N;
cout<<"\nEnter Array elements:";
for(int i=0;i<N;i++)
{
cin>>AR[i];
}
cout<<"\nEnter Elements to be searched for...";
cin>>ITEM;
index=Lsearch(AR,N,ITEM);
if(index==-1)
cout<<"\nSorry!!Given element could not found.\n";

else
cout<<"\n\n\t\tElement found at
index:"<<index<<"\t\tPosition:"<<index+1<<endl;
getch();
}
int Lsearch(int AR[],int size,int ITEM)
{
for(int i=0;i<size;i++)
{
if(AR[i]==ITEM)
return i;
}
return-1;
}

Program: Binary Search In array


Solution:

#include<iostream.h>
#include<conio.h>
int Bsearch(int[],int,int);
void main()
{
clrscr();
int AR[50],ITEM,N,index;
cout<<"Enter desired array size(max 50).......";
cin>>N;
cout<<"\nEnter Array elements:";
for(int i=0;i<N;i++)
cin>>AR[i];
cout<<"\n Enter Element to be searched for:";
cin>>ITEM;
index=Bsearch(AR,N,ITEM);
if(index==-1)
cout<<"\nSorry!!!Given Element could not be found.\n";
else

cout<<"\n\n\tElement found at
index:"<<index<<"\tPosition:"<<index+1<<endl;
getch();
}
int Bsearch(int AR[],int size,int item)
{
int beg,last,mid;
beg=0;
last=size-1;
while(beg<=last)
{
mid=(beg+last)/2;
if(item==AR[mid])
return mid;
else if(item>AR[mid])
beg=mid+1;
else
last=mid-1;
}
return-1;
}

Program: Insertion In Array


Solution:

#include<iostream.h>
#include<conio.h>
#include<process.h>
int FindPos(int[],int,int);
void main()
{
clrscr();
int AR[50],ITEM,N,index;
cout<<"\n\t\tHow many Elements Do you want to create with?....";
cin>>N;
cout<<"\n\t\tEnter Array elements\n";
for(int i=0;i<N;i++)
cin>>AR[i];
char ch='y';
while(ch=='y'||ch=='Y')
{
cout<<"\nEnter Elements to be inserted....";
cin>>ITEM;
if(N==50)

{
cout<<"overflow...!...!!.!!..!!!";
exit(1);
}
index=FindPos(AR,N,ITEM);
for(i=N;i>index;i--)
{
AR[i]=AR[i-1];
}
AR[index]=ITEM;
N+=1;
cout<<"\n\tWant to enter more elements/(y/n)...";
cin>>ch;
}
for(i=0;i<N;i++)
cout<<AR[i]<<" ";
cout<<endl;
getch();
}
int FindPos(int AR[],int size,int item)
{
int pos;

if(item < AR[0])


pos=0;
else
{
for(int i=0;i<size-1;i++)
{
if(AR[i]<=item && item<AR[i+1])
{
pos=i+1;
break;
}
}
if(i==size-1)
pos=size;
}
return pos;
}

Program: Deletion In array


Solution:

#include<iostream.h>
#include<conio.h>
#include<process.h>
int Lsearch(int[],int,int);
void main()
{
clrscr();
int AR[50],ITEM,N,index;
cout<<"HOW MANY ELEMENTS DO YOU WANT TO CREATE
WITH...?:-";
cin>>N;
cout<<"\nEnter Array elements...:-";
for(int i=0;i<N;i++)
cin>>AR[i];
char ch='y';
while(ch=='y'||ch=='Y')
{
cout<<"\n Enter Elements to be deleted....";
cin>>ITEM;

if(N==0)
{
cout<<"Underflow\n";
exit(1);
}
index=Lsearch(AR,N,ITEM);
if(index!=-1)
AR[index]=0;
else
cout<<"Sorry!!No such elements in the array.\n";
cout<<"\n The array now is as shown below...\n";
cout<<"Zero(0) signifies deleted elements\n";
for(i=0;i<N;i++)
cout<<AR[i]<<" " ;
cout<<endl;
cout<<"After this emptied space will be shifted to the end of the
array\n";
for(i=index;i<N;i++)
{
AR[i]=AR[i+1];
}
N-=1;

cout<<"\n Want to delete more elements?(y/n)...";


cin>>ch;
}
cout<<"The array after shifting all emptied space towards right is...\n";
for(i=0;i<N;i++)
cout<<AR[i]<<" ";
cout<<endl;
getch();
}
int Lsearch(int AR[],int size,int item)
{
for(int i=0;i<size;i++)
{
if(AR[i]==item)
return i;
}
return-1;
}

Program: Sum Of Matrices


Solution:

#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
int a[10][10],b[10][10],c[10][10];
int i,j,m,n,p,q;
cout<<"\n\t\t\tInput row & column of matrix-A\n\n\t\t\t";
cin>>m>>n;
cout<<"\n\t\t\tInput row & column of Matrix-B\n\n\t\t\t";
cin>>p>>q;
if((m==p)&&(n==q))
cout<<"Matrices can be added\n";
else
{
cout<<"Matrix cannot be added\n";
exit(0);
}

cout<<"\n\tInput matrix-A\n";
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
cin>>a[i][j];
}
cout<<"\n\tInput matrix-B\n";
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
cin>>b[i][j];
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
c[i][j]= a[i][j]+b[i][j];
}
cout<<"\n\t The sum of two matrices is:\n";
for(i=0;i<m;i++)
{
cout<<"\n";
for(j=0;j<n;j++)

cout<<" "<<c[i][j];
}
getch();
}
Program: Transpose of matrix
Solution:

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[20][20],b[20][20];
int i,j,m,n;
cout<<"\n\t\t\tInput row & column of MATRIX-A\n\n\t\t\t";
cin>>m>>n;
cout<<"\n\t\t\tInput matrix-A\n\n\t\t\t";
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
cin>>a[i][j];
}

cout<<"\n\tMATRIX-A:";
for(i=0;i<m;++i)
{ cout<<"\n";
for(j=0;j<m;++j)
cout<<" "<<a[i][j];
}
for(i=0;i<n;++i)
{
for(j=0;j<m;++j)
b[i][j]=a[j][i];
}
cout<<"\n\tTranspose of matrix is:\n";
for(i=0;i<n;++i)
{
cout<<"\n";
for(j=0;j<m;++j)
cout<<" "<<b[i][j];
}
getch();
}

Program: Insertion In the beginning of the list


Solution:

#include<iostream.h>
#include<process.h>
#include<conio.h>
struct Node
{
int info;
Node*next;
}
*start,*newptr,*save,*ptr;
Node*Create_New_Node(int);
void Insert_Beg(Node*);
void Display(Node*);
void main()
{
clrscr();
start=NULL;
int inf;
char ch='y';
while(ch=='y'||ch=='Y')

{
cout<<"\n\tEnter information for the new node.....";
cin>>inf;
cout<<"\n\tCreating New Node!!Press Enter to continue...";
system("pause");
newptr=Create_New_Node(inf);
if(newptr!=NULL)
{
cout<<"\n\n\tNew Node Created Successfully.Press Enter to
continue...";
system("pause");
}
else
{
cout<<"\n\tCannot create new node!!!Aborting!!\n";
system("pause");
exit(1);
}
cout<<"\n\nNow inserting this node in the beginning of list....";
cout<<"\t\t\t\tPress Enter to continue...\n";
system("pause");
Insert_Beg(newptr);

cout<<"\nNow the list is:\n";


Display(start);
cout<<"Press Y to enter more nodes,N to exit...\n";
cin>>ch;
}
getch();
}
Node*Create_New_Node(int n)
{
ptr=new Node;
ptr->info=n;
ptr->next=NULL;
return ptr;
}
void Insert_Beg(Node*np)
{
if(start==NULL)
start=np;
else
{
save=start;
start=np;

np->next=save;
}
}
void Display(Node*np)
{
while(np!=NULL)
{
cout<<np->info<<"->";
np=np->next;
}
cout<<"........!..!..!.!\n";
}

Program: Deletion from the beginning of the list


Solution:

#include<iostream.h>
#include<stdlib.h>
#include<process.h>
#include<conio.h>
struct Node
{
int info;
Node*next;
}
*start,*newptr,*save,*ptr,*rear;
Node*Create_New_Node(int);
void Insert(Node*);
void Display(Node*);
void DelNode();
void main()
{
clrscr();
start=rear=NULL;
int inf;

char ch='y';
while(ch=='y'||ch=='Y')
{
cout<<"\n\t\tEnter information for the new node.....";
cin>>inf;
newptr=Create_New_Node(inf);
if(newptr==NULL)
{
cout<<"\n\tCannot create new node!!!Aborting!!\n";
system("pause");
exit(1);
}
Insert(newptr);
cout<<"\nPress Y to enter more nodes, N to exit...\n";
cin>>ch;
}
do
{
cout<<"\n\tThe list now is:\n";
Display(start);
system("pause");
cout<<"\nWant to delete first node?(y/n)...";

cin>>ch;
if(ch=='y'||ch=='Y')
DelNode();
}
while(ch=='y'||ch=='Y');
getch();
}
Node*Create_New_Node(int n)
{
ptr=new Node;
ptr->info=n;
ptr->next=NULL;
return ptr;
}
void Insert(Node*np)
{
if(start==NULL)
start=rear=np;
else
{
rear->next=np;
rear=np;

}
}
void DelNode()
{
if(start==NULL)
cout<<"\n\n\tUnderflow.....!.!.!\n";
else
{
ptr=start;start=start->next;
delete ptr;
}
}
void Display(Node*np)
{
while(np!=NULL)
{
cout<<np->info<<"->";
np=np->next;
}
cout<<"\n\n .....!!..!!!..!!!!";
}

Program: Popping from the Linked Stack


Solution:

#include<iostream.h>
#include<stdlib.h>
#include<process.h>
#include<conio.h>
struct Node
{
int info;
Node*next;
}
*top,*newptr,*save,*ptr;
Node*Create_New_Node(int);
void Push(Node*);
void Display(Node*);
void Pop();
void main()
{
clrscr();
top=NULL;
int inf;

char ch='y';
while(ch=='y'||ch=='Y')
{
cout<<"\n\tEnter information for the new node:-";
cin>>inf;
newptr=Create_New_Node(inf);
if(newptr==NULL)
{
cout<<"\n\n\tCannot create new node!!!Aborting!!\n";
system("pause");
exit(1);
}
Push(newptr);
cout<<"\nPress Y to enter more nodes,N to exit...\n";
cin>>ch;
}
do
{
cout<<"\nThe Stack now is:\n";
Display(top);
system("pause");
cout<<"\t\tWant to pop an element?(y/n)...";

cin>>ch;
if(ch=='y'||ch=='Y')
Pop();
}
while(ch=='y'||ch=='Y');
getch();
}
Node*Create_New_Node(int n)
{
ptr=new Node;
ptr->info=n;
ptr->next=NULL;
return ptr;
}
void Push(Node*np)
{
if(top==NULL)
top=np;
else
{
save=top;
top=np;

np->next=save;
}
}
void Pop()
{
if(top==NULL)
cout<<"\n\n\t\t\tunderflow..!!..!!!";
else
{
ptr=top;top=top->next;
delete ptr;
}
}
void Display(Node*np)
{
while(np!=NULL)
{
cout<<np->info<<"->";
np=np->next;
}
cout<<"\n....!.!!...!!1";
}

Program: Deletion from a linked Queue


Solution:

#include<iostream.h>
#include<process.h>
#include<conio.h>
struct Node
{
int info;
Node*next;
}
*front,*newptr,*save,*ptr,*rear;
Node*Create_New_Node(int);
void Insert(Node*);
void Display(Node*);
void DelNode_Q();
void main()
{
clrscr();
front=rear=NULL;
int inf;
char ch='y';

while(ch=='y'||ch=='Y')
{
cout<<"\n\n\tEnter information for the new node.....";
cin>>inf;
newptr=Create_New_Node(inf);
if(newptr==NULL)
{
cout<<"\n\tCannot create new node!!!Aborting!!\n";
system("pause");
exit(1);
}
Insert(newptr);
cout<<"\nPress Y to enter more nodes,N to exit...\n";
cin>>ch;
}
do
{
cout<<"\n\tThe Linked-Queue now is:\n";
Display(front);
cout<<"\nWant to delete first node?(y/n)...";
cin>>ch;
if(ch=='y'||ch=='Y')

DelNode_Q();
}
while(ch=='y'||ch=='Y');
getch();
}
Node*Create_New_Node(int n)
{
ptr=new Node;
ptr->info=n;
ptr->next=NULL;
return ptr;
}
void Insert(Node*np)
{
if(front==NULL)
{
front=rear=np;
}
else
{
rear->next=np;
rear=np;

}
}
void DelNode_Q()
{
if(front==NULL)
cout<<"\n\t\tunderflow......!.!!.!!!..1";
else
{
ptr=front;front=front->next;
delete ptr;
}
}
void Display(Node*np)
{
while(np!=NULL)
{
cout<<np->info<<"->";
np=np->next;
}
cout<<".....!..!!....!!!..1";
}

Program: Insertion and deletion from circular queue.


Solution:

#include<iostream.h>
#include<stdlib.h>
#include<process.h>
int Insert_in_CQ(int[],int);
void Display(int[],int,int);
int Del_in_CQ(int CQueue[]);
const int size=7;
int CQueue[size],front=-1,rear=-1;
int main()
{
int Item,res,ch;
do
{ system("cls");
cout<<"\n\t\t\t\tCircular Queue Menu\n";
cout<<"\t\t 1.Insert\n";
cout<<"\t\t 2.Delete\n";
cout<<"\t\t 3.Display\n";
cout<<"\t\t 4.Exit\n";
cout<<"Enter your choice(1-4):-";

cin>>ch;
switch(ch)
{
case 1: cout<<"Enter ITEM for insertion:";
cin>>Item;
res=Insert_in_CQ(CQueue,Item);
if(res==-1)
cout<<"overflow.....!...!!..1";
else
{
cout<<"\n\nNow the Cir_Quene is:";
Display(CQueue,front,rear);
}
system("pause");
break;
case 2:Item=Del_in_CQ(CQueue);
cout<<"\nElement deleted is:"<<Item<<endl;
Display(CQueue,front,rear);
system("pause");
break;
case 3:Display(CQueue,front,rear);
system("pause");

break;
case 4:break;
default:cout<<"\n\tValid choices are 1...4 only\n";
system("pause");
break;
}
}
while(ch!=4);
return 0;
}
int Insert_in_CQ(int CQuene[],int ele)
{
if((front==0 && rear==size-1)||(front==rear+1))
return-1;
else if(rear==-1) front=rear=0;
else if(rear==size-1) rear=0;
else rear++;
CQuene[rear]=ele;
return 0;
}
void Display(int CQuene[],int front,int rear)
{

int i=0;
cout<<"\nCir_Quene is:\n"<<"\n{Front shown as>>>,Rear as<<<<AND free
space as-}\n";
if(front==-1)
return;
if(rear>=front)
{
for(i=0;i<front;i++)
cout<<"-";
cout<<">>>";
for(i=front;i<rear;i++)
cout<<CQuene[i]<<"<-";
cout<<CQuene[rear]<<"<<<"<<endl;
}
else
{
for(i=0;i<rear;i++)
cout<<CQuene[i]<<"<-";
cout<<CQuene[rear]<<"<<<";
for(;i<front;i++)
cout<<"-";
cout<<">>>";

for(i=front;i<size;i++)
cout<<CQuene[i]<<"<-";
cout<<"\t.....wrap around.....";
}
}
int Del_in_CQ(int CQuene[])
{
int ret;
if(front==-1)return-1;
else
{
ret=CQuene[front];
if(front==rear)
front=rear=-1;
else if(front==size-1)
front=0;
else front++;
}
return ret;
}

Program: To Implement a dynamically allocated Stack containing names


of countries
Solution:

#include<iostream.h>
#include<stdlib.h>
#include<process.h>
int Insert_in_CQ(int[],int);
void Display(int[],int,int);
int Del_in_CQ(int CQueue[]);
const int size=7;
int CQueue[size],front=-1,rear=-1;
int main()
{
int Item,res,ch;
do
{ system("cls");
cout<<"\n\t\t\t\tCircular Queue Menu\n";
cout<<"\t\t 1.Insert\n";
cout<<"\t\t 2.Delete\n";
cout<<"\t\t 3.Display\n";
cout<<"\t\t 4.Exit\n";

cout<<"Enter your choice(1-4):-";


cin>>ch;
switch(ch)
{
case 1: cout<<"Enter ITEM for insertion:";
cin>>Item;
res=Insert_in_CQ(CQueue,Item);
if(res==-1)
cout<<"overflow.....!...!!..1";
else
{
cout<<"\n\nNow the Cir_Quene is:";
Display(CQueue,front,rear);
}
system("pause");
break;
case 2:Item=Del_in_CQ(CQueue);
cout<<"\nElement deleted is:"<<Item<<endl;
Display(CQueue,front,rear);
system("pause");
break;
case 3:Display(CQueue,front,rear);

system("pause");
break;
case 4:break;
default:cout<<"\n\tValid choices are 1...4 only\n";
system("pause");
break;
}
}
while(ch!=4);
return 0;
}
int Insert_in_CQ(int CQuene[],int ele)
{
if((front==0 && rear==size-1)||(front==rear+1))
return-1;
else if(rear==-1) front=rear=0;
else if(rear==size-1) rear=0;
else rear++;
CQuene[rear]=ele;
return 0;
}
void Display(int CQuene[],int front,int rear)

{
int i=0;
cout<<"\nCir_Quene is:\n"<<"\n{Front shown as>>>,Rear as<<<<AND free
space as-}\n";
if(front==-1)
return;
if(rear>=front)
{
for(i=0;i<front;i++)
cout<<"-";
cout<<">>>";
for(i=front;i<rear;i++)
cout<<CQuene[i]<<"<-";
cout<<CQuene[rear]<<"<<<"<<endl;
}
else
{
for(i=0;i<rear;i++)
cout<<CQuene[i]<<"<-";
cout<<CQuene[rear]<<"<<<";
for(;i<front;i++)
cout<<"-";

cout<<">>>";
for(i=front;i<size;i++)
cout<<CQuene[i]<<"<-";
cout<<"\t.....wrap around.....";
}
}
int Del_in_CQ(int CQuene[])
{
int ret;
if(front==-1)return-1;
else
{
ret=CQuene[front];
if(front==rear)
front=rear=-1;
else if(front==size-1)
front=0;
else front++;
}
return ret;
}

Вам также может понравиться