Not Yet Answered # how will i arrange 5 numbers in ascending order?

Bob 15 np complete 8 iamthwee 1,547 MandrewP 60 WaltP 2,905 Bob 15 pyTony 888 WaltP 2,905 pyTony 888 np complete 8 Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...

0

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.

1

C++ supporting sorting and searching techniques can be found in <algorithm> header file. Dev C++ provides algorithm header.

Just include it : `# include < algorithm >`

0

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.

0

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.

0

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?

0

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.

0

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.

*Edited 4 Years Ago by pyTony*

0

5 values. The worst sort in the world is more than adequate for 5 values.

0

http://en.m.wikipedia.org/wiki/Bogosort

maybe nearer to worst sorting algorithm still working.

1

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.

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

Hi. so this is actually a continuation from another question of mineHere but i was advised to start a new thread as the original question was already answered.

This is the result of previous question answered :

code for the listbox - datagridview interaction

At the top of the code ...

the function that I created to find the ...