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

Program To Implement Binary Search in A Linear Array

The document contains 10 programs demonstrating various data structures and algorithms concepts like binary search, linear search, function overloading, bubble sort, selection sort, insertion sort, multiple inheritance, stack operations, and queue operations. Each program is presented with the source code and sample output.

Uploaded by

Tushar Gupta
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Program To Implement Binary Search in A Linear Array

The document contains 10 programs demonstrating various data structures and algorithms concepts like binary search, linear search, function overloading, bubble sort, selection sort, insertion sort, multiple inheritance, stack operations, and queue operations. Each program is presented with the source code and sample output.

Uploaded by

Tushar Gupta
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 18

1.

Program to implement binary search in a linear array


#include<iostream.h> #include<stdio.h> #include<conio.h> void main() { int array[50]; int i,j,size,temp,num; int low,mid,high; clrscr(); cout<<"Enter the size of the array(max. 50)"<<"\n"; cin>>size; for(i=0;i<size;i++) { cout<<"Enter element no. "<<i+1<<"\n"; cin>>array[i]; } for(i=0;i<size;i++) { for(j=0;j<(size-i-1);j++) { if(array[j]>array[j+1]) { temp=array[j]; array[j]=array[j+1]; array[j+1]=temp; } } } cout<<"Sorted array is"<<"\n"; for(i=0;i<size;i++) { cout<<array[i]<<' '; } cout<<"\nEnter the element to be searched"<<"\n"; cin>>num; low=1; high=size; do { mid=(low+high)/2; if(num<array[mid]) high=mid-1; else if(num>array[mid]) low=mid+1; } while(num!=array[mid]&&low<=high); if(num==array[mid]) { cout<<"No. present at location "<<mid+1<<"\n"; }

else { cout<<"No. not found"<<"\n"; } }

Output
Enter the size of array 5 Enter element no. 1 3 Enter element no. 2 2 Enter element no. 3 5 Enter element no. 4 4 Enter element no. 5 1 Sorted array is 12345 Enter the element to be searched 5 No. present at location 3

2. Program to implement linear search in an array


#include<iostream.h> #include<conio.h> void main() { int arr[50],num,size,j=0; start: clrscr(); cout<<"Enter the size of array(max. 50)"<<"\n"; cin>>size; if(size<1&&size>50) goto start; for(int i=0;i<size;i++) { cout<<"Enter element no. "<<i+1<<"\n"; cin>>arr[i]; } cout<<"Enter the element to be searched in the array"<<"\n"; cin>>num; for(i=0;i<size;i++) { if(arr[i]==num) { cout<<"Element found in position no. "<<i+1<<"\n"; j=1; } } if(j==0) cout<<"Element not found"<<"\n"; }

Output
Enter the size of array 5 Enter element no. 1 3 Enter element no. 2 2 Enter element no. 3 5 Enter element no. 4 4 Enter element no. 5 1 Enter the element to be searched 5 No. present at location 3

3. Program to find the area of circle,rectangle and square using function overloading
#include<iostream.h> #include<conio.h> void area(float r) { cout<<"The area of circle is "<<3.14*r*r<<" sq units\n"; } void area(float l,float b) { cout<<"The area of rectangle is "<<l*b<<" sq units\n"; } void area(double l) { cout<<"The area of square is "<<l*l<<" sq units\n"; } void area(double b,double p) { cout<<"The area of triangle is "<<0.5*b*p<<" sq units\n"; } void area(long double d1,long double d2) { cout<<"The area of rhombus is "<<0.5*d1*d2<<" sq units\n"; } void main() { int choice; float a,b; double c,d; long double e,f; start: clrscr(); cout<<"MENU"<<"\n"; cout<<"1. Circle"<<"\n"; cout<<"2. Reactangle"<<"\n"; cout<<"3. Square"<<"\n"; cout<<"4. Triangle"<<"\n"; cout<<"5. Rhombus"<<"\n"; cout<<"Enter your choice"<<"\n"; cin>>choice; switch(choice) { case 1: cout<<"Enter the radius"<<"\n"; cin>>a; area(a); break; case 2: cout<<"Enter the length and breadth"<<"\n"; cin>>a>>b; area(a,b);

break; case 3: cout<<"Enter the side"<<"\n"; cin>>c; area(c); break; case 4: cout<<"Enter the base and perpendicular"<<"\n"; cin>>c>>d; area(c,d); break; case 5: cout<<"Enter the diagonals 1 and 2"<<"\n"; cin>>e>>f; area(e,f); break; default: goto start; } }

Output
MENU 1. Circle 2. Reactangle 3. Square 4. Triangle 5. Rhombus Enter your choice 1 Enter the radius 3 The area of circle is 28.26 sq units

4. Program to sort an array using bubble sort


#include<iostream.h> #include<conio.h> void main() { int array[50]; int i,j,size,temp,num; clrscr(); cout<<"Enter the size of the array(max. 50)"<<"\n"; cin>>size; for(i=0;i<size;i++) { cout<<"Enter element no. "<<i+1<<"\n"; cin>>array[i]; } for(i=0;i<size;i++) { for(j=0;j<(size-i-1);j++) { if(array[j]>array[j+1]) { temp=array[j]; array[j]=array[j+1]; array[j+1]=temp; } } } cout<<"Sorted array using Bubble Sort is"<<"\n"; for(i=0;i<size;i++) { cout<<array[i]<<' '; }

Output
Enter the size of array 5 Enter element no. 1 3 Enter element no. 2 2 Enter element no. 3 5 Enter element no. 4 4 Enter element no. 5 1 Sorted array is 12345

5. Recursive Program to calculate fatorial of the no. entered by user and print fibonacci series
#include<iostream.h> #include<conio.h> void main() { label: clrscr(); int choice,i,num1,num2,num3,num4,a=0,large; long int fact=1; char ch; label1: cout<<"CALCULATOR"<<"\n"; cout<<"1. Factorial"<<"\n"; cout<<"2. Fibonacci"<<"\n"; cout<<"3. Prime Number"<<"\n"; cout<<"4. Largest"<<"\n"; cout<<"Enter your choice"<<"\n"; cin>>choice; if(choice<1||choice>4) { cout<<"Wrong choice. Try again"<<"\n"; goto label1; } else { switch(choice) { case 1: cout<<"Enter the number"<<"\n"; cin>>num1; for(i=num1;i>0;i--) { fact=fact*i; } cout<<"The factorial of "<<num1<<" is "<<fact<<"\n"; break; case 2: cout<<"Enter the term number upto which the series is to be calculated"<<"\n"; cin>>num1; num2=0; num3=1; num4=0; cout<<num2<<" "<<num3<<" "; for(i=0;i<num1-2;i++) { num4=num2+num3; cout<<num4<<" "; num2=num3; num3=num4; } break;

case 3: cout<<"Enter a number"<<"\n"; cin>>num1; for(i=2;i<=num1/2;i++) { if(num1%i==0) { cout<<"The number isn't prime"<<"\n"; a=1; break; } } if(a==0) cout<<"The number is prime"<<"\n"; break; case 4: cout<<"Enter the three numbers"<<"\n"; cin>>num2>>num3>>num4; if(num2>num3&&num2>num4) large=num2; else if(num3>num2&&num3>num4) large=num3; else large=num4; cout<<"The largest is "<<large<<"\n"; break; } } cout<<"Do you want to continue"<<"\n"; cin>>ch; if(ch=='y'||ch=='Y') goto label; }

Output
CALCULATOR 1. Factorial 2. Fibonacci 3. Prime Number 4. Largest Enter your choice 1 Enter the number 3 Factorial of 3 is 6 Do you want to continue n

6. Program to implement selection sort


#include<stdio.h> #include<conio.h> void main() { int arr[50],size,i,cont; label: cout<<"Enter the size of arrary(max. 50)<<"\n"; cin>>size; if(size>50) { cout<<"Size entered was greater than 50<<"\n"; goto label; } cout<<"Enter the elements of array"<<"\n"; for(i=0;i<size;i++) { cout<<"Enter element no."<<i+1<<"\n"; cin>>arr[i]; } for(int j=0;j<size;j++) { for(int k=j;k<size;k++) { if(arr[k]<arr[j]) { cont=arr[j]; arr[j]=arr[k]; arr[k]=cont; } } } cout<<"The array in incresing order is<<"\n"; for(i=0;i<size;i++) cout<<arr[i]<<' '; }

Output
Enter the size of array 5 Enter the size of array 5 Enter element no. 1 3 Enter element no. 2 2 Enter element no. 3 5 Enter element no. 4 4 Enter element no. 5 1 The array in increasing order is 12345

7. Program to implement insertion sort


#include<iostream.h> #include<conio.h> void main() { int arr[50],size,i,j,tmp; cout<<"Enter the size of array(max. 50)"<<"\n"; cin>>size; cout<<"Enter the elements of array"<<"\n"; for(i=0;i<size;i++) { cout<<"Enter element no."<<i+1<<"\n"; cin>>arr[i]; } for(i=1;i<size;i++) { j=i; while(j>0&&arr[j-1]>arr[j]) { tmp=arr[j]; arr[j]=arr[j-1]; arr[j-1]=tmp; j--; } } cout<<"The sorted array is"<<"\n"; for(i=0;i<size;i++) cout<<arr[i]<<' '; }

Output
Enter the size of array 5 Enter the size of array 5 Enter element no. 1 3 Enter element no. 2 2 Enter element no. 3 5 Enter element no. 4 4 Enter element no. 5 1 The array in increasing order is 12345

8. Program to demonstrate multiple inheritance


#include<iostream.h> #include<conio.h> class A { protected: int m; public: void func1(int x); }; class B { protected: int n; public: void func2(int y); }; class C:public A,public B { public: void display(); }; void A::func1(int x) { m=x; } void B::func2(int y) { n=y; } void C:: display() { cout<<"m ="<<m<<endl; cout<<"n = "<<n<<endl; } void main() { C obj; obj.func1(15); obj.func2(16); obj.display(); }

Output
m=15 n=16

9. Program to push or pop elements in the stack


#include<iostream.h> #include<conio.h> void push(int stack[],int *top, int num) { if(*top<5) { *top=*top + 1; stack[*top]=num; } else { cout<<"The stack is full. Can't push"<<"\n"; } } int pop(int stack[], int *top) { int num; if(*top>0) { num=stack[*top]; *top=*top-1; return(num); } else { cout<<"The stack is empty. Can't pop"<<"\n"; return(0); } } void main() { int stack[5],top=0,n,num,c; char ch; label: cout<<"1.PUSH"<<"\n"; cout<<"2.POP"<<"\n"; cout<<"Enter your choice"<<"\n"; cin>>c; switch(c) { case 1: cout<<"Enter the element to be pushed"<<"\n"; cin>>num; push(stack,&top,num); break; case 2: n=pop(stack,&top); if(n>0) cout<<"The popped element is "<<n<<"\n"; break;

default: goto label; } cout<<"Do you want to continue"<<"\n"; cin>>ch; if(ch=='y'||ch=='Y') goto label; }

Output
1.PUSH 2.POP Enter your choice 2 The stack is empty. Can't pop Do you want to continue y 1.PUSH 2.POP Enter your choice 1 Enter the element to be pushed 3 Do you want to continue y 1.PUSH 2.POP Enter your choice 2 The popped element is 3 Do you want to continue n

10. Program to implement insertion,deletion and searching operation in Queue


#include<iostream.h> #include<conio.h> void insert(int x) { if((front==0&&rear==n)||(front==rear+1)) { cout<<"OVERFLOW!! Queue is full"); return; } if(front==-1) { front=0; rear=0; } else if(rear==n) rear=0; else rear=rear+1; queue[rear]=x; } void delete() { if(front==-1) { cout<<"UNDERFLOW!! Queue is empty"<<"\n"; return; } cout<<queue[front]<<" Deleted"<<"\n"; queue[front]=0; if(front==rear) { front=-1; rear=-1; } else if(front==n) front=0; else front=front+1; return; } void display(void); { int i; cout<<"Displaying Queue"<<"\n"; for(i=0;i<3;i++)

cout<<queue[i]<<' '; } int queue[3]; int rear=-1; int front=-1; void main() { int n,choice; char ch; label: cout<<"MENU"<<"\n"; cout<<"1. Insertion"<<"\n"; cout<<"2. Deletion"<<"\n"; cout<<"3. Display"<<"\n"; cout<<"Enter your choice"<<"\n"; cin>>ch; switch(choice) { case 1: cout<<"Enter the no. to be inserted"<<"\n"; cin>>n; insert(n); break; case 2 : delete(); break; case 3: display(); break; default: goto label; } cout<<"Do you want to continue?"<<"\n"; cin>>ch; if(ch=='y'||ch=='Y') goto label; }

Output
MENU 1. Insertion 2. Deletion 3. Display Enter your choice 2 UNDERFLOW!! Queue is empty Do you want to continue? Y MENU

1. Insertion 2. Deletion 3. Display Enter your choice 1 Enter the no. to be inserted 42 Do you want to continue? Y MENU 1. Insertion 2. Deletion 3. Display Enter your choice 2 42 Deleted Do you want to continue? N

You might also like