Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8391085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:10:03+00:00 2026-06-09T19:10:03+00:00

I try to sort an array of double value using qsort, but it doesn’t

  • 0

I try to sort an array of double value using qsort, but it doesn’t seems to work. Wonder what has gone wrong here??

#include <stdio.h>
#include <stdlib.h>
static double compare (const void * a, const void * b)
{
  if (*(double*)a > *(double*)b) return 1;
  else if (*(double*)a < *(double*)b) return -1;
  else return 0;  
}

int main() {

    int idx;
    double* sum_least_square_err;

    sum_least_square_err = (double*) malloc (2500*2500*sizeof(double));

    sum_least_square_err[0] = 0.642;    
    sum_least_square_err[1] = 0.236;
    sum_least_square_err[2] = 0.946;
    idx = 3;

    qsort(sum_least_square_err, idx, sizeof(sum_least_square_err), compare);

    int i;
    for (i=0; i<idx; i++){
       fprintf(stderr,"sum_least_square_err[%d] = %.3f\n", i, sum_least_square_err[i]);            
    }
    fprintf(stderr,"MAEE = %.3f\n", sum_least_square_err[idx/2]);

    free(sum_least_square_err);
}

Result:

sum_least_square_err[0] = 0.642

sum_least_square_err[1] = 0.236

sum_least_square_err[2] = 0.946

MAEE = 0.236

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:10:05+00:00Added an answer on June 9, 2026 at 7:10 pm

    Change:

    static double compare (const void * a, const void * b)
    

    to:

    static int compare (const void * a, const void * b)
    

    and change:

    qsort(sum_least_square_err, idx, sizeof(sum_least_square_err), compare);
    

    to:

    qsort(sum_least_square_err, idx, sizeof(sum_least_square_err[0]), compare);
    

    Note: you should have got an appropriate compiler warning about the first bug – are you compiling with gcc -Wall or equivalent, and if so are you taking notice of compiler warnings ? (If not then please take the hint and let the compiler catch problems such as this for you in future.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to sort an array of structs using selection sort and pointers, but
In a sort of try/catch form I want to execute a bash that doesn't
I try to sort only a selected part of an array. I have try
I'm try to order objects numerically in an array in ascending order, by using
I got trouble when i try to sort array in for loop. I give
I want to sort an array by using uasort() function. I wrote this piece
I know how to sort an array that has two columns: Arrays.sort(myarray, new Comparator<String[]>()
I am trying to build some sort of a javascript antivirus that would try
I need to sort an array containing arrays in ascending numerical order. The data
I'm doing some programming practice right now by trying to sort a 2D array

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.