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;
  }
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Line 20 should be moved to after line 24: you want to let the user enter the search value before you call the search function.

line 86: remove this line - the break statement would quit the loop; before you get to report the location

the seqOrderedSearch function should return a value if the item is not found, e.g. -1; generally you would check the return value to decide whether you would display "item x found at loc" or "item x not found", and remove the cout statement from the seqOrderedSearch function

line 20 is in the wrong place -- move it down to line 25, when the variable search_item is known.\

[edit]^^^ he beat me to it :) [/edit]

Thanks for the replys guys.

I have a couple more questions..

1) Where do I put my cout statement to display my sorted array in the "bubblesort" function? I tried putting it in several places, but it didn't work.

2) How and where do I put a cout statement saying "<number> is not found" when the user insert a number that is not in the array?

Here is my updated 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);


 cout<<endl;
 cout<<"Enter a number to search the array: "<<endl;
 cin>>searchItem;

loc = seqOrderedSearch(numbers,10,searchItem);

cout<<searchItem<<" is found in location #"<<loc<<endl;


system ("PAUSE");
return 0;
}


void printArray (int numbers[])
{

     cout<<"Original array....";

for (int i = 0; i<10; i++)
{
    cout<<numbers[i]<<" ";



}
    cout<<endl;
    cout<<"Sorting array...."<<endl;
    cout<<"Sorted array...";

}
void bubblesort (int numbers[], int length)
{

 int temp;

 for (int i = 0; i < length; i++)
 {
     for (int j = 0; j < length - i; j++)
     {
         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;
  int NOT;


  for (loc = 0; loc < length; loc++)
  {
      if (numbers[loc] >= searchItem)
      {
        found = true;
      }

      if (found)
      if (numbers[loc] == searchItem)
         return loc;
      else
       return -1;
  }
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

[QUOTE=Ancient Dragon;710574]1) after line 19

2) Change line 28 to test for return value of line 26[/QUOTE]

Thanks.

The only problem is #2 though.

I am trying an "if" statement in main so I correct the problem....The problem is that i can't figure out how to write the "cout" statements outside of the loop, so the cout statement can only once. Please help!

Here is the code in main:

int main ()
{

    int numbers[] = {22,59,29,11,77,4,9,82,39,81};
    int loc;
    int searchItem;
    int length = 10;

printArray(numbers);
bubblesort(numbers,10);
for (int i = 0; i < length; i++)
 {
 cout<<numbers[i]<<" ";
 }



 cout<<endl;
 cout<<"Enter a number to search the array: "<<endl;
 cin>>searchItem;

loc = seqOrderedSearch(numbers,10,searchItem);

for (int i = 0; i < length; i++)
{
    if (numbers[i] == searchItem)
    {
 cout<<searchItem<<" is found in location #"<<loc<<endl;
    }
    if (numbers[i] != searchItem)
    {
  cout<<searchItem<<" was NOT found"<<endl;
    }
}

system ("PAUSE");
return 0;
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

I have an update.

I got the "if" statement working for the cout statements on whether the number is found or not. The question I now have is.......

when I enter a number above "82" which is the largest number in the array, it gives me an error and the program shuts down...All numbers that I enter 82 and under shows that it is either found or not. Is that suppose to happen? If so, I am done with this program!

int main ()
{
    
    int numbers[] = {22,59,29,11,77,4,9,82,39,81};
    int loc;
    int searchItem;
    int length = 10;
    bool found = false;
    
printArray(numbers);
bubblesort(numbers,10);
for (int i = 0; i < length; i++)
 {
 cout<<numbers[i]<<" ";
 }



 cout<<endl;
 cout<<"Enter a number to search the array: "<<endl;
 cin>>searchItem;
 
loc = seqOrderedSearch(numbers,10,searchItem);

if (numbers[loc] == searchItem)
cout<<searchItem<<" is found in location #"<<loc<<endl;
else
cout<<searchItem<<" was NOT found"<<endl;



    
system ("PAUSE");
return 0;
}
This question has already been answered. Start a new discussion instead.