Thread: Sort numbers in order in C programming.

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    1

    Unhappy Sort numbers in order in C programming.

    Okay, so here's my question:

    Write a program that orders three double numbers by increasing value. The program
    should include a function named sort3 that takes three double * arguments (pointer
    to double). The function prototype is
    void sort3(double *x, double *y, double *z);
    The function should reorder the values pointed to by its arguments so that after the call
    sort3(&x, &y, &z);
    the numbers satisfy .
    Your program should input data and print results using the following format:
    Enter three numbers: 4.7 1.4 3.2
    The ordered sequence is: 1.4 3.2 4.7

    And here is my program:
    C code - 32 lines - codepad

    I am getting a lot of errors when I run it through GCC. Can someone help me out here ASAP?
    Oh and I can only use pointers.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Please post your code here, as plain text, properly indented and formatted. Make sure you use code tags: [code]Your code here[/code].

    Also, please post your exact error messages (copy-paste) with line numbers.

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Well, the obvious error is that you're not dereferencing your pointers

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Three numbers is enough to sort the normal way:

    Code:
    int dbl_comp (const void *x, const void *y)
        {
        double item1 = (*(double *) x), item2 = (*(double *) y);
        return item1 < item2 ? -1 : item1 > item2 ? 1 : 0;
        }
    
    
    double foo[] = {x, y, z};
    qsort (foo, 3, sizeof (foo[0]), &dbl_comp);
    Last edited by whiteflags; 02-20-2014 at 03:25 AM.

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by whiteflags View Post
    Three numbers is enough to sort the normal way:

    Code:
    int dbl_comp (const void *x, const void *y)
        {
        double item1 = (*(double *) x), item2 = (*(double *) y);
        return item1 < item2 ? -1 : item1 > item2 ? 1 : 0;
        }
    
    
    double foo[] = {x, y, z};
    qsort (foo, 3, sizeof (foo[0]), &dbl_comp);
    I doubt the exercise would allow the use of qsort()... Otherwise what would be the point?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to sort 3 numbers in descending order
    By Tigertan in forum C Programming
    Replies: 2
    Last Post: 10-31-2012, 07:49 PM
  2. Replies: 9
    Last Post: 04-01-2011, 04:13 PM
  3. bubble sort not sorting numbers in order.
    By rushhour in forum C++ Programming
    Replies: 21
    Last Post: 02-19-2009, 01:40 PM
  4. Sort two vectors in the same order
    By jw232 in forum C++ Programming
    Replies: 2
    Last Post: 03-08-2008, 05:49 PM
  5. Can counting sort sort in descending order?
    By Nutshell in forum C Programming
    Replies: 3
    Last Post: 03-06-2003, 09:59 AM

Tags for this Thread