Code "Walkthrough" and "Inspection"

Here are 6 different versions of function findDigit with the same pre- and post- conditions.
Trace with some or all of the following data:
findDigit (30568,2);
findDigit (324,5);
findDigit(-4532,3)

  1. int findDigit(int num, int place)
    //  pre : num is a valid integer;   place >= 1
    //  post: findDigit returns the value in the position place
    {
    	int dividend = 10;
    	for (int count = 1; count < place; count++)
    		dividend = dividend * 10;
    	num = num / dividend;
    	return (num % 10);
    }

    Does it work?________If not what is the problem? If it works, is there any test data that would not work?

  2. int findDigit(int num, int place)
    {
    	int divide = 1;
    	for (int count = 2; count <= place; count ++)
    		divide = divide * 10;
    	return (num / divide);
    {

    Does it work?________If not what is the problem? If it works, is there any test data that would not work?

  3. assume power function works
    int findDigit ( int num, int place)
    {
    	return (int) num / power (10,place+1)/ power(10,place);
    }

    Does it work?________If not what is the problem? If it works, is there any test data that would not work?

  4. int findDigit ( int num, int place)
    {
    	double result = num;
    	for (int count = 1; count <= place; count++)
    		result = result - 1;
    	while (result >= 1)
    		result = result - 1;
    	return int( result * 10);
    }

    Does it work?________If not what is the problem? If it works, is there any test data that would not work?

  5. int findDigit(int num, int place)
    {
    	int count = 1;
    	int x = abs(num);
    	while ((x >= 10) && (count != place))
    	{
    		count ++;
    		x = x / 10;
    	}
    	if (count == place)
    		return (x % 10);
    	else return 0;
    }

    Does it work?________If not what is the problem? If it works, is there any test data that would not work?

  6. int findDigit (int num, int place)
    {
    	if (place == 1)
    		return (num % 10);
    	return (findDigit (num / 10, place -1);
    {

    Does it work?________If not what is the problem? If it works, is there any test data that would not work?


    Continue to:  Unit 1  / Prev  / Next