C/c
Expert: Zlatko - 5/26/2010
QuestionHey am kinda new @ this so..i kinda need some help understanding this part..
if (decimal < 0)
{
decimal = -decimal;
neg_flag = 1;
}
do
{
old_decimal = decimal; // for test
remain = decimal % 2;
AnswerHello Zizo
The first part is keeping the decimal positive. So that if it is less than 0, it is multiplied by -1 to make it positive. The neg_flag is used to remember that the original decimal was negative.
The remain is the remainder that you would have when dividing the decimal by 2. For example, if decimal is 2, 2/2 has a remainder of 0 so 2 % 2 is 0. The "%" operator is also known as the modulo operator and is used to get the remainder. 3/2 has a remainder of 1, 4/2 has a remainder of 0, 5/2 has a remainder of 1 and so on. In this case the remainder is 0 or 1, and might be used to determine if decimal is even or odd.
That is all I can say about this code. It is too short for me to know what the programmer is trying to accomplish.
Best regards
Zlatko