How will I arrange 5 numbers in ascending order and determine the highest? what codes will i use or how will i do it? (using dev c++)
How will I arrange 5 numbers in ascending order and determine the highest? what codes will i use or how will i do it? (using dev c++)
Hi Tim,
The C++ language provides support for sorting and for selecting the maximum value, which makes this really easy. If Dev C++ doesn't come with that support, or if this is homework and you're expected to roll your own, you'll need to sort the values yourself and find the largest.
A simple (though relatively inefficient) sorting routine is the bubblesort, so if you search for examples, that would get you started. If you have trouble getting your code to work you can post it here and ask for specific help, but you'll need to have a go at it yourself first.
Good luck.
C++ supporting sorting and searching techniques can be found in <algorithm> header file. Dev C++ provides algorithm header.
Just include it : # include < algorithm >
You will need a loop (while loop, for loop, possibly a recursive function but that's just being clever), an array (not necessary) but the more numbers you need to sort the more an array makes sense.
Then you will need to use the less than or greater than operator to compare the values as to go through those numbers and sort them.
As Bob said, bubblesort is your next best step for the newbie.
Yes, I would do a bubblesort. If you have five numbers, 84692, you would basically sort them like this:
compare the 8 and 4, if 8 is greater then swap them - 48692
compare the 8 and 6, if 8 is greater then swap them - 46892
compare the 8 and 9, if 8 is greater then swap them - 46892
compare the 9 and 2, if 9 is greater then swap them - 46829
Now repeat that whole process again:
compare the 4 and 6, if 4 is greater then swap them - 46829
compare the 6 and 8, if 6 is greater then swap them - 46829
compare the 8 and 2, if 8 is greater then swap them - 46289
compare the 8 and 9, if 8 is greater then swap them - 46289
Now repeat that whole process again:
compare the 4 and 6, if 4 is greater then swap them - 46829
etc..
You just keep repeating that process until all elements are in numerical order. So you can see that you are going to need two loops - an inner loop and an outer loop. Notice how the 2 gradually moves from the left to the right. That is why this is called a bubblesort, the numbers "bubble" up (or along) until they are all in order.
This is the basic bubblesort algorithm, but it can be made more efficient. I'll leave you to "sort" out the details.
how will i compare it? what code or function , command will i use?
Bob: you'll need to sort the values yourself and find the largest.
Bob: A simple (though relatively inefficient) sorting routine is the bubblesort, so if you search for examples, that would get you started.
iamthwee: You will need a loop
iamthwee: Then you will need to use the less than or greater than operator to compare the values
MandrewP: Yes, I would do a bubblesort.
Seems to me you've already been given the information you need. Or do you want us to do the research and write it for you?
how will i compare it? what code or function , command will i use?
Tim, if you google on "C++ bubblesort" you'll find plenty of examples that discuss how the bubblesort works and provide code to do the sorting, which will include comparison of values as part of the sorting algorithm.
Once your array is in ascending order, the highest value will be at the end of the array.
The fastest way for you to achieve your goal is to google it and have a go yourself. You can always post your code here if you have problems getting it to work correctly.
Good luck with your coding.
Bubble sort is really bad performer and implementing insertion or selection sort instead is not any harder. Best is of course to use the sort from algorithm instead of reinventing the wheel.
Bubble sort is really bad performer and implementing insertion or selection sort instead is not any harder. Best is of course to use the sort from algorithm instead of reinventing the wheel.
5 values. The worst sort in the world is more than adequate for 5 values.
For values like 5, any sorting algo will work fine I suppose. As for OP, if he wants to learn how sorting is done, then he should start with basics like selection sort, bubble sort then go for much better algorithm like quick sort.
Here is another website dedicated to sorting algorithm.
You may also want to see this.