Write a program to test the function seqOrderedSearch. Use either the function b
ID: 3534980 • Letter: W
Question
Write a program to test the function seqOrderedSearch. Use either the function bubbleSort or selectionSort to sort the list before the search.
What I have so far. What goes in the elements input when running the program?
#include
"stdafx.h"
#include
<iostream>
using
namespace std;
void
bubblesort (int list[],int length);
int
seqsearch(int list[],int len,int ele);
void
main()
{
int list[20],n,i;
int search;
int ele;
cout<<
"Enter the size of the list:";
cin>>n;
cout<<
"Enter elements:";
for(i=0;i<n;i++)
{
cin>>list[i];
}
cout<<
"Enter element to look up:" ;
cin>> ele;
bubblesort (list,n) ;
search=seqsearch(list,n,ele);
if(search==-1)
{
cout<<
"Search Was Unsuccessful";
}
else
cout<<
"Element Found at:"<<search;
system(
"pause");
}
void
bubblesort (int list[],int size)
{
int temp, i,j;
for(i=0;i<size;i++)
{
for (j=i,j<size;j++;)
{
if (list [j]>list[j+1])
{
temp=list[j];
list[j]=list[j+1];
list[j+1]=temp;
}
}
}
}
int
seqsearch (int list [], int size, int element)
{
int loc;
bool found = false;
for(loc=0 ; loc<size;loc++)
{
if(list[loc]>=element)
{
found=
true;
break;
}
}
if(found)
if(list[loc]==element)
return loc;
else
return -1;
else
return -1;
}
Explanation / Answer
#include<iostream.h>
#include<conio.h>
void main()
{
int n,a[10],i,j,k,t;
clrscr();
cout<<"enter the size of an array ";
cin>>n;
cout<<"enter the elements in an array ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
cout<<"array before sorting:"<<a[i]<<endl;
}
for(j=0;j<n;j++)
{
k=j;
while(k>0&&a[k]<a[k-1])
{
t=a[k];
a[k]=a[k-1];
a[k-1]=t;
k--;
}
}
for(i=0;i<n;i++)
{
cout<<"array after sorting"<<a[i]<<endl;
}
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.