206 views
0 votes
0 votes
The C 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 a variable of type int is stored using 4 bytes and the decimal representations of arbitrarily large positive integers can be passed as arguments to divby3.

(a) Show that the given function does not work correctly for some integers larger than 10^10^9 .

(b) 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 efficient it would be.

Please log in or register to answer this question.

Related questions

0 votes
0 votes
0 answers
1
Diva Sharma asked Dec 20, 2018
226 views
Why is it easier to implement queue using circular queue instead of array with 2 indices?Also what do they mean by 2 indices?
0 votes
0 votes
0 answers
2
Rudra Pratap asked Dec 8, 2017
167 views
please explain this solution..
3 votes
3 votes
1 answer
3
hem chandra joshi asked Nov 25, 2017
315 views
From CLRS the complexity of radix sort is theta(d(n+k)) where d is # of digits , k is range and n is numbers . So how option C is right . Plz solve it on the basis of min...