Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5428
Number of posts: 16949

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Code Break Posted by loki08 on 25 Feb 2008 at 7:51 PM
My code breaks at this point <in red>. Any suggestions?

int compare (const void * a, const void * b)
{
return ( *(float*)a - *(float*)b );
}


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;
}
Report
Re: Code Break Posted by AsmGuru62 on 25 Feb 2008 at 8:50 PM
: My code breaks at this point <in red>. Any suggestions?
:
: int compare (const void * a, const void * b)
: {
: return ( *(float*)a - *(float*)b );
: }

:
: 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;
: }
:

What's "code breaks"?
1. Code does not compile
2. Code does not run
Report
Re: Code Break Posted by loki08 on 26 Feb 2008 at 6:56 AM
: : My code breaks at this point <in red>. Any suggestions?
: :
: : int compare (const void * a, const void * b)
: : {
: : return ( *(float*)a - *(float*)b );
: : }

: :
: : 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;
: : }
: :
:
: What's "code breaks"?
: 1. Code does not compile
: 2. Code does not run
:


Code doesn't run. I put break points and stepped thru. Then the code does do anything when it gets to the return (* (float* ........
Report
Re: Code Break Posted by Lundin on 26 Feb 2008 at 9:03 AM
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)
Report
Re: Code Break Posted by loki08 on 26 Feb 2008 at 11:16 AM
: 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....




Report
Re: Code Break Posted by bluj91 on 28 Feb 2008 at 9:14 PM
: : 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.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.