Thread: Sorting Problems

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    208

    Sorting Problems

    Hey I need a function that sorts and array of floats. Everyone I have tried to write of find has and incomatible types error I am not asking anyone to write it for me but I need a step in the rite direction on how to implement this into my program thanks
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  2. #2
    hint
    Guest
    Bubble Sort:
    Using a loop, compare the first and second elements, and if they're not in the right order, swap them. Repeat this with the second and third elements, and so on, for the rest of the elements((elements - 1) times).
    Using another loop, outside this one, repeat the above elements times.

  3. #3
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    How are you comparing floats in your if statements?
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    How about qsort?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int fcmp(const void* a, const void *b)
    {
        const float *x = a;
        const float *y = b;
        if(*x > *y)
        {
            return 1;
        }
        if(*x < *y)
        {
            return -1;
        }
        return 0;
    }
    
    void show(const float *value, size_t size)
    {
        while(size--)
        {
            printf("%14.6f\n", *value++);
        }
    }
    
    int main(void)
    {
        float array[] = { 1.23F, 0.0F, -15.6F, 23.45, -1.0F, 1.23456F, 5.16F };
        puts("BEFORE");
        show(array,sizeof(array)/sizeof(*array));
        qsort(array,sizeof(array)/sizeof(*array),sizeof(*array),fcmp);
        puts("AFTER");
        show(array,sizeof(array)/sizeof(*array));
        return 0;
    }
    
    /* my output
    BEFORE
          1.230000
          0.000000
        -15.600000
         23.450001
         -1.000000
          1.234560
          5.160000
    AFTER
        -15.600000
         -1.000000
          0.000000
          1.230000
          1.234560
          5.160000
         23.450001
    */

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    ... and of course, don't forget to read this FAQ entry...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    208
    It might help if I post the errors I keep getting when trying to compile.

    invalid types `float **[float]' for array subscript
    thats always the error within my sorting function.
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  7. #7
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    It's even more useful if you post the code block that's giving you problems. But I think it might be caused by indexing an array using a float.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>But I think it might be caused by indexing an array using a float.
    Yes, you can only use an integral type for array subscripting.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    208

    well I started my own

    from scratch so yea and I realized I didn't know how to do it so heres what I have so far I know it is wrong ccause it gives faulty output but anyhoo here it is can anyone help me make it work it doesn't have to be the best just sort the freeking array

    Code:
    void sort(float u[],int n)
    {for(int l=0;l<=n;l++) 
     {if (u[l]>u[l-1])
       {u[l]=u[l-1];
        u[l-1]=u[l];
       } 
     }
    }
    thanx
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You need to use a temporary variable to do a swap.

    Code:
        u[l]=u[l-1];
        u[l-1]=u[l];
    Is the same as
    A = B;
    B = A;
    Can you see the problem?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User
    Join Date
    May 2002
    Posts
    208

    ok

    I am getting closer. lol
    heres what I got now.

    Code:
    void sort(float u[],int n)
    { float temp;
    
    for(int l=0;l<=n;l++) 
     {if (u[l]>u[l-1])
       {temp=u[l];
        u[l]=u[l-1];
        u[l-1]=temp;
       } 
     }
    }
    it seems to sort some well it moves them around a bit anways.
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    To do the sort you'll need a nested loop, traversing the array once won't do the job.

    Also, in your code, when you go through the loop the first time, l is set to 0, so you're actually doing this:
    >if (u[0]>u[-1])
    ... which is illegal as you're going outside of the array bounds.


    Search the board for some sort code, or, if you want, lookup qsort().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting problems.
    By Sedvan in forum C++ Programming
    Replies: 2
    Last Post: 02-14-2008, 01:13 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  5. Sorting structures
    By RedRum in forum C++ Programming
    Replies: 2
    Last Post: 05-23-2002, 12:19 PM