Hi guys,
First of all I'm new here so how are you all? and nice too meet you! :)
My name is Barak, I'm a first year student in Computer Science although I have some experience (studied in John Bryce).
We recently had a test where I think my teacher dedacted points for no good reason.
Our task was:
We have two arrays, one is a bucket array and one is an input array that holds numbers with up to 10 digits.
we had to count each number and the seconed array and increase the bucket by one in the index that is equal to the amount of digits of the number.
My way of doing that is:
for(int i = 0 ; i < size ; i++)
{
temp = num[i]; // Store The Number In A Temp Integer.
while (temp > 0) // Run While There Are Still Digits In The Number.
{
temp /= 10; // Cut The Last Digit.
DigitsNum++; // Increase The Counter.
}
Counts[DigitsNum]++; // Increase The Bucket In The Index - DigitNum.
DigitsNum = 0; // Init DigitsNum.
}
My teacher told me that "the while part should have been taken out of the function and placed in a side function that counts the digits". but in my opinion, even though there shouldn't be a significant amount of improvment, doing the while in the same function (since it is small and not difficult to understand) is just as efficient and even more then doing it like he said (I forgot to mention that in the first page they asked for most efficent while using the least ammount of memory), there for there is no reason too write it in a side function.
Am I right? wrong? missing something?
Thanks a lot,
Barak.