What happens is I receive negative values, so I wrote quick abs function but it must be bunk:
Code: Select all
int abs(int num)
{
if(num<0) {
int sqrd=num*num;
int prod=num/sqrd;
return prod;
} else {
return num;
}
}
Code: Select all
int abs(int num)
{
if(num<0) {
int sqrd=num*num;
int prod=num/sqrd;
return prod;
} else {
return num;
}
}
Code: Select all
int abs(int num) {
return (num < 0) ? -num : num;
}
Not only is this quite an idiotic way to get an abs value, the formula is of course wrong. 1) "num / (num*num)" will always produce a number smaller than 1 (except if num == 1), so prod always returns 0; 2) since num is negative, dividing it by a positive number will always yield a negative number (so even if you'd divide sqrd by num, which I more or less assume was your intention, you'd get a negative).Omega wrote:Code: Select all
int abs(int num) { if(num<0) { int sqrd=num*num; int prod=num/sqrd; return prod; } else { return num; } }