Program to arrange numbers in ascending order and descending order.

This is a discussion on Program to arrange numbers in ascending order and descending order. within the C Programming forums, part of the General Programming Boards category; Hey guys, the last 5 hours ive been trying to figure out how to write a single program that can ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    7

    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.
    Last edited by jimbo_1; 04-01-2011 at 02:42 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    Quote Originally Posted by quzah View Post
    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. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    Quote Originally Posted by quzah View Post
    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. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jimbo_1 View Post
    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. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    7
    Quote Originally Posted by CommonTater View Post
    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. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jimbo_1 View Post
    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. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.
      //declare your variables here
      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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ascending and descending order
    By ssk in forum C Programming
    Replies: 3
    Last Post: 03-25-2011, 08:03 PM
  2. Sorting in descending order
    By michael- in forum C Programming
    Replies: 3
    Last Post: 12-12-2005, 12:07 PM
  3. Sorting numbers in descending order
    By Yunasnk in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2003, 04:55 PM
  4. Can counting sort sort in descending order?
    By Nutshell in forum C Programming
    Replies: 3
    Last Post: 03-06-2003, 08:59 AM
  5. Replies: 1
    Last Post: 01-06-2002, 05:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21