CS401 - Solutions for Exam 2

Make sure you have your exam handy when you work through these solutions.

The average was 76.6% (about 119 out of 150) which was decent, but not nearly as good as the first exam.  I felt that people did generally well on the output questions, but went all or nothing on the questions where you had to write code.  Make sure you can write a few functions when it comes time to take the final, I guarantee you'll be asked to do this.

About writing code on exams... make sure you do only what is asked.  Many of you printed things out for no reason (only print from a function when it supposed to print, don't assume that this side-effect is desired by the function user).  Also, many of you wrote me large blocks of code that did things not specified in the problem.  If it says to "write a function", and nothing else, then just write the function definition, don't write a prototype or a main() block, or anything else.  Only do that if it says to do so. It is very important that you compare what you did to what was expected, so you can see what you misunderstood about the questions.

Lastly, a few of you may have gotten the other sections' exam.  If these answers look strange to you, just switch to the other solution page and it should be ok.
 

  1. True/False
  2. (a)  TRUE - persistant operators stay in effect until you switch them back to the default, thus all sucessive items (those that follow) are affected.
    (b)  FALSE - reference parameters require that the caller provide variables (as the argument), thus a call with this argument missing (what default values give you the ability to do) would screw up the PBR because there would be nothint to refer to!
    (c)  FALSE - the return type and the details of the parameter list are independent issues.  Any valid return type is allowed and any valid parameter list is allowed, no relationship there, other than logical).
    (d)  FALSE - you cannot have two identifiers with the same name in the same block, no matter what.
    (e)  TRUE - this is called overloading.  It is not only legal, but very useful and powerful in C++.
  3. Describe in a few lines - Essentially, there were some key ideas I was looking for you to mention. 
  4. (a)  function prototype and purpose?

    A function prototype describes the interface to the function by giving the return type, the name of the function, and the types of and number of parameters.  The purpose of a prototype to allow you to call functions before they are actually defined (in essence, they are like function "declarations").

    (b)  library and the how the process works in C++?

    A library is a collection of related functions, constants, objects, etc. that are packaged together.  To use a library you need to use the proper #include directive in your C++ file.  The process begins with the preprocessor replacing the directive with the header file (a collection of prototypes), and then is completed when the linker brings together the object files containing the compiled code implementing all the functions in the library.

    (c) for construct

    There are four parts:

       for (init; condition; update)
          body;

    The initialization occurs first, followed by a test of the condition, followed by the body, up to the update, then back again to the condition, and so on, until the condition is false.
     

  5. multiple choice
  6. (a)  IV - the switch statement is most useful when building a menu-based system (e.g., as in project 4).  None of the other answers have to do with the switch statement.

    (b)  III - void doStuff(int, int=1);  In order to work on 1 or 2 arguments (but not 0), you need to look for a function with a default parameter in the second position.  This makes it optional.  There can't be a default value in the first position because the function must fail on 0 inputs.  The only possible choice left is III.

    (c)  III - whether each parameter should be global or local.  This is the only decision that you cannot make.  All parameters are automatically local to the function (remember us talking about that?), so they can't be global no mattter what you do.  The other answers are all things you have to decide on when writing a function definition.

    (d)  II - calc(x + 1.3, y + 1.8);  This is the only illegal call because the first argument is PBR, and this call does not contain a variable (all alone) in the first argument position. 

    (e)  III - handles file inclusion directives.  That's all the preprocessor does is handle directives (and macros, and everything else starting with "#").  The other items listed are all handled by different parts of the compiler.
     

  7. output - read this one closely, there is no loop!  A number of people thought the if statement was a while, not so.  This program has only four lines of numerical output.  A very large number of people skipped the first print statement as well.

  8. Please enter an integer: 4<enter>
    4 2
    4 5
    4 7
    5 2
  9. output - most people got this one easy.  Be careful on your overloaded function calls and get the correct one.  The functions just multiplied the sum of the arguments with the number of (or count) of the arguments.  Also note all the parameter passing is PBV, so no need to worry about side-effects of x and y back in main().

  10. 18 51
    21 20
  11. output - remember lab 4?  This is a very watered down version of the function you saw there.  The first two calls are really easy, they finish up right away.  (1 for 1, and 3*1 for 3).  But, when you pass 8 in there, you have a few recursive calls to wade through.  You end up getting 3 * 3 * 3 * 3 * 1 (make sure you see why when you step through it), which is 81.  Each recursive call decreases the argument down by 2 (going from 8, to 6, to 4, to 2), thus "piling up" 4 multiplications of 3 in a row.  No "else" is required (as several people asked) because the return in the if will end the function call.

  12. 1
    3
    81
  13. output - you had to set aside a width of 5 for each value printed (meaning there were 3 blank spaces just before the two-digit output values).  Also, the increment of i in the function refers to the global i.  A few of the calls get hairy, but just remember, the first argument is PBR, so just figure out which variable bumpUp() will be modifying, then realize the second is PBV, so just get ready to evaluate whatever you find there, then copy it down to the function into j.  The final call refers to i in two ways, one through global reference, the other through a refeence parameter.

  14.    10   15
       11   20
       12    8
       13   16
       24   16
  15. structs - just read the supplemental reading on structs if you missed any points.  This problem amounted to the first few paragraphs of those notes.  Some people used TVshow without creating a variable first.

  16. Tvshow show;     // creates a new variable called show
    show.name = "Simpsons";     // put values in using the dot operator and field names.
    show.startingHour = 8;
    show.startingMin = 0;
    show.length = 30;
    show.network = "FOX";
  17. function - if you missed this, then you probably didn't do the self-answer quiz2!  A nearly identical problem was asked there having you return the largest of three values.  There are many ways to go about it, let me go a different way than we did the largest in the quiz.
  18. //directives here

    int smalles(int x, int y, int z) {
       if ((x <= y) && (x <= z))
          return x;
       if ((y <= x) && (y <= z))
          return y;

       return z;
    }

  19. function - many people wrote huge solutions to this one, and it really is quite short.  Just think "loop" and you've got most of it.
  20.  
    void print_evens(int high) {
       for (int i=2; i<=high; i+=2)
          cout << i << endl;
    }
     

    // a sample call, keep it as simple as possible

    print_evens(22);
     

  21. function - this was the hardest problem.  First you had to nail down the interface (void function with 4 parameters), then write the body.  In the body of this funciton you need to cacluate change somehow.  The idea is that you first give as many quarters as you can, then switch to dimes, end up with nickels.  Note:  it did not say to print and it did not say to show a sample call, thus you should not have written those things.
  22.  
    void get_change(int p, int &q, int &d, int &n) {

       q = p / 25;
       int amtleft = p - q*25;
       d = amtleft / 10;
       amtleft = p - q*25 - d*10;   // find out how much is left after removing the quarters
       n = amtleft / 5;

    }
     


 

Last Updated: 6/18/01 by H. Chad Lane, hcl@cs.pitt.edu
© 2000-2001 Jim Skrentny, University of Wisconsin