Spring 2025 CS 31

Project 3 Practice Exercises
Function Fun

These exercises are designed to help get you comfortable with functions. You will not turn anything in for these exercises, but you must do them anyway. If for the full Project 3 you ask us for help on something that is similar to one of these exercises, we may ask you to first explain your answer to that similar item in these exercises. (This is to help you learn to recognize common patterns and common errors.)

For all programs shown, assume the appropriate headers have been included, along with using namespace std; being present. In other words, we won't show #include <iostream>, etc. When asked for the output of a program, play computer and determine the output by hand instead of just running the program, since this is a skill you'll need when debugging (and for the exams). Similarly, when asked to find an error that would cause compilation to fail, try to do that yourself first, to train your eye to notice what looks wrong. After doing a problem yourself, you can, if you like, run it on a computer to check your answer.

  1. What is the output produced by this program?

    int square(int n)
    {
        return n*n;
    }
    
    int main()
    {
        for (int k = 1; k <= 6; k++)
            cout << "The square of " << k << " is " << square(k) << endl;
    }
    
  2. Why doesn't this code compile correctly?

    int square(int n)
    {
        return n*n;
    }
    
    int main()   // This program contains incorrect code!!
    {
        cout << "Enter an integer: ";
        int k;
        cin >> k;
        cout << "The square of " << k << " is " << square(int k) << endl;
    }
    
  3. Why doesn't this code compile correctly?

    int main()   // This program contains incorrect code!!
    {
        int square(int n)
        {
            return n*n;
        }
    
        cout << "Enter an integer: ";
        int k;
        cin >> k;
        cout << "The square of " << k << " is " << square(k) << endl;
    }
    
  4. What is the output produced by this program?

    int factorial(int n)
    {
        int prod = 1;
        for (int i = 2; i <= n; i++)
            prod *= i;
        return prod;
    }
    
    int main()
    {
        for (int k = 1; k <= 6; k++)
            cout << "The factorial of " << k << " is " << factorial(k) << endl;
    }
    
  5. What is the output produced by this program? Between the function printFactorial and the function factorial of the previous question, which is likely to be useful in a wider variety of programs than the other, and why?

    void printFactorial(int n)
    {
        int prod = 1;
        for (int i = 2; i <= n; i++)
            prod *= i;
        cout << "The factorial of " << n << " is " << prod << endl;
    }
    
    int main()
    {
        for (int k = 1; k <= 6; k++)
            printFactorial(k);
    }
    
  6. What is the output produced by this program?

    int countChars(string s, char c)
    {
        int total = 0;
        for (int k = 0; k != s.size(); k++)
        {
            if (s.at(k) == c)
                total++;
        }
        return total;
    }
    
    int main()
    {
        cout << countChars("abracadabra", 'a') << endl;
        int n = 10 * countChars("99 Cent Only", '9') + 5;
        cout << n << endl;
        cout << countChars("O Rose, thou art sick!", 'f') << endl;
    }
    
  7. Why doesn't this code compile correctly?

    int countChars(string s, char c)
    {
        int total = 0;
        for (int k = 0; k != s.size(); k++)
        {
            if (s.at(k) == c)
                total++;
        }
        return total;
    }
    
    int main()   // This program contains incorrect code!!
    {
        cout << countChars('s', "She sells seashells down by the seashore") << endl;
    }
    
  8. What is the output produced by this program?

    bool contains(string s, char c)
    {
        for (int k = 0; k != s.size(); k++)
        {
            if (s.at(k) == c)
                return true;
        }
        return false;
    }
    
    int main()
    {
        if (contains("Computer Science 31", '3'))
            cout << "W";
        if ( ! contains("Smallberg", 'x'))
            cout << "o";
        if (contains("Smallberg", 'l'))
            cout << "w";
        if (contains("Start your CS 31 projects early!", ' '))
            cout << "!" << endl;
    }
    
  9. Why does the following program produce the output x instead of xy?

    bool contains(string s, char c)  // This code is suspect
    {
        for (int k = 0; k != s.size(); k++)
        {
            if (s.at(k) == c)
                return true;
            else
                return false;
        }
    }
    
    int main()
    {
        if (contains("xyz", 'x'))
            cout << "x";
        if (contains("xyz", 'y'))
            cout << "y";
        cout << endl;
    }
    
  10. Why does the following program exhibit undefined behavior (i.e., you might get different results on different machines)?

    int countSpacesInFirstSentence(string s)  // This code is suspect
    {
        int nSpaces = 0;
        for (int k = 0; k != s.size(); k++)
        {
            if (s.at(k) == ' ')
                nSpaces++;
            else if (s.at(k) == '.')
                return nSpaces;
        }
    }
    
    int main()
    {
        cout << countSpacesInFirstSentence("Call me Ishmael.") << endl;
        cout << countSpacesInFirstSentence("I am Sam.  Sam I am.") << endl;
        cout << countSpacesInFirstSentence("Where now? Who now? When now?") << endl;
    }
    
  11. What is the output of the following program?

    string justLetters(string s)
    {
        string result = "";
        for (int k = 0; k != s.size(); k++)
        {
            if ( islower(s.at(k)) )
                result += s.at(k);
            if ( isupper(s.at(k)) )
                result += tolower(s.at(k));
        }
        return result;
    }
    
    int main()
    {
        cout << justLetters("CS 31's 1st lecture is dated Mar. 31.") << endl;
    }