C++ Programs

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 44

/* WAP TO SEARCH AN ELEMENT FROM AN ARRAY (binary search) */ #include<iostream.h> #include<conio.

h> void main() { clrscr(); void bsearch(int[],int,int); int a[50],size,item; cout<<"\n Enter size:"; cin>>size; cout<<"\n Enter array in ascending order:"; for(int i=0; i<size; i++) cin>>a[i]; cout<<"\n Enter element to be searched for:"; cin>>item; bsearch(a,size,item); getch(); } void bsearch(int a[],int size,int item) { int beg,last,mid,f=0; beg=0;last=size-1; while(beg<=last) { mid=(beg+last)/2; if(a[mid]= =item) { cout<<"\n Element found at:"<<mid+1; f=1; break; } else if(item>a[mid]) beg=mid+1; else last=mid-1; } if(f= =0) cout<<"\n Element not found"; } OUTPUT: Enter size:5 Enter array in ascending order:2 3 4 6 7 Enter element to be searched for:6 Element found at:4

/* WAP TO REVERSE A STRING */ #include<iostream.h> #include<conio.h> #include<stdio.h> void main() { clrscr(); char a[80],rev[80]; int i,j; cout<<"\n Enter any string(max.80):"; gets(a); for(i=0; a[i]!='\0'; i++); int k; for(j=i-1,k=0; j>=0; j--,k++) rev[k]=a[j]; rev[k]='\0'; cout<<"\n String in reverse order is:"; puts(rev); getch(); } OUTPUT: Enter any string(max.80):I love C++ String in reverse order is:++C evol I

/*WAP TO STORE DETAILS OF 2 STUDENTS & DISPLAY THEIR DETAILS */ #include<iostream.h> #include<conio.h> #include<stdio.h> struct student { int roll; char name[20]; }s[20]; void main() { clrscr(); for(int i=0; i<2; i++) { cout<<"\n Enter student roll number:"; cin>>s[i].roll; cout<<"\n Enter student name:"; gets(s[i].name); } cout<<"\n Student data is given below-"; for(i=0; i<2; i++) { cout<<"\n Roll number is:"<<s[i].roll; cout<<"\n Name is:"<<s[i].name; } getch(); } OUTPUT: Enter student roll number:16 Enter student name:Gautam Sidher Enter student roll number:18 Enter student name:Jatin Bedi Student data is given belowRoll number is:16 Name is:Gautam Sidher Roll number is:18 Name is:Jatin Bedi

/* WAP TO DISPLAY THE DIAGONAL VALUES OF A MATRIX */ #include<iostream.h> #include<conio.h> void main() { clrscr(); int a[50][50],i,j,r,c; cout<<"\n Enter the number of rows and columns:"; cin>>r>>c; cout<<"\n Enter value of matrix:"; for(i=0; i<r; i++) { for(j=0; j<c; j++) cin>>a[i][j]; } cout<<"\n Diagonal values are:"; for(i=0; i<r; i++) { for(j=0; j<c; j++) { if(i= =j) cout<<a[i][j]; cout<<" "; } } getch(); } OUTPUT: Enter the number of rows and columns:3 3 Enter value of matrix:1 2 3 4 5 6 7 8 9 Diagonal values are:1 5 9

/* WAP TO SORT AN ARRAY USING INSERTION SORT METHOD */ #include<iostream.h> #include<conio.h> #include<limits.h> void InsSort(int[],int); void main() { clrscr(); int a[50],size; cout<<"\n How many elements do U want to create array with?(max.50):"; cin>>size; cout<<"\n Enter array elements:"; for(int i=1;i<=size;i++) cin>>a[i]; InsSort(a,size); cout<<"\n The Sorted array is as shown below:\n"; for(i=1;i<=size;i++) cout<<a[i]<<" "; cout<<endl; getch(); } 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<<"\n Array after pass-"<<i<<"-is:\t"; for(int k=1;k<=size;k++) cout<<a[k]<<" "; cout<<endl; }} OUTPUT: How many elements do U want to create array with?(max.50):5 Enter array elements:9 4 34 2 6 Array after pass-1-is: 9 4 34 2 6 Array after pass-2-is: 4 9 34 2 6 Array after pass-3-is: 4 9 34 2 6 Array after pass-4-is: 2 4 9 34 6 Array after pass-5-is: 2 4 6 9 34 The Sorted array is as shown below: 2 4 6 9 34

/* WAP TO SORT AN ARRAY USING MERGE SORT METHOD */ #include<iostream.h> #include<conio.h> void Merge(int [],int,int[],int,int[]); void main() { clrscr(); int A[50],B[50],C[50],MN=0,M,N; cout<<"\n How many elements do U want to create first array with?(max.50):"; cin>>M; cout<<"\n Enter first array's elements [ascending]:\n"; for(int i=0;i<M;i++) cin>>A[i]; cout<<"\n How many elements do U want to create second array with?(max.50):"; cin>>N; MN=M+N; cout<<"\n Enter second array's elements [descending]:\n"; for(i=0;i<N;i++) cin>>B[i]; Merge(A,M,B,N,C); cout<<"\n The Merged array is as shown below:\n"; for(i=0;i<MN;i++) cout<<C[i]<<" "; cout<<endl; getch(); } void Merge(int A[],int M,int B[],int N,int C[]) { int a,b,c; for(a=0,b=N-1,c=0;a<M&&b>=0;) { if(A[a]<=B[b]) C[c++]=A[a++]; else C[c++]=B[b--]; } if(a<M) { while(a<M) C[c++]=A[a++]; } else { while(b>=0) C[c++]=B[b--]; } } OUTPUT: How many elements do U want to create first array with?(max.50):5 Enter first array's elements [ascending]: 2 5 8 9 12 How many elements do U want to create second array with?(max.50):7 Enter second array's elements [descending]: 16 12 10 8 7 3 1 The Merged array is as shown below: 1 2 3 5 7 8 8 9 10 12 12 16

/* WAP TO INPUT & DISPLAY INFO OF TWO FLIGHTS USING CLASSES */ #include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> class Flight {int flightno; char destination[20]; float distance; float fuel; public: Flight() { flightno=0; strcpy(destination,"India"); distance=20; fuel=5; } void Feed_Info() { cout<<"\n Enter the flight number:"; cin>>flightno; cout<<"\n Enter the destination:"; gets(destination); cout<<"\n Enter the distance travelled (in km):"; cin>>distance; cout<<"\n Enter the quantity of fuel used (in litres):"; cin>>fuel; } void Show_Info() { cout<<"\n Flight number:"<<flightno; cout<<"\n Destination:"<<destination; cout<<"\n Distance travelled (in km):"<<distance; cout<<"\n Fuel used (in litres):"<<fuel; } }; void main() { clrscr(); Flight ob[2]; for(int i=0;i<2;i++) { cout<<"\n"; ob[i].Feed_Info(); ob[i].Show_Info(); } getch(); }

OUTPUT: Enter the flight number:507 Enter the destination:China Enter the distance travelled (in km):500 Enter the quantity of fuel used (in litres):250 Flight number:507 Destination:China Distance travelled (in km):500 Fuel used (in litres):250 Enter the flight number:302 Enter the destination:London Enter the distance travelled (in km):1200 Enter the quantity of fuel used (in litres):750 Flight number:302 Destination:London Distance travelled (in km):1200 Fuel used (in litres):750

/* INSERTION AND DELETION IN CIRCULAR QUEUE */ #include<iostream.h> #include<conio.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() { clrscr(); int Item,res,ch; do{ cout<<"\t\t\tCircular Queue Menu\n"; cout<<"\t1.Insert\n"; cout<<"\t2.Delete\n"; cout<<"\t3.Display\n"; cout<<"\t4.Exit\n"; cout<<"Enter your choice(1-4)..."; cin>>ch; switch(ch) { case 1: cout<<"\n Enter ITEM for insertion:"; cin>>Item; res=Insert_in_CQ(CQueue,Item); if(res==-1) cout<<"OVERFLOW!!!\n"; else { cout<<"\n Now the Cir_Queue is:\n"; Display(CQueue,front,rear); } getch(); break; case 2: Item=Del_in_CQ(CQueue); cout<<"Element deleted is:"<<Item<<endl; Display(CQueue,front,rear); getch(); break; case 3: Display(CQueue,front,rear); getch(); break; case 4: break; default:cout<<"Valid choices are 1...4 only\n"; getch(); break; } }while(ch!=4); return 0; } int Insert_in_CQ(int CQueue[],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++; CQueue[rear]=ele; return 0; } void Display(int CQueue[],int front,int rear) { int i=0; cout<<"\n Cir_Queue is:\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<<CQueue[i]<<"<-"; cout<<CQueue[rear]<<"<<<"<<endl; } else {for(i=0;i<rear;i++) cout<<CQueue[i]<<"<-"; cout<<CQueue[rear]<<"<<<"; for(;i<front;i++) cout<<"-"; cout<<">>>"; for(i=front;i<size;i++) cout<<CQueue[i]<<"<-"; cout<<"\t...wrap around..."; }} int Del_in_CQ(int CQueue[]) {int ret; if(front==-1) return-1; else {ret=CQueue[front]; if(front==rear) front=rear=-1; else if(front==size-1) front=0; else front++; }return ret; } OUTPUT: Circular Queue Menu 1.Insert 2.Delete 3.Display 4.Exit Enter your choice(1-4)...1 Enter ITEM for insertion:9 Now the Cir_Queue is: Cir_Queue is: (Front shown as >>>,Rear as <<<AND free space as-) >>>9<<< Circular Queue Menu 1.Insert 2.Delete 3.Display 4.Exit Enter your choice(1-4)...4

/* WAP TO ILLUSTRATE WORKING OF FUNCTION OVERLOADING */ #include<iostream.h> #include<conio.h> #include<stdlib.h> void amount(float princ,int time,float rate) { cout<<"\nPrincipal Amount:"<<princ; cout<<"\tTime:"<<time<<"years"; cout<<"\tRate:"<<rate; cout<<"\nInterest Amount:"<<(princ*time*rate)<<"\n\n"; } void amount(float princ,int time) { cout<<"\nPrincipal Amount:"<<princ; cout<<"\tTime:"<<time<<"years"; cout<<"\tRate:0.08"; cout<<"\nInterest Amount:"<<(princ*time*0.08)<<"\n\n"; } void amount(float princ,float rate) { cout<<"\nPrincipal Amount:"<<princ; cout<<"\tTime:2years"; cout<<"\tRate:"<<rate; cout<<"\nInterest Amount:"<<(princ*2*rate)<<"\n\n"; } void amount(int time,float rate) { cout<<"\nPrincipal Amount:2000"; cout<<"\tTime:"<<time<<"years"; cout<<"\tRate:"<<rate; cout<<"\nInterest Amount:"<<(2000*time*rate)<<"\n\n"; } void amount(float princ) { cout<<"\nPrincipal Amount:"<<princ; cout<<"\tTime:2years"; cout<<"\tRate:0.08"; cout<<"\nInterest Amount:"<<(princ*2*0.08)<<"\n\n"; } void main() { clrscr(); cout<<"Case 1"; amount(2000.0F); cout<<"Case 2"; amount(2500.0F,3); cout<<"Case 3"; amount(2300.0F,3,0.11F); cout<<"Case 4"; amount(2,0.12F); cout<<"Case 5"; amount(6,0.07F); getch(); }

OUTPUT: Case 1 Principal Amount:2000 Time:2years Interest Amount:320 Case 2 Principal Amount:2500 Time:3years Interest Amount:600 Case 3 Principal Amount:2300 Time:3years Interest Amount:759 Case 4 Principal Amount:2000 Time:2years Interest Amount:480 Case 5 Principal Amount:2000 Time:6years Interest Amount:840 Rate:0.08

Rate:0.08

Rate:0.11

Rate:0.12

Rate:0.07

/* INSERTION IN THE BEGINNING OF A LIST */ #include<iostream.h> #include<conio.h> #include<process.h> struct Node { int info; Node*next; } *start,*newptr,*save,*ptr; Node*Create_New_Node(int); void Insert_Beg(Node*); void Display(Node*); int main() { clrscr(); start=NULL; int inf; char ch='y'; while(ch= ='y'||ch= ='Y') { cout<<"\n Enter INFOrmation for the new node..."; cin>>inf; cout<<"\n Creating New Node!! Press Enter to continue..."; getch(); newptr=Create_New_Node(inf); if(newptr!=NULL) { cout<<"\n\nNew Node Created Successfully.Press Enter to continue..."; getch(); } else { cout<<"\nCannot create new node!!! Aborting!!\n"; getch(); exit(1); } cout<<"\n\nNow inserting this node in the beginning of list...\n"; cout<<"Press Enter to continue...\n"; getch(); Insert_Beg(newptr); cout<<"\nNow the list is:\n"; Display(start); cout<<"\n Press Y to enter more nodes,N to exit...\n"; cin>>ch; }return 0; } 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"; } OUTPUT: Enter INFOrmation for the new node...9 Creating New Node!! Press Enter to continue... New Node Created Successfully.Press Enter to continue... Now inserting this node in the beginning of list... Press Enter to continue... Now the list is: 9->!!! Press Y to enter more nodes,N to exit... Y Enter INFOrmation for the new node...3 Creating New Node!! Press Enter to continue... New Node Created Successfully.Press Enter to continue... Now inserting this node in the beginning of list... Press Enter to continue... Now the list is: 3->9->!!! Press Y to enter more nodes,N to exit... N

/* INSERTION IN THE END OF LIST */ #include<iostream.h> #include<conio.h> #include<stdlib.h> #include<process.h> struct Node { int info; Node*next; }*start,*newptr,*save,*ptr,*rear; Node*Create_New_Node(int); void Insert_End(Node*); void Display(Node*); int main() { clrscr(); start=rear=NULL; int inf; char ch='y'; while(ch= ='y'||ch= ='Y') {cout<<"\n Enter INFOrmation for the new node..."; cin>>inf; cout<<"\n Creating New Node!! Press Enter to continue..."; getch(); newptr=Create_New_Node(inf); if(newptr!=NULL) {cout<<"\n\nNew Node Created Successfully.Press Enter to continue..."; getch(); } else {cout<<"\nCannot create new node!!! Aborting!!\n"; getch(); exit(1); } cout<<"\n\nNow inserting this node in the end of list...\n"; cout<<"Press Enter to continue...\n"; getch(); Insert_End(newptr); cout<<"\nNow the list is:\n"; Display(start); cout<<"\n Press Y to enter more nodes,N to exit...\n"; cin>>ch; }return 0; } Node*Create_New_Node(int n) { ptr=new Node; ptr->info=n; ptr->next=NULL; return ptr; }

void Insert_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<<"!!!\n"; } OUTPUT: Enter INFOrmation for the new node...9 Creating New Node!! Press Enter to continue... New Node Created Successfully.Press Enter to continue... Now inserting this node in the end of list... Press Enter to continue... Now the list is: 9->!!! Press Y to enter more nodes,N to exit... Y Enter INFOrmation for the new node...3 Creating New Node!! Press Enter to continue... New Node Created Successfully.Press Enter to continue... Now inserting this node in the end of list... Press Enter to continue... Now the list is: 9->3->!!! Press Y to enter more nodes,N to exit... N

/* DELETION FROM THE BEGINNING OF LIST */ #include<iostream.h> #include<conio.h> #include<stdlib.h> #include<process.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(); int main() { clrscr(); start=rear=NULL; int inf; char ch='y'; while(ch= ='y'||ch= ='Y') {cout<<"\n Enter INFOrmation for the new node..."; cin>>inf; newptr=Create_New_Node(inf); if(newptr= =NULL) {cout<<"\nCannot create new node!!! Aborting!!\n"; getch(); exit(1); } Insert(newptr); cout<<"\n Press Y to enter more nodes,N to exit...\n"; cin>>ch; } do {cout<<"\n The List now is:\n"; Display(start); getch(); cout<<"Want to delete first node?(y/n)..."; cin>>ch; if(ch= ='y'||ch= ='Y') DelNode(); }while(ch= ='y'||ch= ='Y'); return 0; } 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<<"UNDERFLOW!!!\n"; else {ptr=start; start=start->next; delete ptr; } } void Display(Node*np) { while(np!=NULL) {cout<<np->info<<"->"; np=np->next; } cout<<"!!!\n"; } OUTPUT: Enter INFOrmation for the new node...25 Press Y to enter more nodes,N to exit... Y Enter INFOrmation for the new node...21 Press Y to enter more nodes,N to exit... N The List now is: 25->21->!!! Want to delete first node?(y/n)...y The List now is: 21->!!! Want to delete first node?(y/n)...n

/* PUSHING IN STACK-ARRAY */ #include<iostream.h> #include<conio.h> #include<stdlib.h> #include<process.h> int Push(int[],int&,int); void Display(int[],int); const int size=50; int main() {clrscr(); int Stack[size],Item,top=-1,res; char ch='y'; while(ch= ='y'||ch= ='Y') {cout<<"\n Enter ITEM for insertion:"; cin>>Item; res=Push(Stack,top,Item); if(res= =-1) { cout<<"OVERFLOW!!! Aborting!!\n"; exit(1); } cout<<"\n The Stack now is:\n"; Display(Stack,top); cout<<"\n Want to insert more elements?(y/n)..."; cin>>ch; } return 0; } int Push(int Stack[],int& top,int ele) {if(top= =size-1) return -1; else {top++; Stack[top]=ele; } return 0; } void Display(int Stack[],int top) {cout<<Stack[top]<<"<--"<<endl; for(int i=top-1;i>=0;i--) cout<<Stack[i]<<endl; }

OUTPUT: Enter ITEM for insertion:9 The Stack now is: 9<-Want to insert more elements?(y/n)...y Enter ITEM for insertion:6 The Stack now is: 6<-9 Want to insert more elements?(y/n)...y Enter ITEM for insertion:3 The Stack now is: 3<-6 9 Want to insert more elements?(y/n)...n

/* 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*); int main() {clrscr(); int inf; char ch='y'; top=NULL; while(ch= ='y'||ch= ='Y') {cout<<"\n Enter INFOrmation for the new node..."; cin>>inf; newptr=Create_New_Node(inf); if(newptr= =NULL) {cout<<"\nCannot create new node!!! Aborting!!\n"; exit(1); } Push(newptr); cout<<"\nNow the linked-stack is:\n"; Display(top); cout<<"\n Press Y to enter more nodes,N to exit..."; cin>>ch; }return 0; } 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 Display(Node*np) { while(np!=NULL) {cout<<np->info<<"->"; np=np->next; } cout<<"!!!\n"; }

OUTPUT: Enter INFOrmation for the new node...5 Now the linked-stack is: 5->!!! Press Y to enter more nodes,N to exit...Y Enter INFOrmation for the new node...8 Now the linked-stack is: 8->5->!!! Press Y to enter more nodes,N to exit...Y Enter INFOrmation for the new node...15 Now the linked-stack is: 15->8->5->!!! Press Y to enter more nodes,N to exit...N

/* INSERTION IN ARRAY-QUEUE */ #include<iostream.h> #include<conio.h> #include<stdlib.h> #include<process.h> int Insert_in_Q(int[],int); void Display(int[],int,int); const int size=50; int Queue[size],front=-1,rear=-1; int main() {clrscr(); int Item,res; char ch='y'; while(ch= ='y'||ch= ='Y') {cout<<"\n Enter ITEM for insertion:"; cin>>Item; res=Insert_in_Q(Queue,Item); if(res= =-1) { cout<<"OVERFLOW!!! Aborting!!\n"; exit(1); } cout<<"Now the Queue (Front.....to.....Rear) is:\n"; Display(Queue,front,rear); cout<<"Want to insert more elements?(y/n)..."; cin>>ch; } return 0; } int Insert_in_Q(int Queue[],int ele) {if(rear= =size-1)return -1; else if(rear= =-1) {front=rear=0; Queue[rear]=ele; } else {rear++; Queue[rear]=ele; } return 0; } void Display(int Queue[],int front,int rear) {if(front= =-1)return; for(int i=front;i<rear;i++) cout<<Queue[i]<<"<-"; cout<<Queue[rear]<<endl; }

OUTPUT: Enter ITEM for insertion:3 Now the Queue (Front.....to.....Rear) is: 3 Want to insert more elements?(y/n)...y Enter ITEM for insertion:6 Now the Queue (Front.....to.....Rear) is: 3<-6 Want to insert more elements?(y/n)...y Enter ITEM for insertion:9 Now the Queue (Front.....to.....Rear) is: 3<-6<-9 Want to insert more elements?(y/n)...y Enter ITEM for insertion:12 Now the Queue (Front.....to.....Rear) is: 3<-6<-9<-12 Want to insert more elements?(y/n)...n

/* 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); void Insert_End(Node*); void Display(Node*); int main() { clrscr(); front=rear=NULL; int inf; char ch='y'; while(ch= ='y'||ch= ='Y') { cout<<"\n Enter INFOrmation for the new node..."; cin>>inf; newptr=Create_New_Node(inf); if(newptr= =NULL) { cout<<"\nCannot create new node!!! Aborting!!\n"; exit(1); } Insert_End(newptr); cout<<"\nNow the Queue( Front...to...Rear ) is:\n"; Display(front); cout<<"\n Press Y to enter more nodes,N to exit..."; cin>>ch; }return 0; } Node*Create_New_Node(int n) { ptr=new Node; ptr->info=n; ptr->next=NULL; return ptr; } void Insert_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<<"!!!\n"; }

OUTPUT: Enter INFOrmation for the new node...5 Now the Queue( Front...to...Rear ) is: 5->!!! Press Y to enter more nodes,N to exit...Y Enter INFOrmation for the new node...12 Now the Queue( Front...to...Rear ) is: 5->12->!!! Press Y to enter more nodes,N to exit...Y Enter INFOrmation for the new node...23 Now the Queue( Front...to...Rear ) is: 5->12->23->!!! Press Y to enter more nodes,N to exit...N

/* INSERTION IN ARRAY */ #include<iostream.h> #include<conio.h> #include<process.h> int FindPos(int[],int,int); void main() { clrscr(); int a[50],item,size,index; cout<<"\n Enter the size of array(max.50):"; cin>>size; cout<<"\n Enter array elements(must be sorted in Asc order):\n\n"; for(int i=0;i<size;i++) cin>>a[i]; char ch='y'; while(ch= ='y'||ch= ='Y') { cout<<"\n Enter element to be inserted:"; cin>>item; if(size= =50) { cout<<"Overflow!!\n"; exit(1);} index=FindPos(a,size,item); for(i=size;i>index;i--) { a[i]=a[i-1]; } a[index]=item; size+=1; cout<<"\n Want to insert more elements?(y/n):"; cin>>ch; } cout<<"\n The array now is as shown below:\n"; for(i=0;i<size;i++) cout<<a[i]<<" "; getch(); } int FindPos(int a[],int size,int item) { int pos; if(item<a[0]) pos=0; else { for(int i=0;i<size-1;i++) { if(a[i]<=item&&item<a[i+1]) { pos=i+1; break; }} if(i= =size-1) pos=size; }return pos; }

OUTPUT: Enter the size of array(max.50):6 Enter array elements(must be sorted in Asc order): 2 6 9 10 12 15 Enter element to be inserted:11 Want to insert more elements?(y/n):y Enter element to be inserted:4 Want to insert more elements?(y/n):n The array now is as shown below: 2 4 6 9 10 11 12 15

/* DELETION IN ARRAY */ #include<iostream.h> #include<conio.h> #include<process.h> int Lsearch(int[],int,int); void main() {clrscr(); int a[50],item,size,index; cout<<"\n Enter the size of array(max.50):"; cin>>size; cout<<"\n Enter array elements:\n\n"; for(int i=0;i<size;i++) cin>>a[i]; char ch='y'; while(ch= ='y'||ch= ='Y') { cout<<"\n Enter element to be deleted:"; cin>>item; if(size= =0) { cout<<"Underflow!!\n"; exit(1); } index=Lsearch(a,size,item); if(index!=-1) a[index]=0; else cout<<"Sorry!! No such element in the array.\n"; cout<<"\n The array now is as shown below:\n\n"; cout<<"Zero (0) signifies deleted element\n"; for(i=0;i<size;i++) cout<<a[i]<<" "; cout<<"\n After this emptied space will be shifted to the end of array\n"; for(i=index;i<size;i++) {a[i]=a[i+1]; } size-=1; cout<<"\n Want to delete more elements?(y/n):"; cin>>ch; } cout<<"The array after shifting ALL emptied spaces towards right is:\n\n"; for(i=0;i<size;i++) cout<<a[i]<<" "; getch(); } int Lsearch(int a[],int size,int item) { for(int i=0;i<size;i++) { if(a[i]= =item) return i; }return -1; }

OUTPUT: Enter the size of array(max.50):10 Enter array elements: 2 4 5 3 7 9 12 15 33 40 Enter element to be deleted:9 The array now is as shown below: Zero (0) signifies deleted element 2 4 5 3 7 0 12 15 33 40 After this emptied space will be shifted to the end of array Want to delete more elements?(y/n):n The array after shifting ALL emptied spaces towards right is: 2 4 5 3 7 12 15 33 40

/* DELETION IN ARRAY- QUEUE */ #include<iostream.h> #include<conio.h> #include<stdlib.h> #include<process.h> int Remove(int[]); int Insert(int[],int); void Display(int[],int,int); const int size=50; int Queue[size],front=-1,rear=-1; int main() {clrscr(); int Item,res; char ch='y'; while(ch=='y'||ch=='Y') {cout<<"\n Enter ITEM for insertion:"; cin>>Item; res=Insert(Queue,Item); if(res==-1) { cout<<"OVERFLOW!!! Aborting!!\n"; exit(1); } cout<<"\n Now the Queue (Front...to...Rear) is:\n"; Display(Queue,front,rear); cout<<"\n Want to insert more elements?(y/n)..."; cin>>ch; } cout<<" Now deletion of elements begins...\n"; ch='y'; while(ch=='y'||ch=='Y') {res=Remove(Queue); if(res==-1) { cout<<"UNDERFLOW!!! Aborting!!\n"; exit(1); } else {cout<<"\nElement deleted is:"<<res<<endl; cout<<" Now the Queue ( Front...to...Rear ) is:\n"; Display(Queue,front,rear); } cout<<" Want to delete more elements?(y/n)..."; cin>>ch; } return 0; } int Insert(int Queue[],int ele) {if(rear==size-1) return -1; else if(rear==-1) {front=rear=0; Queue[rear]=ele;

} else {rear++; Queue[rear]=ele; } return 0; } int Remove(int Queue[]) {int ret; if(front==-1) return -1; else {ret=Queue[front]; if(front==rear) front=rear=-1; else front++; } return ret; } void Display(int SQueue[],int front,int rear) {if(front==-1) return; for(int i=front;i<rear;i++) cout<<Queue[i]<<"<-\t"; cout<<Queue[rear]<<endl; } OUTPUT: Enter ITEM for insertion:4 Now the Queue (Front...to...Rear) is: 4 Want to insert more elements?(y/n)...y Enter ITEM for insertion:6 Now the Queue (Front...to...Rear) is: 4<- 6 Want to insert more elements?(y/n)...n Now deletion of elements begins... Element deleted is:4 Now the Queue ( Front...to...Rear ) is: 6 Want to delete more elements?(y/n)...n

/* POPPING FROM ARRAY-STACK */ #include<iostream.h> #include<conio.h> #include<process.h> int Pop(int[],int&); int Push(int[],int&,int); void Display(int[],int); const int size=50; int main() {clrscr(); int Stack[size],Item,top=-1,res; char ch='y'; while(ch=='y'||ch=='Y') {cout<<"\n Enter ITEM for insertion:"; cin>>Item; res=Push(Stack,top,Item); if(res==-1) {cout<<"OVERFLOW!!! Aborting!!\n"; exit(1); } cout<<"\n The Stack now is:\n"; Display(Stack,top); cout<<"\n Want to insert more elements?(y/n)..."; cin>>ch; } cout<<"Now deletion of elements begins...\n"; ch='y'; while(ch=='y'||ch=='Y') {res=Pop(Stack,top); if(res==-1) {cout<<"UNDERFLOW!!! Aborting!!\n"; exit(1); } else {cout<<"\nElement deleted is:"<<res<<endl; cout<<"\n The Stack now is:\n"; Display(Stack,top); } cout<<"\n Want to delete more elements?(y/n)..."; cin>>ch; } return 0; } int Push(int Stack[],int & top,int ele) { if(top==size-1) return -1;

else {top++; Stack[top]=ele; } return 0; } int Pop(int Stack[],int & top) {int ret; if(top==-1) return -1; else {ret=Stack[top]; top--; } return ret; } void Display(int Stack[],int top) {if(top==-1) return; cout<<Stack[top]<<"<--"<<endl; for(int i=top-1;i>=0;i--) cout<<Stack[i]<<endl; } OUTPUT: Enter ITEM for insertion:9 The Stack now is: 9<-Want to insert more elements?(y/n)...y Enter ITEM for insertion:6 The Stack now is: 6<-9 Want to insert more elements?(y/n)...n Now deletion of elements begins... Element deleted is:6 The Stack now is: 9<-Want to delete more elements?(y/n)...n

/* WAP TO SHOW WORKING OF VIRTUAL BASE CLASSES */ #include<iostream.h> #include<conio.h> #include<stdlib.h> class base {public: int a; }; class d1:virtual public base {public: int b; }; class d2:virtual public base {public: int c; }; class d3:public d1,public d2 {public: int total; }; void main() {clrscr(); d3 ob; ob.a=25; ob.b=50; ob.c=75; ob.total=ob.a+ob.b+ob.c; cout<<ob.a<<"\t" <<ob.b<<"\t"<<ob.c<<"\t" <<ob.total<<endl; getch(); } OUTPUT: 25 50 75 150

/* WAP TO APPEND DATA IN A FILE */ #include<iostream.h> #include<conio.h> #include<fstream.h> #include<stdio.h> class stu{int rollno; char name[25]; char Class[4]; float marks; public: void getdata() {cout<<"Rollno:"; cin>>rollno; cout<<"Name:"; gets(name); cout<<"Class:"; gets(Class); cout<<"Marks:"; cin>>marks; } void putdata() {cout<<name<<",rollno"<<rollno<<"has"<<marks<<"% marks."; } int getrno() { return rollno; } }s1; void main() {clrscr(); ofstream fo("app.cpp",ios::app); char ans='y'; while(ans=='y') {s1.getdata(); fo.write((char*)&s1,sizeof(s1)); cout<<"Record added to file.\n"; cout<<"Want to enter more records?(y/n).."; cin>>ans; } fo.close(); getch(); } OUTPUT: Rollno:1 Name:Gautam Sidher Class:12A Marks:100 Record added to file. Want to enter more records?(y/n)..y Rollno:2 Name:Prashant Tanwar Class:12A Marks:99 Record added to file. Want to enter more records?(y/n)..n

/* WAP TO SEARCH A RECORD IN A FILE */ #include<fstream.h> #include<conio.h> #include<stdio.h> class stu{int rollno; char name[25]; char Class[4]; float marks; public: void getdata() {cout<<"Rollno:"; cin>>rollno; cout<<"Name:"; gets(name); cout<<"Class:"; gets(Class); cout<<"Marks:"; cin>>marks; } void putdata() {cout<<name<<",rollno"<<" "<<rollno<<" "<<"has"<<" "<<marks <<"% marks."<<endl; } int getrno() { return rollno; } } s1; void main() {clrscr(); int rn; char found='n'; ifstream fi("app.cpp",ios::in); cout<<"Enter rollno to be searched for:"; cin>>rn; while(!fi.eof()) {fi.read((char*)&s1,sizeof(s1)); if(s1.getrno()==rn) {s1.putdata(); found='y'; break; } } if(found=='n') cout<<"Rollno not found in file!!"<<endl; fi.close(); getch(); } OUTPUT: Enter rollno to be searched for:1 Gautam Sidher,rollno 1 has 100% marks.

/* WAP TO INSERT DATA IN A SORTED FILE */ #include<fstream.h> #include<conio.h> #include<stdio.h> class stu{int rollno; char name[25]; char Class[4]; float marks; public: void getdata() {cout<<"Rollno:"; cin>>rollno; cout<<"Name:"; gets(name); cout<<"Class:"; gets(Class); cout<<"Marks:"; cin>>marks; } void putdata() {cout<<"Rollno:"<<rollno<<"\tName:"<<name<<"\n Marks:" <<marks<<endl; } int getrno() { return rollno; } }s1,stud; void main() {clrscr(); ifstream fi("app.cpp",ios::in); ofstream fo("tmp.cpp",ios::out); char last='y'; cout<<"Enter details of student whose record is to be inserted:\n"; s1.getdata(); while(!fi.eof()) {fi.read((char*)&stud,sizeof(stud)); if(s1.getrno()<=stud.getrno()) {fo.write((char*)&s1,sizeof(s1)); last='n'; break; } else fo.write((char*)&stud,sizeof(stud)); } if(last=='y') fo.write((char*)&s1,sizeof(s1)); else if(!fi.eof()) {while(!fi.eof()) {fi.read((char*)&stud,sizeof(stud)); fo.write((char*)&stud,sizeof(stud)); } } fi.close();

fo.close(); remove("app.cpp"); rename("tmp.cpp","app.cpp"); fi.open("app.cpp",ios::in); cout<<"\n File now contains:\n"; while(!fi.eof()) {fi.read((char*)&stud,sizeof(stud)); if(fi.eof()) break; stud.putdata(); } fi.close(); getch(); } OUTPUT: Enter details of student whose record is to be inserted: Rollno:3 Name:Ajay Gupta Class:12A Marks:98 File now contains: Rollno:1 Name:Gautam Sidher Marks:100 Rollno:2 Name:Prashant Tanwar Marks:99 Rollno:3 Name:Ajay Gupta Marks:98

You might also like