[My apologies to the regulars, who've heard me say this about 7 billion times]
Euno, coding is like football. The only way to learn it is to do it yourself. You'd never think that you could watch football videos and then join a team. No number of videos will help you actually play the game.
The fact that you couldn't figure out how to solve this on your own indicates that you know very little about programming. It appears that you've been getting along by finding code online. The point that Salem C and Keskiverto are making is that you'll hit a wall where this no longer works and almost certainly fail your course at that time. You need to start writing code yourself.
As a start, try this. The way your posted code handles negative numbers could be improved a lot. Can you do this on your own?
In pseudo-code, it should do this:
1 2 3 4 5 6 7
|
if numerator is negative then
change the sign of numerator and denominator
// Now the numerator is guaranteed to be positive.
if (denominator is negative) then
set the negResult flag
change the sign of the denoninator
}
|
The rest of the code from line 32 is the same.
If you've learned about recursion then it's even easier:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
if numerator is negative then
change the sign of numerator and denominator
// Now the numerator is guaranteed to be positive.
if (denominator is negative) then
return -(division(num1, -num2)); //
}
// If you get here then numerator and denominator are positive.
int quotient = 0;
while (num1 >= num2)
{
num1 = num1 - num2 ;
quotient++ ;
}
return quotient;
|