I'm workin on a porblem that basically compares a linear search to a binary search, what I want to do is be able to check for 5 different values, but this is where I run into a problem. When ever I start to use arrays and loops it starts to mess up my output.
Here is some code that works perfect, but it only does the search for one value ...
Code:
// Linear and Bin search
// ERIC LILLY
#include <iostream>
#include <stdlib.h>
#include <cstdlib>
using namespace std;
void swap2(int&m, int&n);
void main()
{
int a [10]={40,60,30,70,90,80,5,25,95,100};
int k;
bool notfound;
int searchvalue;
int probes=1;
int probes2=1;
int temp;
bool swap;
int searchpos;
int low = 0;
int high = 9;
int linprob;
int binprob;
cout<<"Outputs for the array"<<endl<<endl;
for (k = 0; k <10; k ++)
{
cout<<a[k]<<endl;
}
cout<<""<<endl<<endl;
{
{
cout<<"Please enter an integer: ";
cin>>searchvalue;
cout<<""<<endl<<endl;
// linear search
notfound = true;
k = 0;
while(k < 10 && notfound)
{
if(searchvalue != a[k])
{ k++;
probes ++;
}
else
notfound = false;
}
if( notfound == false )
linprob=probes;
else
linprob=-1;
// sort array a
do
{
swap = false;
for (int count=0; count<(10-1); count++)
{
if (a[count]>a[count+1])
{
temp = a[count];
a[count] = a[count+1];
a[count+1]=temp;
swap=true;
}
}
}while (swap);
// binary search
searchpos = (low + high) / 2;
while((a[searchpos] != searchvalue) && (low <= high))
{
probes2++;
if (a[searchpos] > searchvalue)
{
high = searchpos - 1;
}
else
{
low=searchpos + 1;
}
searchpos = (low + high) / 2;
}
if (low <= high)
binprob=probes2;
else
binprob=-1;
}
cout<<"Value Linear Search "<<
"Binary Search"<<endl;
cout<<searchvalue<<" "<<linprob<<" "<<binprob<<endl;
cout<<endl<<endl;
}
}
void swap2(int &x, int &y)
{
int temp;
temp = x;
x = y;
y = temp;
}
And here we have my code that messes up because i'm trying to use loops and arrays to get 5 values to input and output ...
Code:
// Linear and Bin search
// ERIC LILLY
#include <iostream>
#include <stdlib.h>
#include <cstdlib>
using namespace std;
void swap2(int&m, int&n);
void main()
{
int a [10]={40,60,30,70,90,80,5,25,95,100};
int k,i;
bool notfound;
int searchvalue [5];
int probes=1;
int probes2=1;
int temp;
bool swap;
int searchpos;
int low = 0;
int high = 9;
int linprob [5];
int binprob [5];
cout<<"Outputs for the array"<<endl<<endl;
for (k = 0; k <10; k ++)
{
cout<<a[k]<<endl;
}
cout<<""<<endl<<endl;
{
for(i=1;i<=5;i++)
{
probes = 1;
probes2 = 1;
cout<<"Please enter an integer: ";
cin>>searchvalue [i];
cout<<""<<endl<<endl;
// linear search
notfound = true;
k = 0;
while(k < 10 && notfound)
{
if(searchvalue [i] != a[k])
{ k++;
probes ++;
}
else
notfound = false;
}
if( notfound == false )
linprob [i]=probes;
else
linprob [i]=-1;
// sort array a
do
{
swap = false;
for (int count=0; count<(10-1); count++)
{
if (a[count]>a[count+1])
{
temp = a[count];
a[count] = a[count+1];
a[count+1]=temp;
swap=true;
}
}
}while (swap);
// binary search
searchpos = (low + high) / 2;
while((a[searchpos] != searchvalue [i]) && (low <= high))
{
probes2++;
if (a[searchpos] > searchvalue [i])
{
high = searchpos - 1;
}
else
{
low=searchpos + 1;
}
searchpos = (low + high) / 2;
}
if (low <= high)
binprob [i]=probes2;
else
binprob [i]=-1;
}
cout<<"Value Linear Search Binary Search"<<endl;
for (i=1;i<=5;i++)
{
cout<<searchvalue [i]<<" "<<
linprob [i]<<" "<<binprob [i]<<endl;
}
cout<<endl<<endl;
}
}
void swap2(int &x, int &y)
{
int temp;
temp = x;
x = y;
y = temp;
}