Mix of Many Array Programs with User Defined Functions and Menu Driven in C++
//Mix of Many Array Programs with menu driven choice and functions
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<iomanip.h>
//Global variable for array size
int const size=50;
void FillArray(int ar[], int &n)
{
randomize();
for(int i=0;i<n;i++)
ar[i]=random(91)+10;
}
void ShowArray(int ar[], int &n)
{
cout<<"\nThe Array contains the following values\n";
for(int i=0;i<n;i++)
cout<<setw(5)<<ar[i];
cout<<endl;
}
void SumArray(int ar[], int &n)
{
int sum=0;
ShowArray(ar,n);
for(int i=0;i<n;i++)
sum+=ar[i];
cout<<"\nThe Sum of Array is : "<<sum<<endl;
}
void AvgArray(int ar[], int &n)
{
int sum=0;
float avg;
ShowArray(ar,n);
for(int i=0;i<n;i++)
sum+=ar[i];
avg=(float)sum/n;
cout<<"\nThe Average of Array is : "<<avg<<endl;
}
void SelectionSort(int arr[], int &n)
{
int temp, i, j, p;
cout<<"\nThe Original Array is : ";
for(j=0;j<n;j++)
{
cout<<setw(4)
<<arr[j];
}
cout<<endl;
for(i=0;i<n-1;i++)
{
p=i;
for(j=i+1;j<n;j++)
{
if(arr[p]>arr[j])
p=j;
}
temp=arr[p];
arr[p]=arr[i];
arr[i]=temp;
cout<<"Step-"<<i+1<<": ";
for(j=0;j<n;j++)
{
textcolor(YELLOW);
if(j==i)
cprintf("%4d",arr[j]);
else
cout<<setw(4)<<arr[j];
textcolor(7);
}
cout<<endl;
}
cout<<"\nFinally Array Sorted in Ascending Order\n";
for(i=0;i<n;i++)
{
cout<<setw(4)<<arr[i];
}
cout<<endl;
}
void BubbleSort(int arr[], int &n)
{
int i, j, temp;
cout<<"The Original Array is : ";
for(j=0;j<n;j++)
{
cout<<setw(4)<<arr[j];
}
cout<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
cout<<"Step-"<<i+1<<": ";
for(j=0;j<n;j++)
{
cout<<setw(4)<<arr[j];
}
cout<<endl;
}
cout<<"\nThe Sorted Array is : ";
for(j=0;j<n;j++)
{
cout<<setw(4)<<arr[j];
}
cout<<endl;
}
int LinearSearch(int arr[], int &n, int s)
{
int i,pos=-1;
for(i=0;i<n;i++)
{
if(s==arr[i])
{
pos=i;
return pos;
}
}
return pos;
}
int BinarySearch(int arr[], int &n, int s)
{
int i,pos=-1;
int first=0, last=n-1, mid;
while(first<=last)
{
mid=(first+last)/2;
if(s==arr[mid])
{
pos=mid;
return pos;
}
else if(s<=arr[mid])
last=mid-1;
else if(s>=arr[mid])
first=mid+1;
}
return pos;
}
void InsertInArray(int arr[], int &n, int val)
{
if(n==size-1 || n>=size)
{
cout<<"\nArray overflow\n";
cout<<"\nThe value cannot be inserted....\n";
}
else
{
arr[n]=val;
n++;
cout<<"\nThe Array updated...\n";
}
}
void DeleteFromLast(int arr[], int &n)
{
if(n<0)
{
cout<<"\nArray is Empty....\n";
}
else
{
cout<<"The value deleted is : "<<arr[n-1]<<endl;
n--;
}
}
void DeleteFromFirst(int arr[], int &n)
{
if(n<0)
{
cout<<"\nArray is Empty....\n";
}
else
{
cout<<"The value deleted is : "<<arr[0]<<endl;
for(int i=0;i<n-2;i++)
{
arr[i]=arr[i+1];
}
n--;
}
}
void main()
{
int ch, n=-1, a[size];
int num, index;
do
{
clrscr();
cout<<"\tArray Options\n";
cout<<"==============================\n";
cout<<"0. Exit\n";
cout<<"1. Fill Random Values in Array\n";
cout<<"2. Show Values of Array\n";
cout<<"3. Sum of Array\n";
cout<<"4. Average of Array\n";
cout<<"5. Display in Ascending Order using Selection Sort Method\n";
cout<<"6. Display in Descending Order using Bubble Sort Method\n";
cout<<"7. Search using Linear Search Concept\n";
cout<<"8. Search using Binary Search Concept\n";
cout<<"9. Insert new Value in Array\n";
cout<<"10.Delete from Last\n";
cout<<"11.Delete from First\n";
cout<<"Enter your choice : ";
cin>>ch;
switch(ch)
{
case 1: cout<<"Enter Size of Array: Max("<<size<<"): ";
cin>>n;
FillArray(a,n);
cout<<"\nThe Array Values has been filled....\n";
break;
case 2: ShowArray(a,n);
cout<<"\nThe Array has been displayed....\n";
break;
case 3: SumArray(a,n); break;
case 4: AvgArray(a,n); break;
case 5: SelectionSort(a,n); break;
case 6: BubbleSort(a,n); break;
case 7:
cout<<"\nLinear Search Concept\n";
cout<<"Enter the number you want to search in Array: ";
cin>>num;
index=LinearSearch(a,n,num);
if(index==-1)
{
ShowArray(a,n);
cout<<"\nThe Number "<<num
<<" is not in array.\n";
}
else
{
ShowArray(a,n);
cout<<"\nThe Number "<<num
<<" found at index no. "<<index<<endl;
}
break;
case 8:
cout<<"\nBinary Search Concept\n";
cout<<"Enter the number you want to search in Array: ";
cin>>num;
index=BinarySearch(a,n,num);
if(index==-1)
{
ShowArray(a,n);
cout<<"\nThe Number "<<num
<<" is not in array.\n";
}
else
{
ShowArray(a,n);
cout<<"\nThe Number "<<num
<<" found at index no. "<<index<<endl;
}
break;
case 9:
cout<<"\nEnter the new value to insert in array: ";
cin>>num;
InsertInArray(a,n,num);
ShowArray(a,n);
break;
case 10: DeleteFromLast(a,n); break;
case 11: DeleteFromFirst(a,n); break;
}getch();
}while(ch);
}
No comments
Post your comments