450 views
1 votes
1 votes

The function $divby3$ given below is intended to check whether a given number is divisible by 3. It assumes that the argument $(number)$ is a string containing the decimal representation of a positive integer, and returns 1 or 0 depending on whether the integer is divisible by 3 or not.

int divby3(char *number)
{
int sum = 0;
while (*number != ’\0’) {
sum += *number - ’0’;
number++;
}
return (sum % 3) ? 0 : 1;
}

Assume that (i) a variable of type $int$ is stored using 4 bytes, and (ii) the decimal representations of arbitrarily large positive integers can be passed as arguments to $divby3$.

  1. Show that the given function does not work correctly for some integers larger than $10^{{10}^9}.$
  2. Modify the above function so that it works as intended for all positive integers.

note: The smaller the number of ALU operations used by your function, the more marks you will get.

Please log in or register to answer this question.

Related questions

4 votes
4 votes
0 answers
1
17 votes
17 votes
4 answers
4