Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Code Break

loki08loki08 Posts: 5Member
My code breaks at this point . Any suggestions?

int compare (const void * a, const void * b)
{
[color=Red]return ( *(float*)a - *(float*)b );
}[/color]

float Calc_Median(int intArraySize)
{
int n;
qsort (CalcTimeArray, intArraySize, sizeof(float), compare);
for (n=0; n<intArraySize; n++)
printf ("%d ",CalcTimeArray[n]);
return 0;
}

Comments

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    : My code breaks at this point . Any suggestions?
    :
    : int compare (const void * a, const void * b)
    : {
    : [color=Red]return ( *(float*)a - *(float*)b );
    : }[/color]
    :
    : float Calc_Median(int intArraySize)
    : {
    : int n;
    : qsort (CalcTimeArray, intArraySize, sizeof(float), compare);
    : for (n=0; n<intArraySize; n++)
    : printf ("%d ",CalcTimeArray[n]);
    : return 0;
    : }
    :
    [color=Blue]
    What's "code breaks"?
    1. Code does not compile
    2. Code does not run
    [/color]
  • loki08loki08 Posts: 5Member
    : : My code breaks at this point . Any suggestions?
    : :
    : : int compare (const void * a, const void * b)
    : : {
    : : [color=Red]return ( *(float*)a - *(float*)b );
    : : }[/color]
    : :
    : : float Calc_Median(int intArraySize)
    : : {
    : : int n;
    : : qsort (CalcTimeArray, intArraySize, sizeof(float), compare);
    : : for (n=0; n<intArraySize; n++)
    : : printf ("%d ",CalcTimeArray[n]);
    : : return 0;
    : : }
    : :
    : [color=Blue]
    : What's "code breaks"?
    : 1. Code does not compile
    : 2. Code does not run
    : [/color]

    Code doesn't run. I put break points and stepped thru. Then the code does do anything when it gets to the return (* (float* ........
  • LundinLundin Posts: 3,711Member
    Since you didn't post what type of variables you put into qsort, the question is impossible to answer. There is no error in that code. (Except it is non-portable, but that might not be an issue here)
  • loki08loki08 Posts: 5Member
    : Since you didn't post what type of variables you put into qsort, the
    : question is impossible to answer. There is no error in that code.
    : (Except it is non-portable, but that might not be an issue here)

    Figured out that it didn't like using "float" but did fine with the "int". I went to using a vector class. Works the way I want it to.

    Thanks....




  • bluj91bluj91 Posts: 133Member
    : : Since you didn't post what type of variables you put into qsort, the
    : : question is impossible to answer. There is no error in that code.
    : : (Except it is non-portable, but that might not be an issue here)
    :
    : Figured out that it didn't like using "float" but did fine with the
    : "int". I went to using a vector class. Works the way I want it to.
    :
    : Thanks....
    :
    :
    :
    :
    :

    Hmm, on that note I thought I would contribute a reason. Type casting it to float means that it would use floating point arithmatic. Which simply doesn't make sense to use in that context. For example thinking of floating point arithmatic in terms of scientific notation, they are very similar:

    if you go

    1.45E-1 + 4.00E-1 you get 5.45E-1 but if you add 1.34E0 and 5.00E-1 you get 1.84E0 which is represented you completely different in terms of binary than the sum of each direct number.

    Aye aye aye.
Sign In or Register to comment.