CPS 006, Spring 2000, Quiz Questions, Feb 21 - Feb 25

Two or three of these questions will be used for an in-class quiz at the end of the week. It's possible that the questions may be modified slightly.
  1. You're creating a program and one of the #include statements generates an error that the included file cannot be find. Which phase is most likely the problem:
    1. preprocessing
    2. compiling
    3. linking
  2. You're creating a program and get an error message that the function Foo::DoStuff, called from runfoo.cpp is an unresolved reference. Which phase is most likely the problem:
    1. preprocessing
    2. compiling
    3. linking
  3. What purpose does a break statement serve when used in a switch statement and how is this different from how break is used in a loop?
  4. The next four problems refer to the program below

    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    #include "tvector.h"
    #include "prompt.h"
    
    const int MAXLEN = 15;
    
    int main()
    {
        string filename = PromptString("enter name of file: ");
    
        ifstream input(filename.c_str());
        string word;
        tvector<int> lencounter(MAXLEN,0);
    
        // read all words, count # occurrences of each word length
        while (input >> word)
        {
            if (word.length() < MAXLEN)
            {   lencounter[word.length()]++;
            }
        }
    
        for(size_t k=0; k < MAXLEN; k++)
        {   cout << k << "\t" << lencounter[k] << endl;
        }
        return 0;
    }
    
    When this program is run on hawthorne.txt the output below is generated:
    prompt> countlen
    enter name of file: data/hawthorne.txt
    0       0
    1       2591
    2       14549
    3       18490
    4       13535
    5       9565
    6       8006
    7       6298
    8       4540
    9       3114
    10      2240
    11      1332
    12      739
    13      415
    14      212
  5. In the program, the output of all vector counts occurs in main. Write a function that can be called in place of the loop in main, the function will be called as follow:
  6.   Print(lencounter);
    (the loop will be in Print, you must write the function Print.)
  7. Write a function that determines the wordlength that is maximal, that is there are more words of the maximal wordlength than any other length. In the run above the maximal word length is three since there are 18,490 length-three words, more than any other length. The function must have the prototype below. Write the function and show how to call it from main so that the maximal length and the number of occurrences can be printed. For the example above, MaximalLength should return three.
  8.   int MaximalLength(const tvector<int>& len)
      // pre: len contains MAXLEN entries
      // post: returns index of maximal value in len
    
  9. Write a function that computes the average word-length of all the values in a vector from the program.
  10.   double AverageLength(const tvector<int>& len)
      // pre: len contains MAXLEN entries
      // post: returns average wordlength computed from values in len
    
  11. Move the word-reading loop out of main by writing a function CountLength so that the body of main is as follows:

  12. int main()
    {
        string filename = PromptString("enter name of file: ");
        tvector<int> lencounter(MAXLEN,0);

        CountLengths(filename, lencounter);
        Print(lencounter);

        return 0;
    }