Bubble sorting and finding a location of a number in array

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2008
Posts: 365
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

Bubble sorting and finding a location of a number in array

 
0
  #1
Oct 11th, 2008
I am trying to do the bubble sort to make my numbers in the array go from smallest to highest. Afterwards, I want to use the SeqOrderedSearch 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:




  
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. void printArray (int numbers[]);
  7. void bubblesort (int numbers[], int length);
  8. int seqOrderedSearch (int numbers[], int length, int searchItem);
  9.  
  10.  
  11. int main ()
  12. {
  13.  
  14. int numbers[] = {22,59,29,11,77,4,9,82,39,81};
  15. int loc;
  16. int searchItem;
  17.  
  18. printArray(numbers);
  19. bubblesort(numbers,10);
  20. loc = seqOrderedSearch(numbers,10,searchItem);
  21.  
  22.  
  23. cout<<"Enter a number to search the array: "<<endl;
  24. cin>>searchItem;
  25.  
  26. cout<<searchItem<<" is found in location #"<<loc<<endl;
  27.  
  28. system ("PAUSE");
  29. return 0;
  30. }
  31.  
  32.  
  33.  
  34. void printArray (int numbers[])
  35. {
  36.  
  37. cout<<"Original array...."<<endl;
  38.  
  39. for (int i = 0; i<10; i++)
  40. {
  41. cout<<numbers[i]<<" ";
  42.  
  43.  
  44.  
  45. }
  46. cout<<endl;
  47. cout<<"Sorting array...."<<endl;
  48.  
  49. }
  50.  
  51.  
  52.  
  53. void bubblesort (int numbers[], int length)
  54. {
  55.  
  56. int temp;
  57. int iteration;
  58.  
  59. for (iteration = 1; iteration < length; iteration++)
  60. {
  61. for (int i = 0; i < length - iteration; i++)
  62. {
  63. if (numbers[i] > numbers[i+1])
  64. {
  65. temp = numbers[i];
  66. numbers[i] = numbers[i+1];
  67. numbers[i + 1] = temp;
  68. }
  69. }
  70. }
  71.  
  72.  
  73. }
  74.  
  75.  
  76.  
  77. int seqOrderedSearch (int numbers[], int length, int searchItem)
  78. {
  79.  
  80. cout<<endl;
  81.  
  82. int loc;
  83. bool found = false;
  84.  
  85.  
  86. for (loc = 0; loc < length; loc++)
  87. {
  88. if (numbers[loc] >= searchItem)
  89. {
  90. found = true;
  91. break;
  92. }
  93.  
  94. if (found)
  95. if (numbers[loc] == searchItem)
  96. return loc;
  97. else
  98. cout<<searchItem<<" was NOT found"<<endl;
  99. }
  100. }
Last edited by NinjaLink; Oct 11th, 2008 at 10:49 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: Bubble sorting and finding a location of a number in array

 
0
  #2
Oct 11th, 2008
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
Last edited by dougy83; Oct 11th, 2008 at 11:12 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Bubble sorting and finding a location of a number in array

 
0
  #3
Oct 11th, 2008
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]
Last edited by Ancient Dragon; Oct 11th, 2008 at 10:57 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 365
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

Re: Bubble sorting and finding a location of a number in array

 
0
  #4
Oct 11th, 2008
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:


  
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. void printArray (int numbers[]);
  7. void bubblesort (int numbers[], int length);
  8. int seqOrderedSearch (int numbers[], int length, int searchItem);
  9.  
  10.  
  11. int main ()
  12. {
  13.  
  14. int numbers[] = {22,59,29,11,77,4,9,82,39,81};
  15. int loc;
  16. int searchItem;
  17.  
  18. printArray(numbers);
  19. bubblesort(numbers,10);
  20.  
  21.  
  22. cout<<endl;
  23. cout<<"Enter a number to search the array: "<<endl;
  24. cin>>searchItem;
  25.  
  26. loc = seqOrderedSearch(numbers,10,searchItem);
  27.  
  28. cout<<searchItem<<" is found in location #"<<loc<<endl;
  29.  
  30.  
  31. system ("PAUSE");
  32. return 0;
  33. }
  34.  
  35.  
  36. void printArray (int numbers[])
  37. {
  38.  
  39. cout<<"Original array....";
  40.  
  41. for (int i = 0; i<10; i++)
  42. {
  43. cout<<numbers[i]<<" ";
  44.  
  45.  
  46.  
  47. }
  48. cout<<endl;
  49. cout<<"Sorting array...."<<endl;
  50. cout<<"Sorted array...";
  51.  
  52. }
  53. void bubblesort (int numbers[], int length)
  54. {
  55.  
  56. int temp;
  57.  
  58. for (int i = 0; i < length; i++)
  59. {
  60. for (int j = 0; j < length - i; j++)
  61. {
  62. if (numbers[i] < numbers[i+1])
  63. {
  64. temp = numbers[i];
  65. numbers[i] = numbers[i+1];
  66. numbers[i + 1] = temp;
  67. }
  68. }
  69. }
  70.  
  71.  
  72. }
  73.  
  74.  
  75. int seqOrderedSearch (int numbers[], int length, int searchItem)
  76. {
  77.  
  78. cout<<endl;
  79.  
  80. int loc;
  81. bool found = false;
  82. int NOT;
  83.  
  84.  
  85. for (loc = 0; loc < length; loc++)
  86. {
  87. if (numbers[loc] >= searchItem)
  88. {
  89. found = true;
  90. }
  91.  
  92. if (found)
  93. if (numbers[loc] == searchItem)
  94. return loc;
  95. else
  96. return -1;
  97. }
  98. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Bubble sorting and finding a location of a number in array

 
0
  #5
Oct 11th, 2008
1) after line 19

2) Change line 28 to test for return value of line 26
Last edited by Ancient Dragon; Oct 11th, 2008 at 11:50 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 365
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

Re: Bubble sorting and finding a location of a number in array

 
0
  #6
Oct 12th, 2008
Originally Posted by Ancient Dragon View Post
1) after line 19

2) Change line 28 to test for return value of line 26

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:



  
  1. int main ()
  2. {
  3.  
  4. int numbers[] = {22,59,29,11,77,4,9,82,39,81};
  5. int loc;
  6. int searchItem;
  7. int length = 10;
  8.  
  9. printArray(numbers);
  10. bubblesort(numbers,10);
  11. for (int i = 0; i < length; i++)
  12. {
  13. cout<<numbers[i]<<" ";
  14. }
  15.  
  16.  
  17.  
  18. cout<<endl;
  19. cout<<"Enter a number to search the array: "<<endl;
  20. cin>>searchItem;
  21.  
  22. loc = seqOrderedSearch(numbers,10,searchItem);
  23.  
  24. for (int i = 0; i < length; i++)
  25. {
  26. if (numbers[i] == searchItem)
  27. {
  28. cout<<searchItem<<" is found in location #"<<loc<<endl;
  29. }
  30. if (numbers[i] != searchItem)
  31. {
  32. cout<<searchItem<<" was NOT found"<<endl;
  33. }
  34. }
  35.  
  36. system ("PAUSE");
  37. return 0;
  38. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 365
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

Re: Bubble sorting and finding a location of a number in array

 
0
  #7
Oct 12th, 2008
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!


  1. int main ()
  2. {
  3.  
  4. int numbers[] = {22,59,29,11,77,4,9,82,39,81};
  5. int loc;
  6. int searchItem;
  7. int length = 10;
  8. bool found = false;
  9.  
  10. printArray(numbers);
  11. bubblesort(numbers,10);
  12. for (int i = 0; i < length; i++)
  13. {
  14. cout<<numbers[i]<<" ";
  15. }
  16.  
  17.  
  18.  
  19. cout<<endl;
  20. cout<<"Enter a number to search the array: "<<endl;
  21. cin>>searchItem;
  22.  
  23. loc = seqOrderedSearch(numbers,10,searchItem);
  24.  
  25. if (numbers[loc] == searchItem)
  26. cout<<searchItem<<" is found in location #"<<loc<<endl;
  27. else
  28. cout<<searchItem<<" was NOT found"<<endl;
  29.  
  30.  
  31.  
  32.  
  33. system ("PAUSE");
  34. return 0;
  35. }
Last edited by Ancient Dragon; Oct 12th, 2008 at 8:20 am. Reason: removed icode tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: Bubble sorting and finding a location of a number in array

 
0
  #8
Oct 12th, 2008
It shouldn't happen, but this code isn't giving anything
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Bubble sorting and finding a location of a number in array

 
0
  #9
Oct 12th, 2008
Your program works fine for me.

Sci: His last post was only main() -- the rest is in the origianl post.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC