1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
#include<iostream>
#include<cstdlib>
using namespace std ;
int search(int e,int size, int a[]){
int loc = -1 ;
for(int i=0 ; i<size; i++){
if(a[i]== e){
loc=i;
break;
}
}
return loc ;
}
int del(int e,int size, int a[]){
int loc = search(e,size,a);
if(loc != -1){
for(int i=loc ; i<size ; i++){
a[i]=a[i+1];
}
size = size - 1;
}
return loc ;
}
int add(int e , int size , int a[] , int loc){
int flag = -1 ;
if(loc < size){
flag = 0 ;
for(int i=size; i>=(loc-1);i--){
a[i]=a[i-1];
}
a[loc-1]= e;
}
return flag ;
}
void sort(int size , int a[]){
for(int pass=1; pass <= size-1; pass++){
for(int j=0;j <= (size-pass)-1; j++){
if(a[j] > a[j+1]){
int temp ;
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp ;
}
}
}
cout<<"\nSorted Array";
for(int k=0;k<size;k++){
cout<<" "<<a[k];
}
}
int main () {
const int size = 10 ;
int arr[size] = {134,32,45,23,-1,0,37,23,17,10};
int loc = -1 ;
int e , de ,ans ;
cout<<"\t\t\t\tARRAY OPERATIONS";
while(true){
cout<<"\n\n1. Traversal.";
cout<<"\n2. Search.";
cout<<"\n3. Deletion";
cout<<"\n4. Addition.";
cout<<"\n5. Sort.";
cout<<"\n6. Exit.";
cout<<"\nWhich operation would you like to carry ";
cin>>ans;
switch(ans){
case 1 :
cout << "\nTraversal:\n";
for(int i=0 ; i<10; i++){
cout<<" "<<arr[i];
}
break;
case 2 :
cout << "\n\nEnter the element to search ";
cin >> e;
if((loc=search(e,size,arr))!= -1) {
cout<<"\nThe location of the element is "<< (loc+1);
}else {
cout<<"\nElement not found !!";
}
break;
case 3 :
cout <<"\n\nEnter the element to delete";
cin >> de ;
if(del(de,size,arr)!= -1 ){
cout << "\nElement deleted successfully !!";
cout << "\nNew Array: ";
for(int i=0 ; i<(size-1);i++){
cout<<" "<<arr[i];
}
}else {
cout << "Element not found !!";
}
break;
case 4 :
cout<<"\nEnter any element at a particular location(ele loc)";
cin>>e>>loc;
if(add(e,size,arr,loc)!= -1 ){
cout<<"\nElement added successfully!!";
cout<<"\nNew Array:";
for(int i=0;i<(size+1);i++){
cout<<" "<<arr[i];
}
}else {
cout<<"Element could not be added !!";
}
break;
case 5 :
sort(size,arr);
break;
case 6:
exit(0);
break;
default:
cout<<"Invalid argument. Exiting program...";
}
}
return 0 ;
}
|