| | |
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:
Solved Threads: 0
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:
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:
C++ Syntax (Toggle Plain Text)
#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; } }
Last edited by NinjaLink; Oct 11th, 2008 at 10:49 pm.
•
•
Join Date: Jun 2007
Posts: 275
Reputation:
Solved Threads: 45
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 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.
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]
[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.
•
•
Join Date: Mar 2008
Posts: 365
Reputation:
Solved Threads: 0
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:
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:
C++ Syntax (Toggle Plain Text)
#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; } }
•
•
Join Date: Mar 2008
Posts: 365
Reputation:
Solved Threads: 0
•
•
•
•
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:
C++ Syntax (Toggle Plain Text)
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; }
•
•
Join Date: Mar 2008
Posts: 365
Reputation:
Solved Threads: 0
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!
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!
C++ Syntax (Toggle Plain Text)
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; }
Last edited by Ancient Dragon; Oct 12th, 2008 at 8:20 am. Reason: removed icode tags
![]() |
Other Threads in the C++ Forum
- Previous Thread: Binary to Decimal HELP :(
- Next Thread: MPH to Minutes and Seconds to run a mile
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






