UMBC CS 201, Spring 09
UMBC CMSC 201
Spring '09

UMBC | CSEE | 201 | 201 S'09 | lectures | projects | help

Function Practice Problems

Here are a few functions for you to write. Again we'll show a correct answer and its explanation.
Keep in mind that there are many ways to write correct code, so your correct answers won't necessarily match these. This course often requires more than just correct code, but code that has also been well designed. The explanations will go into some design issues.

For this exercise, in addition to writing the function described, you should write a short main() whose sole purpose is to test that function, passing it appropriate arguments and printing out the returned value.

A short disposable main() like this is called a driver by programmers in the field or a test harness by the academic community.

  1. IsOdd()

    Write a predicate function called IsOdd() that takes an integer, n, as its single argument and returns "true" if n is odd and "false" if n is not odd.
  2. NumDigits()

    Write a function called NumDigits() that has a single formal parameter of type int, n, and returns the number of digits in that integer.
  3. Doubler()

    Write a function called Doubler() that has a single formal parameter of type int, n, and returns (as an integer) the number of times n must be doubled to get to or exceed 1,000,000.
  4. Doubler() v2.0

    Let's improve the function Doubler() by passing in a value for the goal instead of having a fixed goal of 1000000. So this time, Doubler() should have two formal parameters; the first of type int is the number to be repeatedly doubled, and the second of type int is the goal, the cut-off point to stop the doubling. As before this function should return (as an integer) the number of times the number must be doubled to get to or exceed the goal.

  1. IsOdd()

    /***************************************** * Function: IsOdd() * Usage: if (IsOdd(n)) . . . * * Checks to see if a number is odd * * Input: The number to test for odd-ness * Output: Returns true if n is odd. *****************************************/ int IsOdd (int n) { /* Odd numbers are not evenly divisible by 2 */ return n % 2 != 0; }
    Explain


  2. NumDigits()

    /***************************************** * Function: NumDigits() * Usage: numDigits = NumDigits(n); * * Returns the number of digits in the * integer passed in * * Input: An integer whose number of digits * is to be determined * Output: Returns number of digits in n * * Assumptions: Only for n > 0 * This function only works properly * for positive values of n *****************************************/ int NumDigits (int n) { int numDigits = 0; /* when n reaches 0 there are no ** more digits to count */ while( n > 0) { /* strip a digit */ n /= 10; numDigits++; } return numDigits; } Explain


  3. Doubler()

    /***************************************** * Function: Doubler() * Usage: numTimes = Doubler(n); * * Returns the number of times the integer * passed in must be doubled to reach or exceed * the value 1,000,000. * * Input: An integer to be repeatedly doubled * Output: Returns number of times n had to be * doubled * * Assumptions: Only for n > 0 * This function only works properly * for positive values of n *****************************************/ int Doubler (int n) { int numTimes = 0; /* when n reaches or exceed 1000000 we're done */ while(n < 1000000) { /* double n */ n *= 2; numTimes++; } return numTimes ; } Explain


  4. Doubler() v2.0

    /***************************************** * Function: Doubler() * Usage: numTimes = Doubler(n, goal); * * Returns the number of times the integer passed in as the * first argument must be doubled to reach or exceed the * value of the goal, passed in as the second argument. * * Inputs: An integer to be repeatedly doubled * An integer which is the goal, the cut-off point for the doubling. * Output: Returns the number of times n had to be doubled to reach the goal. * * Assumptions: Only for n > 0 * This function only works properly * for positive values of n *****************************************/ int Doubler (int n, int goal) { int numTimes = 0; /* when n reaches or exceeds the goal we're done */ while(n < goal) { /* double n */ n *= 2; numTimes++; } return numTimes; } Explain

  1. Explanation of IsOdd()

    • A predicate function must return either a true value or a false value.
    • Since C has no Boolean type, it uses an int to store booleans, hence the return type of int.
    • In C, 0 is considered false and any non-zero value is considered true.
    • n % 2 will be 1 if n is odd and 0 if n is not odd (even).
    • When the expression n % 2 != 0 is evaluated, its value will be non-zero (interpreted as true) if n is odd. Its value will be 0 (interpreted as false) if n is not odd.
    • You should never assume that the non-zero value of an evaluated expression is 1 since sometimes it is some other non-zero value. You can only safely assume that a true value is non-zero.
    • Couldn't we have said return n % 2 instead ?
      The code would certainly be correct, but for readablilty reasons n % 2 != 0 is better, since what the code does will be apparent to anyone at a glance. That's the key to readablilty!

  2. Explanation of NumDigits()

    • Notice the assumption made by this function! This function will only work correctly if it is passed a positive n. Is it okay to write a function whose usefulness limited? Believe it or not, yes, BUT it is absolutely essential that you explain its limits in an assumptions section in the function header comment.
    • Where should you ensure that n is positive?
      Anywhere except in this function. Remember that a function should do one thing and only one thing. The job, or function, of this function is to return the number of digits of a positive integer that has been passed to it.
    • Each cycle of the while loop will strip the units digit from n, accomplished by n /= 10;
      and add one to the number of digits counted so far, accomplished by numDigits++;
    • The loop will run until n equals 0, meaning there are no more digits left to strip from n.
    • After there are no more digits to strip from n, the variable, numDigits, holds the number of digits that were in the original n passed into the function.
    • Finally, the value of the variable, numDigits, is returned back to the calling function.

  3. Explanation of Doubler()

    • Notice the assumption made by this function! This function will only work correctly if it is passed a positive n. As in our last example it's okay to do this as long as you explain its limits in an assumptions section in the function header comment.
    • Each cycle of the while loop will double the value of n, accomplished by n *= 2;
      and add one to the number of times n has been doubled so far, accomplished by numTimes++;
    • The loop will run until n reaches or exceeds 1000000.
      So if the termination condition is when n >= 1000000, we can use DeMorgan's law to get the continuation condition:
      !(n >= 1000000) = n < 1000000
    • When the loop ends, the variable, numTimes, holds the number of times n was doubled.
    • Finally, the value of the variable, numTimes, is returned back to the calling function.

  4. Explanation of Doubler() v2.0

    • Notice we are still making the assumption that the function will only work correctly if it is passed a positive n.
    • In this version, the loop will run until n reaches or exceeds the goal passed in as the second argument.
    • Finally, the value of the variable, numTimes, is returned back to the calling function.


Last Modified - Thursday, 22-Jan-2009 11:40:57 EST


UMBC | CSEE | 201 | 201 S'09 | lectures | projects | help