-1

I'm writing a program which prompts the user to say how many elements an array should have, then fill that array with numbers:

    int n;
    int i;
    float numbers[100];

    printf("Enter number of elements:");
    scanf("%d", &n);

    for(i = 0; i < n - 1; i++)
        scanf("%f\n", &numbers[i]);

I'm trying to write a function which takes that array of numbers, and sorts it in ascending order. I read somewhere online that there's actually a built in function in C that does this - qsort(). Here's my code:

void sort(int n, float array[])
{
    int i;
    int cmpfunc (const void * a, const void * b)
    {
        return(*(int*)a - *(int*)b );
    }

    qsort(array, n, sizeof(float), cmpfunc);

    for(i = 1; i <= n; i++)
        printf("%.2f\n", array[i]);
}

Basically the problem is I don't think I'm implementing the function correctly. For example, I'll run the program, set the array size to 5, and input the following numbers:

1,9,2,8,5

The output I get is:

1,2,8,9,0

There is a similar pattern in all my outputs.

It sorts it somewhat decently, except it always ends with a 0, and for some unknown reason, randomly skips one number of the array and doesn't sort it (in the example above 5 is skipped). In the for loop, I had i set to 0, but then my output would also skip a number when sorting, and it would still have a 0, except this time it would be at the beginning of the output.

Finally, it should be noted that I have absolutely no idea how the qsort() function works, or why it needs that other compare function to work, I just sort of copy pasted it in there.

If anyone knows the answer to my problem please help me out I'd really appreciate it, thank you.

8
  • 2
    Functions cannot be nested in C. And for(i = 1; i <= n; i++) looks like an off-by-one. Commented Dec 29, 2020 at 0:07
  • 1
    You are sorting an array of float, so the pointers passed to the comparison function point to float values, not int values. Commented Dec 29, 2020 at 0:13
  • 2
    Casting pointers pointing at float to int* doesn't look like a good idea. Commented Dec 29, 2020 at 0:13
  • 1
    @wildplasser Functions can be nested in GCC extension. Commented Dec 29, 2020 at 0:13
  • 1
    for(i = 0; i < n - 1; i++) should be for(i = 0; i < n ; i++), and for(i = 1; i <= n; i++) should be for(i = 0; i < n; i++). Commented Dec 29, 2020 at 0:16

2 Answers 2

2

You are not filling the last element of your array here

for(i = 0; i < n - 1; i++)
    scanf("%f\n", &numbers[i]);

And in the sorting function you are: not printing the first element; printing the last element, which can be anything; printing an element out of the array's bounds

for(i = 1; i <= n; i++)
    printf("%.2f\n", array[i]);

Both the loops should be

for(i = 0; i < n; i++)

And it seems that using qsort with a float array isn't straightforward, you should adjust its comparison function as said here: Problem trying to use the C qsort function

Sign up to request clarification or add additional context in comments.

7 Comments

This answer misses the fact that float* values are cast as int* and dereferenced inside the cmpfunc, which invokes undefined behavior
If I switch the for loop used for filling the array, I end up getting prompted one additional time for an extra element. (i.e. if I specify I want 5 elements, I get prompted for 6 elements). Any idea what this is?
@Wacy if you enter 5 the first time, then you are asked to enter 5 elements
Using qsort with a float array should be OK. The comparision function is wrong. The linked question is also saying so.
Yes I updated the answer to reflect the content of the link
|
2

OP's compare fails when

a, b not within about rage of int.

a, b unequal, but (int) a == (int) b

The subtraction overflows.


A better compare

int cmpfunc (const void * a, const void * b) {
  float fa = *(const float *)a;
  float fb = *(const float *)b;
  return (fa > fb) - (fa < fb);
}

Use for(i = 0; i < n; i++) as well answered by @Davide.


Fails then n > 100.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.