I am just curious if there is any other way I could write this code, I see 3 if statements in a row and am wondering if there is a more efficient way.
Code:if (number1<0) number1*=-1; if (number2<0) number2*=-1; if (number2>=number1) c=b;
I am just curious if there is any other way I could write this code, I see 3 if statements in a row and am wondering if there is a more efficient way.
Code:if (number1<0) number1*=-1; if (number2<0) number2*=-1; if (number2>=number1) c=b;
That looks fine.
It's clean and readable.
Don't try to use weird obfuscated constructs to try to make it "faster", because, 1) it probably won't be, and 2) it will just make your code unreadable and unmaintainable.
3 in a row is nothing... some programs have dozens.
Unless there's something in common, such as differing values of the same variable. What you got is about as good as it gets.
I should have included this,
I tried changing it up to:Code:s=values[j]+values[l]; number1=r-t; number2=r-s; if (number1<0) number1*=-1; if (number2<0) number2*=-1; if (number2>=number1) c=b;
However it doesnt work, it says: implicit declaration of function 'abs'Code:s=values[j]+values[l]; number1 = abs(r-t); number2 = abs(r-s); if (number2>=number1) c=b;
What is happening?
Last edited by dsured; 02-01-2011 at 10:37 PM. Reason: forgot to include s=values[j]+values[l];
You didn't include the header file that abs() needed. Not sure, but probably that is <math.h> that you need.
Compile and link that, and see how it works then.
I did include math.h in the header.