Operations On 1-d Array in C++

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Program No.

15
Object: Write a menu driven program to show following operations in a 1-d array(using user defined function)
Menu:-
1. Creation of an array
2. Searching array using
 Linear Search
 Binary Search
3. Sorting array using
 Selection Sort
 Bubble Sort
 Insertion Sort
 Merge Sort
4. Merge two arrays of integer in ascending order and descending order.
5. Inserting an element at Ith position.
6. Deleting an element from an array
Quit
Header files used:
 #include<iostream>
 #include<process.h>

Source code:
#include <bits/stdc++.h>
#include<iostream>
#include<process.h>
using namespace std;
void enter()
{
int n,array[50];
cout<<"\nEnter the size of the array:";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter the "<<i+1<<" element :";
cin>>array[i];
}
cout<<"\nArray created.";
}
void linsearch()
{
int arr[10], i, num, n, c=0, pos;
cout<<"\nEnter the array size : ";
cin>>n;
for(i=0; i<n; i++)
{
cout<<"Enter the "<<i+1<<" element of the array:";
cin>>arr[i];
}
cout<<"Enter the number to search :";
cin>>num;
for(i=0; i<n; i++)
{
if(arr[i]==num)
{
c=1;
pos=i+1;
break;
}
}
if(c==0)
cout<<"Number not found..!!";
else
cout<<num<<" found at position "<<pos;
}
void binary_search()
{
int arr_search[50], i, element,n;
cout<<"\nEnter the no. of elements in array:";
cin>>n;
cout <<"\nEnter elements for Searching : " << endl;
for (i=0;i<n;i++)
{
cout<<"Enter the "<<i+1<<" element: ";
cin>>arr_search[i];
}
cout <<"\nYour Entered Array is:";
for (i = 0; i <n; i++)
{
cout << "\t" << arr_search[i];
}
cout << "\nEnter Element to Search : ";
cin>>element;
int f=0, r=n, mid;
while(f<=r)
{
mid=(f+r)/2;
if (arr_search[mid] == element) {
cout<<"\nYou searched: "<<element<<"\nElement Found at Position: "<<mid+1<<".\n";
break;
} else if(arr_search[mid]<element)
f=mid+1;
else
r=mid-1;
}
if(f>r)
cout <<"\nYou Searched: "<<element<<"\nElement Not Found \n";
}
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void selectionSort()
{
int arr[50],i,n;
cout<<"Enter the size of the array:";
cin>>n;
cout<<"\nEnter the array:";
for(i=0;i<n;i++)
{
cout<<"\n";
cin>>arr[i];
}
int j, min_idx;
for (i = 0; i < n-1; i++)
{
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
swap(&arr[min_idx], &arr[i]);
}
cout<<"Sorted array is: ";
for(i=0;i<n;i++)
cout<<arr[i]<<"\t";
cout<< endl;
}
void bubbleSort()
{
int arr[50],i,n;
cout<<"Enter the size of the array:";
cin>>n;
cout<<"\nEnter the array:";
for(i=0;i<n;i++)
{
cout<<"\n";
cin>>arr[i];
}
int j;
bool swapped;
for (i = 0; i < n-1; i++)
{
swapped=false;
for (j = 0; j < n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
swap(&arr[j], &arr[j+1]);
swapped = true;
}
}
if (swapped == false)
break;
}
cout<<"Swapped array is: ";
for(i=0;i<n;i++)
cout<<arr[i]<<"\t";
}
void insertionSort()
{
int arr[50],i,n;
cout<<"Enter the size of the array:";
cin>>n;
cout<<"\nEnter the array:";
for(i=0;i<n;i++)
{
cout<<"\n";
cin>>arr[i];
}
int key,j;
for(i=1;i<n;i++)
{
key=arr[i];
j=i-1;
while(j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
cout<<"Sorted array is: ";
for (i = 0; i < n; i++)
cout<<arr[i]<<"\t";
}
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
int m = l+(r-l)/2;
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);
merge(arr, l, m, r);
}
}
void printArray(int A[], int size)
{
int i;
for (i=0; i < size; i++)
cout<<A[i]<<"\t";
}
void sortedMerge(int a[], int b[], int res[],int n, int m)
{
int i = 0, j = 0, k = 0;
while (i < n) {
res[k] = a[i];
i += 1;
k += 1;
}
while (j < m) {
res[k] = b[j];
j += 1;
k += 1;
}
sort(res,res+n+m);
}
int* insertX(int n, int arr[],int x, int pos)
{
int i;
n++;
for (i = n; i >= pos; i--)
arr[i] = arr[i - 1];
arr[pos - 1] = x;
return arr;
}
int deleteElement(int arre[], int q, int l)
{
int g;
for (g=0; g<q; g++)
if (arre[g] == l)
break;
if (g < q)
{
q = q - 1;
for (int j=g; j<q; j++)
arre[j] = arre[j+1];
}
return q;
}
int main()
{
int z,choice,ret,c;
char ch;
do
{
cout<<" Menu:";
cout<<"\n 1-Creation of an array.";
cout<<"\n 2-Searching in array";
cout<<"\n 3-Sorting an array";
cout<<"\n 4-Merge two arrays of integer in ascending order and descending order.";
cout<<"\n 5-Inserting an element at Ith position.";
cout<<"\n 6-Deleting an element from an array.";
cout<<"\n 7-Quit.";
cout<<"\n Enter your choice(1-7):";
cin>>z;
switch (z)
{
case 1: enter();
break;
case 2: cout<<"1-Linear Search. \n2-Binary Search.";
cout<<"\nEnter the type of search:";
cin>>choice;
if(choice==1)
linsearch();
if(choice==2)
binary_search();
break;
case 3: cout<<"\n1-Selection Sort \n2-Bubble Sort \n3-Insertion Sort \n4-Merge Sort";
cout<<"\nEnter choice:";
cin>>c;
if(c==1)
selectionSort();
if(c==2)
bubbleSort();
if(c==3)
insertionSort();
if(c==4)
{
int i,n,arr[50];
cout<<"Enter the size of the array:";
cin>>n;
for(i=0;i<n;i++)
cin>>arr[i];
cout<<"\n";
mergeSort(arr, 0,n-1);
cout<<"Sorted array is: ";
printArray(arr,n);
}
break;
case 5: int arr[50],i, x, pos,n;
cout<<"Enter the size of the array: ";
cin>>n;
cout<<"Enter the array: \n";
for (i = 0; i <n; i++)
cin>>arr[i];
cout<<"\nOriginal array is: ";
for (i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
cout<<"\nEnter the element to insert: ";
cin>>x;
cout<<"\nEnter the position for insertion: ";
cin>>pos;
insertX(n, arr, x, pos);
cout<<"\nUpdated array is: ";
for (i = 0; i < n + 1; i++)
cout << arr[i] << " ";
cout << endl;
break;
case 6: int arre[50];
int q;
int l,g;
cout<<"Enter the size of the array: ";
cin>>q;
cout<<"Enter the array: \n";
for (g = 0; g <q; g++)
cin>>arre[g];
cout<<"\nOriginal array is: ";
for (g = 0; g < q; g++)
cout << arre[g] << " ";
cout << endl;
cout<<"\nEnter the element to delete: ";
cin>>l;
q = deleteElement(arre, q, l);
cout << "Modified array is: ";
for (int g=0; g<q; g++)
cout << arre[g] <<"\t";
break;
case 7: exit(0);
}
cout<<"\n\nDo you want to continue(y/n): ";
cin>>ch;
}while(ch=='Y'||ch=='y');
return 0;
}
Output:

You might also like