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 6937065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:18:58+00:00 2026-05-27T12:18:58+00:00

if I have an array double i[5] = {1.023, 1.22, 1.56, 2, 5, 3.331};

  • 0

if I have an array

double i[5] = {1.023, 1.22, 1.56, 2, 5, 3.331};

how do i sort the values so that they look like this:

double i[5] = {1.023, 1.22, 1.56, 2, 3.331, 5};

i’ve tried qsort() with no luck, after trying some examples, i came up with:

qsort(i, 5, sizeof(double), sort);

int sort(const void *x, const void *y)
{
return (*(double*)x - *(double*)y);
}

with => error: incompatible type for argument 1
not sorting the array…..

  • 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-05-27T12:18:59+00:00Added an answer on May 27, 2026 at 12:18 pm

    The first argument to qsort is the pointer to the start of the array to be sorted. Instead of

    qsort(i[5], 5, sizeof(double), sort);
    

    it should read

    qsort(i, 5, sizeof(double), sort);
    

    Some further observations:

    1. The length of i‘s initializer is incorrect (i has five elements, yet the initializer has six).
    2. Hard-coding the 5 into the qsort call is asking for trouble later on.
    3. The name “i” is most commonly used for loop counters and the like.
    4. Calling the comparison function sort is confusing.
    5. Your comparison function is wrong. Consider how it would compare the numbers 1.1 and 1.2. Also think about what would happen if the difference between the two values doesn’t fit in an int.

    I would rewrite your entire example like so:

    double arr[] = {1.023, 1.22, 1.56, 2, 5, 3.331};
    
    int cmp(const void *x, const void *y)
    {
      double xx = *(double*)x, yy = *(double*)y;
      if (xx < yy) return -1;
      if (xx > yy) return  1;
      return 0;
    }
    
    int main() {
      qsort(arr, sizeof(arr)/sizeof(arr[0]), sizeof(arr[0]), cmp);
    }
    

    Note that the above comparison function still doesn’t correctly handle NaNs; I leave it as an exercise for the reader to fix that.

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

Sidebar

Related Questions

In my program I have one array with 25 double values 0.04 When I
Let's say I have an array of lots of values (C++ syntax, sorry): vector<double>
i have an array, a double array like... Double my_array = new Double[] {6272640.0,
I have an array List<double[]> values = new ArrayList<double[]>(); values.add(new double[] { 12.3, 12.5,
I have a sorted array of double values in C++. Is there an STL
I have problem in getting wrong values in array of double. Instead of getting
I have an array of values (between -1.0 and 1.0) that represent intensity (Black
I have an array of double values vals, I need to randomly index into
I have a double[] array holding many numbers. I have an algorithm that selects
I have a 1 x N double array and I would like to merge

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.