Quiz 2 preparation material
Quiz 2 will be 50 minutes long, handwritten, and conducted in the first half of
the weekly lab session.
The quiz will be closed book, closed notes, and no electronics permitted,
but students will be permitted one double-sided 8.5x11" 'cheat sheet'. (These do not need to
be handwritten.)
Some questions on the quiz will ask you to write short explanations/descriptions of different
aspects of C++, some will ask you to write short C++ code segments to achieve specific results,
some will ask you to show and/or explain the results of short C++ code segments.
The topics and suggested preparation activities for the quiz are listed below.
As with quiz 1, the best way to practice coding in general is to write and debug as many small programs as
you have time for. The best way to practice for hand-written quizzes and exams is to try
writing small programs on paper then take your answers and type them in/debug them to see
where the bugs are. If you need a refresher on how to write/compile 'from scratch' programs
(as opposed to the ones in the provided lab and make files), see the notes near the
top of the quiz 1 prep material.
- Function prototypes, calls, and implementations
- Be sure you are familiar with the parts of a function prototype, function call, and
function implementation.
If given a function prototype and a description of what it does
then you should be able to identify its return type, the parameter types and names,
write an appropriate call to it from a main routine, and write the body of the function
(if it's something short/simple enough to produce in about 15 minutes).
- The use and behaviour of if/else if/else
- You should be able to read a code segment containing a basic if/else if/else statement
and understand/describe which code sections it would run under different circumstances
(e.g. describe the conditions under which it would run the if's code section, the circumstances
under which it would run the else if's, and the circumstances under which it would run the else's.
You should also be able to write basic if/else if/else statements and conditions to achieve
a desired result.
- Input error checking
- Following a cin, you should be able to write code that checks if cin failed and use cin.clear
and cin.ignore appropriately to recover, and you should be able to detect out-of-range values
(e.g. for numeric user input, testing if it is too small or too big).
(Repeatedly trying again is discussed in the recursive section below.)
- The basic creation and use of recursive functions
- You should be able to use recursion to repeat basic actions and stop at an appropriate
point, e.g. repeatedly prompting for user input, reading it, and handling errors until the
user eventually provides a valid value.
If provided with a simple recursive function and a sample call to it, you should be
able to identify the resulting actions, output, or behaviour.
A set of practice/warmup exercises
Try each of the exercises below on paper first without any reference material, and make note
of each C++ feature/syntax rule you have to look up (these would be good things to put on your
cheat sheet!).
For each of them you'll try it on paper first, then type it into an actual program and
compile/debug it until it does what you intended.
Part 1: functions study the prototype and description below then answer questions 1-3 about it
// returns 1 if a has the smallest value, 2 if b has the smallest value, 3 if c has the smallest value
int smallest(float a, float b, float c);
- What is the return type for the function?
- What is/are the types of the function parameter(s)?
- what would the value of x be after call
x = smallest(4.5, 7.2, 0.1);
- Extra practice: try writing the full version of the function.
Note that the comment doesn't specify what the behaviour should be if two or more
parameters are equally small, e.g. smallest(5,5,5)
Part 2: if/else study the code segment below then answer questions 1-3 about it
if (x < y) {
cout << "thinking" << endl;
if (x > z) {
cout << "first" << endl;
}
} else {
if (y > z) {
cout << "second" << endl;
}
}
- What would the output be if x had value 5, y had value 10, and z had value 0?
- What would the output be if x had value 3, y had value 0, and z had value 3?
- In a situation where the code above outputs "second", what (if anything) do we
know about the value of x compared to the value of z?
Part 3: recursion study the recursive function below then show, as precisely as
possible, what output it would produce if called as dostuff(7); then briefly explain
your answer.
void dostuff(int i)
{
cout << i << endl;
if (i > 2) {
dostuff(i-2);
}
cout << "done call " << i << endl;
}
Part 4: error checking answer both 1 and 2 below.
- Suppose we ask the user to enter an integer and store the result in variable Input.
Provide a short code segment that checks if they entered a non-integer value and 'cleans up'
cin and the input buffer appropriately.
- Suppose we ask the user to enter a number greater than 7 and they reply 6 instead.
In our checking of the input, should we do a cin.clear and cin.ignore? Briefly explain why
or why not.