Need help with this C++ program to write a version of the sequential search algo
ID: 3719546 • Letter: N
Question
Need help with this C++ program to write a version of the sequential search algorithm that can be used to search a sorted list. Write a program to test the sequential search algorithm. Use either the function bubbleSort or insertionSort to sort the list before the search. Your program should prompt the user to enter 10 digits and then prompt the user to enter a digit to search - if the list contains this digit, output its position to the console:
If the digit is not contained in the list, output the following:
Explanation / Answer
#include <iostream>
using namespace std;
int main()
{
int arr[10],tmp,flag=0,pos;
cout<<"Enter elements in the array :";
for(int i=0;i<10;i++)
cin>>arr[i];
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
if(arr[i]<arr[j])
{
tmp=arr[i];
arr[i]= arr[j];
arr[j]=tmp;
}
}
}
int digit;
cout<<"Enter the digit to be searched :";
cin>>digit;
for(int i=0;i<10;i++)
{
if(arr[i]==digit)
{
flag=1;
pos=i+1;
break;
}
else
flag=0;
}
if(flag)
cout<<digit<<" is found at Position "<<pos<<endl;
else
cout<<digit<<" is not in the list ";
}