Sie sind auf Seite 1von 31

[1] //Linear Search #include<iostream.h> #include<conio.

.h> void main() { int i, item, loc=0; static int a[10]; clrscr(); for (i = 0; i<10; i++) { cout << "Enter The Element : "; cin >> a[i]; } cout << "Enter The Element To Be Search : "; cin >> item; for (i = 0; i<10; i++) { if (item == a[i]) { loc = i; break; } } if (loc !=0) cout << "Item found in location : " << loc+1; else cout << "Item Not Found"; getch(); } OUTPUT Enter The Element : 1 Enter The Element : 2 Enter The Element : 3 Enter The Element : 4 Enter The Element : 5 Enter The Element : 6 Enter The Element : 7 Enter The Element : 8 Enter The Element : 9 Enter The Element : 10 Enter The Element To Be Search : 5 Item found in location : 5

[2] //Binary Search #include<iostream.h> #include<conio.h> const int N = 10; void main() { int beg, end, mid, item,i; int loc = 0; static int a[N]={10,14,19,23,30,40,50,60,70,80}; clrscr(); cout<<"\n Array is:\n"; for (i = 0;i<N;i++) { cout<<a[i] << " " ; } cout<< "\nEnter The Item To Be Search : "; cin >> item; beg = 0; end = N; mid = (beg + end)/2; while (beg <= end) { if(item < a[mid]) end = mid - 1; else beg = mid + 1; mid = (beg + end)/2; } if (item == a[mid]) loc = mid+1; else loc=0; if (loc == 0) cout << "Item Not Found"; else cout << "Item Found In Location : "<< loc; getch(); } OUTPUT Array is: 10 14 19 23 30 40 50 60 70 80 Enter The Item To Be Search : 10 Item Found In Location : 1

[3] //Insertion in array #include<iostream.h> #include<conio.h> class array { private: int *a; public: array(int); void getArray(int); void showArray(int); void insert(int); }; array :: array(int n) { a = new int[n]; } void array :: getArray(int n) { cout << endl << "Input " <<n<< " Array Elements\n"; for (int i = 0; i < n; i++) cin>>a[i]; } void array :: showArray(int n) { cout << endl <<"Array is \n"; for(int i = 0; i < n; i++) cout<<a[i]<<" "; } void array :: insert(int n) { int ele,pos; cout<<endl<<"Input Element To Be Inserted And Its Position :" ; cin>>ele>>pos; for (int i = n; i >= pos-1; i--) a[i+1] = a[i]; a[pos-1] = ele; }

void main() { int n; clrscr(); cout<<"Insert Required Array Size(<=50) "; cin>>n; array aa(n); aa.getArray(n); aa.showArray(n); aa.insert(n); cout<< endl<<"After Insertion :"<<endl; aa.showArray(n+1); getch(); } OUTPUT Insert Required Array Size(<=50) 10 Input 10 Array Elements 1 2 3 4 5 6 7 8 9 10 Array is 1 2 3 4 5 6 7 8 9 10 Input Element To Be Inserted And Its Position :5 5 After Insertion : Array is 1 2 3 4 5 5 6 7 8 9 10

[4] //Deletion from array #include<iostream.h> #include<conio.h> const int size = 50; class array { private: int *a; public: array(int); void getArray(int); void showArray(int); void del(int); }; array :: array(int n) { a = new int[n]; } void array :: getArray(int n) { cout<<endl<<"Input "<<n<<" Array Elements\n"; for (int i= 0; i<n ; i++) cin>>a[i]; } void array :: showArray(int n) { cout<<endl<<"Array is : \n"; for (int i= 0; i<n; i++) cout<<a[i]<<" "; } void array :: del(int n) { int pos; cout<<endl<<"Input Position Of Element To Be Deleted : "; cin>>pos; for (int i = pos-1; i < n; i++) a[i] = a[i+1]; }

void main() { int n; clrscr(); cout<<"Input Required Size Of Array(<=50) :"; cin>>n; array aa(n); aa.getArray(n); aa.showArray(n); aa.del(n); cout<<endl<<"After Deletion :"<<endl; aa.showArray(n-1); getch(); } OUTPUT Input Required Size Of Array(<=50) :10 Input 10 Array Elements 1 2 3 4 5 6 7 8 9 10 Array is : 1 2 3 4 5 6 7 8 9 10 Input Position Of Element To Be Deleted : 7 After Deletion : Array is : 1 2 3 4 5 6 8 9 10

[5] //Largest and Smallest in Array #include<iostream.h> #include<conio.h> void main() { int i,a[50],n,max,min; clrscr(); cout<<"\n Enter the range of array : "; cin>>n; cout<<"\n\nEnter the elements in array :\n"; for(i=0;i<n;i++) { cin>>a[i]; } max=a[0]; min=a[0]; for(i=0;i<n;i++) { if(a[i]>max) max=a[i]; if(a[i]<min) min=a[i]; } cout<<"\n\nLargest element in the array : "<<max; cout<<"\n\nSmallest element in the array : "<<min; getch(); } OUTPUT Enter the range of array : 10

Enter the elements in array : 34 75 87 65 87 56 9 25 66 89

Largest element in the array : 89 Smallest element in the array : 9

[6] //Merging of Array #include<iostream.h> #include<conio.h> const int M = 3; const int N = 5; void main() { void merging(int *, int, int *, int, int *); int A[M] = {22, 66,88}; int B[N] = {11, 33, 44, 77, 99}; int C[M+N], i; clrscr(); cout<<"\n\nArray A is : \n"; for (i = 0; i<M; i++) cout << A[i]<<" "; cout <<"\n\nArray B is : \n"; for (i = 0; i<N; i++) cout<<B[i]<<" "; merging(A,M,B,N,C); cout <<"\n\nArray C is : \n"; for (i = 0; i<M+N; i++) cout<<C[i]<<" "; getch(); } void merging(int *a, int r, int *b, int s, int *c) { int na = 0, nb = 0, ptr = 0; while(na < r && nb < s) { if (a[na] < b[nb]) { c[ptr] = a[na]; ptr++; na++; } else { c[ptr] = b[nb]; ptr++; nb++; }

} if ( na>= r) { for (int k = 0; k<=s-nb; k++) { c[ptr+k] = b[nb+k]; } if (nb >= s) { for (int k = 0; k<= r-na; k++) c[ptr+k] = a[na+k]; } } } OUTPUT Array A is : 22 66 88 Array B is : 11 33 44 77 99 Array C is : 11 22 33 44 66 77 88 99

[7] //Bubble Sort #include<iostream.h> #include<conio.h> const N = 5; void main() { int i, j, temp; static int a[N]; clrscr(); cout<<"\n\nEnter The "<<N<<" Element : \n"; for ( i = 0; i < N; i++) { cin>>a[i]; } cout << "\n Original Array Is : \n"; for (i = 0; i<N; i++) { cout<<a[i]<<"\t"; } for (i = 0; i < N-1; 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; } } } cout << "\n Sorted Array is : \n"; for ( i = 0; i < N; i++) { cout<<a[i]<<"\t"; } getch(); }

OUTPUT Enter The 5 Element : 13524 Original Array Is : 1 3 5 2 Sorted Array is : 1 2 3 4

4 5

[8] //Insertion Sort #include<iostream.h> #include<conio.h> const int m = 20; class sort { int a[m], k, n, minval, loc; public: void getdata(); void putdata(); void inssort(); }; void sort :: getdata() { cout<<"\n Enter Number Of Elements In Array : "; cin>>n; cout << "\n\n Enter Element In Array : \n"; for (k = 1; k <= n; k++) cin>>a[k]; } void sort :: putdata() { cout<<"\n\t After Sorting :\n\n Elements In Array Are : \n"; for (k = 1; k<=n; k++) cout<<a[k]<<" "; } void sort :: inssort() { a[0] = -32768; for (k= 2; k<=n; k++) { int temp = a[k]; int ptr = k-1; while ( temp < a[ptr]) { a[ptr + 1] = a[ptr]; ptr = ptr - 1; } a[ptr + 1] = temp; } }

void main() { sort ins; clrscr(); ins.getdata(); ins.inssort(); ins.putdata(); getch(); } OUTPUT Enter Number Of Elements In Array : 5

Enter Element In Array : 13524 After Sorting : Elements In Array Are : 12345

[9] //Selection Sort #include<iostream.h> #include<conio.h> const m = 20; class sort { int a[m], k, n, minval, loc; public: void getdata(); void putdata(); void selsort(); int min(int k); }; void sort :: getdata() { cout<<"\n Enter No Of Elements In Array : "; cin>>n; cout<<"\n\n Enter Elements In Array : \n"; for (k = 0; k<n; k++) cin>>a[k]; } void sort :: putdata() { cout<<"\n Elements In Array Are : \n"; for (k = 0; k<n; k++) cout<<a[k]<<" "; } int sort :: min(int k) { minval = a[k]; loc = k; for (int j = k+1; j<n; j++) { if (minval > a[j]) { minval = a[j]; loc = j; } } return loc; }

void sort :: selsort() { for (k=0; k<n; k++) { min(k); int temp = a[k]; a[k] = a[loc]; a[loc] = temp; } cout<<"\n Sorted List Is : \n"; for (k=0; k<n; k++) { cout<<a[k]<<" "; } } void main() { sort s; clrscr(); s.getdata(); s.putdata(); s.selsort(); getch(); } OUTPUT Enter No Of Elements In Array : 5

Enter Elements In Array : 13524 Elements In Array Are : 13524 Sorted List Is : 12345

[10] //Transpose Of Matrix #include<iostream.h> #include<conio.h> #include<iomanip.h> main() { int a[3][3], i, j; clrscr(); cout<<"\n Enter The Element Of Matrix : \n\n"; for ( i=0; i<=2; i++) { for ( j=0; j<=2; j++) { cin>>a[i][j]; } } int t; for (i = 0; i<=2; i++) { for (j=0; j<=2; j++) { t = a[i][j]; a[i][j] = a[j][i]; a[j][i] = t; } } cout <<"\n Transpose Of Matrix is : \n"; for (i = 0; i<=2; i++) { for (j=0; j<=2; j++) { cout<<setw(4)<<a[j][i]; } cout<<"\n"; } getch(); }

OUTPUT Enter The Element Of Matrix : 1 2 3 4 5 6 7 8 9 Transpose Of Matrix is : 1 4 7 2 5 8 3 6 9

[11] //Largest and Smallest from Matrix #include<iostream.h> #include<conio.h> #include<iomanip.h> void main() { int i,a[3][3],j,max,min; clrscr(); cout<<"\n\nEnter the elements of matrix :\n"; for(i=0;i<=2;i++) { for(j=0;j<=2;j++) { cin>>a[i][j]; } } max=a[0][0]; min=a[0][0]; for(i=0;i<=2;i++) { for(j=0;j<=2;j++) { if(a[i][j]>max) max=a[i][j]; if(a[i][j]<min) min=a[i][j]; } } cout<<"\n\nLargest element in the matrix : "<<max; cout<<"\n\nSmallest element in the matrix : "<<min; getch(); } OUTPUT Enter the elements of matrix : 23 43 54 62 00 64 71 99 72 Largest element in the matrix : 99 Smallest element in the matrix : 0

[12] //Sum of each row and column #include<iostream.h> #include<conio.h> const int M = 3; const int N = 3; void main() { static int i, j, rsum[M], csum[N]; static int a[M][N]; clrscr(); for (i = 0; i<M; i++) { for (j=0; j<N; j++) { cout<<"Enter The Element : "; cin>>a[i][j]; } } cout<<"\n\n Matrix is : \n"; for (i = 0; i<M; i++) { for (j=0; j<N; j++) { cout<<a[i][j]<<"\t"; csum[j] = csum[j] + a[i][j]; rsum[i] = rsum[i] + a[i][j]; } cout<<"\n"; } cout<<"\n Sum Of Column Of Matrix Is :\n "; for (i=0; i<N; i++) cout<<"\n Sum Of Column "<<i<<" Is "<<csum[i]; cout<<"\n\n Sum Of Row Of Matrix Is : \n"; for (i=0; i<M; i++) cout<<"\n Sum Of Row Elements "<<i<<" Is "<<rsum[i]; getch(); }

OUTPUT Enter The Element : 9 Enter The Element : 8 Enter The Element : 7 Enter The Element : 6 Enter The Element : 5 Enter The Element : 4 Enter The Element : 3 Enter The Element : 2 Enter The Element : 1

Matrix is : 9 8 7 6 5 4 3 2 1 Sum Of Column Of Matrix Is : Sum Of Column 0 Is 18 Sum Of Column 1 Is 15 Sum Of Column 2 Is 12 Sum Of Row Of Matrix Is : Sum Of Row Elements 0 Is 24 Sum Of Row Elements 1 Is 15 Sum Of Row Elements 2 Is 6

[13] //LCM & GCD #include<iostream.h> #include<conio.h> class number { private: int a, b, lcm, gcd; public: void getdata(void); void showdata(void); int fun_gcd(int x, int y); }; void number :: getdata(void) { cout <<"\nEnter The 1st Number : "; cin>>a; cout<<"\nEnter The 2nd Number : "; cin>>b; } void number :: showdata(void) { gcd = fun_gcd(a,b); cout<<"\n GCD = "<<gcd; lcm = (a*b)/gcd; cout<<"\n LCM = "<<lcm; } int number :: fun_gcd(int x, int y) { if ( x % y == 0) return y; else return(fun_gcd(y, x % y)); } void main() { number n; clrscr(); n.getdata(); n.showdata(); getch(); }

OUTPUT Enter The 1st Number : 5 Enter The 2nd Number : 15 GCD = 5 LCM = 15

[14] //PUSH and POP operation for stack #include<iostream.h> #include<conio.h> void main() { int i,n,a[50]; clrscr(); cout<<"\n\nEnter the range of stack : "; cin>>n; for(i=1;i<=n;i++) { cout<<"\n\nEnter element "<<i<< " : "; cin>>a[i]; } cout<<"\n\nElements in stack : "; for(i=1;i<=n;i++) { cout<<"\t"<<a[i]; } for(i=n;i>=1;i--) { cout<<"\n\nDeleted element : "<<a[i]; } cout<<"\n\nAll elements deleted. Now stack is empty."; getch(); }

OUTPUT Enter the range of stack : 5 Enter element 1 : 4557 Enter element 2 : 55 Enter element 3 : 24 Enter element 4 : 789 Enter element 5 : 100 Elements in stack : 4557 55 24 789 100

Deleted element : 100 Deleted element : 789 Deleted element : 24 Deleted element : 55 Deleted element : 4557 All elements deleted. Now stack is empty.

[15] //Insert and Delete element in Queue #include<iostream.h> #include<conio.h> int MAX= 20; #include<stdlib.h> int queue[20]; int front = -1; int rear = -1 ; void push(int data) { if (rear == MAX-1) { cout<<"\n\t Queue is Full : "<<endl; getch(); } else { rear++; queue[rear] = data; if (rear == 0) { front = 0; } } } int pop(void) { int data; if (front == -1 && rear == -1) { cout<<"\n\t Queue Is Empty, Data Cannot Be Removed."<<endl; getch(); exit(0); } else { data = queue[front]; cout<<"\n\t Data Poped Is : "<<endl; if ( front == rear) { front = -1; rear = -1; } else { front++; }

} return(data); } void main(void) { int choice; int x; int data; while(1) { clrscr(); cout<<"\n\t 1. TO Insert Data "<<endl; cout<<"\n\t 2. To Delete Data "<<endl; cout<<"\n\t 3. To Exit "<<endl; cout<<"\n\t Enter your Choice "<<endl; cin>>choice; switch(choice) { case 1: { cout <<"\n Enter Data "<<endl; cin>>data; push(data); break; } case 2: { x = pop(); cout<<"\nData Removed is "<<x; getch(); break; } case 3: { cout<<"\n Exit"; getch(); exit(0); break; } } } }

OUTPUT 1. TO Insert Data 2. To Delete Data 3. To Exit Enter your Choice 1 Enter Data 20

1. TO Insert Data 2. To Delete Data 3. To Exit Enter your Choice 1 Enter Data 30

1. TO Insert Data 2. To Delete Data 3. To Exit Enter your Choice 2 Data Poped Is : Data Removed is 20

1. TO Insert Data 2. To Delete Data 3. To Exit Enter your Choice 2 Data Poped Is : Data Removed is 30

1. TO Insert Data 2. To Delete Data 3. To Exit Enter your Choice 2 Queue Is Empty, Data Cannot Be Removed.

[16] //Factorial using Recursion #include<iostream.h> #include<conio.h> void main() { int g, n, fact(int); clrscr(); cout<<endl<<"Enter The Number : "; cin>>n; g = fact(n); cout<<"\nFactorial Of "<<n<<" Is "<<g; getch(); } int fact(int m) { int f; if (m == 0) return(1); f = m * fact(m-1); return(f); } OUTPUT Enter The Number : 5 Factorial Of 5 Is 120

[17] //Fibonacci using Recursion #include<iostream.h> #include<conio.h> class fibonacci { private: int n; public: void getfibo() { cout<<endl<<"Enter The Number Up To Which Fibonacci Series Is Required :"; cin>>n; cout<<endl<<"\The Series Upto "<<n<<" number is : \n"; int a=1; while (a<=n) { cout <<fib(a)<<"\t"; a++; } } int fib(int m) { if (m == 1 || m==2) return(1); else return(fib(m-1) + fib(m-2)); } }; void main() { fibonacci f; clrscr(); f.getfibo(); getch(); } OUTPUT Enter The Number Up To Which Fibonacci Series Is Required :6 The Series Upto 6 number is : 1 1 2 3 5 8

[18] //Product of 2 numbers using Recursion #include<iostream.h> #include<conio.h> void main() { int n1, n2 , mul(int,int); clrscr(); cout<<"Enter The 1st Numbers : "; cin>>n1; cout<<"\nEnter The 2nd Number : "; cin>>n2; int n = mul(n1,n2); cout<<"\nProduct = "<<n; getch(); } mul(int a, int b) { if ( b == 0) return 0; int s = a+ mul(a,b-1); return s; } OUTPUT Enter The 1st Numbers : 5 Enter The 2nd Number : 6 Product = 30

Das könnte Ihnen auch gefallen