# Thread: Program to arrange numbers in ascending order and descending order.

1. ## Program to arrange numbers in ascending order and descending order.

Hey guys,
the last 5 hours ive been trying to figure out how to write a single program that can arrange 4 numbers ,that the user will give,in descending,ascending and the order-that-were-given order.

So far i have written 3 different programms for each occasion using the insertion sort algorithm.

However i noticed that im asked to write this program using !only! functions.
I have done a lot of research and i find out nothing except for bubble sort and if-else statements which are very unpractical.

Are there any functions that i can use to write my program,that im not aware of?
I gave up searching,my eyes hurt.

2. What do you mean by equations? Do they just want you to compare one item to another and swap them, or are you expecting somehow to swap them with some fancy math?

Quzah.

3. Originally Posted by quzah
What do you mean by equations? Do they just want you to compare one item to another and swap them, or are you expecting somehow to swap them with some fancy math?

Quzah.
They want us to write a single program that can do all three using only functions,that's all they say!!Im guessing fancy math,but how?
Mind you,that i have only been taught about if-else statements and some basic equations and nothing more.
How hard can it be?

For example lets say that the user has given the numbers 3 4 1 2.
The result in the screen should be the following:
INPUT NUMBER: 3 4 1 2
DESCENDING : 1 2 3 4
ASCENDING: ......

(i edited my post,i meant functions not equations oh well)

4. That's still pretty vague. By just using "functions" you could just call the standard qsort function. I am assuming that's not what they expect you to do.

Quzah.

5. Originally Posted by quzah
That's still pretty vague. By just using "functions" you could just call the standard qsort function. I am assuming that's not what they expect you to do.

Quzah.
And by basic i mean how to calculate absolute value of a number and square root.
Is the qsort easier than the insertion sort?
Can i combine all three into 1 by using the qsort?

6. There is a function in C called qsort that will sort for you. Grab your C book, find the index, and look up qsort. Or just read the man page.

Quzah.

7. Originally Posted by jimbo_1
Are there any functions that i can use to write my program,that im not aware of?
I gave up searching,my eyes hurt.
So, are you saying you don't know how to write a function?

8. Originally Posted by CommonTater
So, are you saying you don't know how to write a function?
im not asking you to give me the right code or do my homework as you initially wrote.
just for some help.
i didnt know if i must use equations like qsort or bubble sort or just make mine.im pretty new to this thing you know and it's just an optional class.

9. Originally Posted by jimbo_1
im not asking you to give me the right code or do my homework as you initially wrote.
just for some help.
i didnt know if i must use equations like qsort or bubble sort or just make mine.im pretty new to this thing you know and it's just an optional class.
Yeah that's why I re-edited as an afterthought... I do that sometimes.

I'd say the assignment wants you to write your own functions to sort the lists.
It's a pretty routine part of any C language programming task.

10. After the include statements, you might have a #define SIZE 4, and next would be your prototype for the sort() function.

I'd be quite tempted to make one sort function, and send that function a parameter value to tell it what kind of a sort to do: ascending might be 0, descending order might be any other value.
Code:
```void sort(int numbers[SIZE], int typeOfSort) { //might be the first line of your sort function.
if(typeOfSort == 0) {
//put an ascending sort in here
}
else {
//put a descending sort in here
}
}```
If you have a working Insertion sort, then I'd use it. Not only is it a quick little sorter on small and medium amounts of data, but it also works very fast on data that is nearly or already sorted - which oddly enough, is the most common sorting situation.

Some people prefer the bubble sort, which is fast only on the very smallest quantities. I ran across a simple substitution sort years ago, and found it suits me fine for smaller quantities.

It's important to get comfortable with a basic sorter, at least. That's why I don't like C's qsort - it has tricky little casts that you need to work with, (which vary with the type of data you're trying to sort), and that can be quite frustrating. For that reason, I'd avoid qsort until you're more proficient with C.