Code:
#include iostream
using namespace std;
// A function to perform linear search.
int Search(int *a, int n, int item)
{
int i,j;
for(i = 0; i < n; i++)
{
// Linearly search for the item.
if(item == a[i])
{
cout<<"The element "<
// Break if the item is found.
break;
}
// If index reaches to the end then the item is not there.
if(i == n-1)
{
cout<<"\nThe element not found.";
return -1;
}
}
// Shift each element before matched item.
for(j = i; j > 0; j--)
a[j] = a[j-1];
// Put the recently searched item at the beginning of the data array.
a[0] = item;
return 0;
}
int main()
{
int i, n, a[20]={89, 53, 95, 1, 9, 67, 72, 66, 75, 77, 18, 24, 35, 90, 38, 41, 49, 81, 27, 97};
char ch;
// Initial status of the data array.
cout<<"\nThe status of the array: ";
for(i = 0; i < 20; i++)
cout<
up:
cout<<"\nEnter the Element to be searched: ";
cin>>n;
// Print the updated data array.
if(Search(a, 20, n) != -1)
{
cout<<"\nThe status of the array after this search: ";
for(i = 0; i < 20; i++)
cout<
}
// Ask user to enter choice for further searching.
cout<<"\n\n\tDo you want to search more...enter choice(y/n)?";
cin>>ch;
if(ch == 'y' || ch == 'Y')
goto up;
return 0;
}
Output:
Case 1:
The status of the array: 89 53 95 1 9 67 72 66 75 77 18 24 35 90 38 41 49 81 27 97
Enter the Element to be searched: 97
The element 97 found at 19 index.
The status of the array after this search: 97 89 53 95 1 9 67 72 66 75 77 18 24 35 90 38 41 49 81 27
Do you want to search more...enter choice(y/n)?y
Enter the Element to be searched: 24
The element 24 found at 12 index.
The status of the array after this search: 24 97 89 53 95 1 9 67 72 66 75 77 18 35 90 38 41 49 81 27
Do you want to search more...enter choice(y/n)?y
Enter the Element to be searched: 38
The element 38 found at 15 index.
The status of the array after this search: 38 24 97 89 53 95 1 9 67 72 66 75 77 18 35 90 41 49 81 27
Do you want to search more...enter choice(y/n)?y
Enter the Element to be searched: 97
The element 97 found at 2 index.
The status of the array after this search: 97 38 24 89 53 95 1 9 67 72 66 75 77 18 35 90 41 49 81 27
Do you want to search more...enter choice(y/n)?n
More C++ Programs: