I am trying to do the [B]bubble sort[/B] to make my numbers in the array go from smallest to highest. Afterwards, I want to use the [B]SeqOrderedSearch[/B] to find the location of the number in the array. If the number is not found, i will have to state it is not found.
The program runs fine. I'm just not getting the numbers I want.
Sample:
Original Array: 22, 59, 29, 11, 77, 4, 9, 82, 39, 81
Sorting array...
Sorted Array: 4, 9, 11, 22, 29, 39, 59, 77, 81, 82
Enter a number to search the array: 77
77 was found in location #8
Note: if the user selects 42 (42 is not in the array), it will then be "42 was NOT found"
Here is my code:
#include <iostream>
#include <iomanip>
using namespace std;
void printArray (int numbers[]);
void bubblesort (int numbers[], int length);
int seqOrderedSearch (int numbers[], int length, int searchItem);
int main ()
{
int numbers[] = {22,59,29,11,77,4,9,82,39,81};
int loc;
int searchItem;
printArray(numbers);
bubblesort(numbers,10);
loc = seqOrderedSearch(numbers,10,searchItem);
cout<<"Enter a number to search the array: "<<endl;
cin>>searchItem;
cout<<searchItem<<" is found in location #"<<loc<<endl;
system ("PAUSE");
return 0;
}
void printArray (int numbers[])
{
cout<<"Original array...."<<endl;
for (int i = 0; i<10; i++)
{
cout<<numbers[i]<<" ";
}
cout<<endl;
cout<<"Sorting array...."<<endl;
}
void bubblesort (int numbers[], int length)
{
int temp;
int iteration;
for (iteration = 1; iteration < length; iteration++)
{
for (int i = 0; i < length - iteration; i++)
{
if (numbers[i] > numbers[i+1])
{
temp = numbers[i];
numbers[i] = numbers[i+1];
numbers[i + 1] = temp;
}
}
}
}
int seqOrderedSearch (int numbers[], int length, int searchItem)
{
cout<<endl;
int loc;
bool found = false;
for (loc = 0; loc < length; loc++)
{
if (numbers[loc] >= searchItem)
{
found = true;
break;
}
if (found)
if (numbers[loc] == searchItem)
return loc;
else
cout<<searchItem<<" was NOT found"<<endl;
}
}