I received the results of one of my tests on Wednesday, and caused a bit of a debate with my professor. It was in C++ class. One of the practical portion questions was pretty simply:

Write a piece of code that will use a for loop to calculate the number of digits in a positive integer (Hint use division by ten)

So I created a short, simple function:

int digits(int num)
{
int i;
for (i = 0; num > 0; i++, num /= 10);
return i;
}

To my surprise, I only got 3 out of 4 on that question. Apparently, the loop was supposed to start at 1 (rather than 0), and the test changed to > 9. This would result in 0 having one digit.

Read the rest of this entry

, ,