Thread: dynamic allocation and qsort

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    3

    dynamic allocation and qsort

    Hello
    I don't know why this code doesn't work. I tried everything. Can you help me, please? *puppy face*

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define COMPARE(a, b) (((a) > (b)) - ((a) < (b)))
    
    
    int compare (const void *a, const void *b)
    {
      float fa = *(const float*) a;
      float fb = *(const float*) b;
      return (fa > fb) - (fa < fb);
    }
    
    
    int main()
    {
            float *v, price;
            int i=0, n=32;
            v=malloc(n*sizeof(float));
            while(scanf("%f",&price)!='*')
            {
                if(++i==n)
                {
                    n+=32;
                    v=realloc(v,n*sizeof(float));
                }
                v[i]=price;
            }
            n=i;
            qsort(v, 10, sizeof(float), compare);
            for(i=0;i<n;i++)
                printf("%f",v[i]);
            free(v);
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1. Look up what scanf actually returns.
    2. Pass n rather than 10 to qsort.
    3. Add some newlines in your printfs.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    3
    1. I changed * in EOF
    2. okay
    3. okay

    i did it but it still do nothing

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Quote Originally Posted by neko369 View Post
    1. I changed * in EOF
    2. okay
    3. okay

    i did it but it still do nothing
    Please repost your code so we an see the actual changes.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    3
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define COMPARE(a, b) (((a) > (b)) - ((a) < (b)))
    
    
    int compare (const void *a, const void *b)
    {
      float fa = *(const float*) a;
      float fb = *(const float*) b;
      return (fa > fb) - (fa < fb);
    }
    
    
    int main()
    {
            float *v, price;
            int i=0, n=32;
            v=malloc(n*sizeof(float));
            while(scanf("%f",&price)!=EOF)
            {
                if(++i==n)
                {
                    n+=32;
                    v=realloc(v,n*sizeof(float));
                }
                v[i]=price;
            }
            n=i;
            qsort(v, n, sizeof(float), compare);
            for(i=0;i<n;i++)
                printf("%f\n",v[i]);
            free(v);
        return 0;
    }

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    You're not storing the price where it should be stored (because you're incrementing 'i' before using it as the index for the array)

  7. #7
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    scanf() should be tested against 0, not EOF.

    Don't increment i until AFTER:
    Code:
    v[i]=price;
    i++;
    In addition, please tell the user how to use the program, and how to end the input. Keyboard input or redirection from a file?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic allocation from 1 instead of zero
    By cfdprogrammer in forum C Programming
    Replies: 27
    Last Post: 04-28-2009, 08:21 AM
  2. Dynamic mem allocation, how to get it right?
    By Subsonics in forum C Programming
    Replies: 31
    Last Post: 02-08-2009, 01:41 PM
  3. Dynamic allocation
    By SARAHCPS in forum C Programming
    Replies: 5
    Last Post: 11-13-2005, 02:24 PM
  4. Qsort and Dynamic Array
    By Thantos in forum C Programming
    Replies: 5
    Last Post: 12-12-2003, 06:36 PM
  5. Dynamic Allocation
    By Darkwraith in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 07-09-2003, 10:00 PM

Tags for this Thread