The material presented in this guide covers the concepts emphasized on Test 2. It is meant to guide students studying and reviewing for Test 2; it is not guarranteed to be comprehensive. Actual test questions will differ from any examples given here. Students should use this guide in addition to other study activities (like reading Chapters 8-11, 13-17, and 21 reviewing completed lab assignments, old quizzes, etc.) Knowledge of Chapters 1-7 is assumed, but the emphasis is on material in Chapters 8-11, 13-17, and 21.
if/else
, switch
,
while
,
and for
statements, and the conditional operator.
sqrt
,
pow
,
clock
,
rand
, and
srand
.
For example, given the function
func(3, 12)
?
Be able to write a C++ function given its specification. In order to write the code you may need to:
const
in
parameter declarations and method declarations
For example, complete the following function named
is_reduced
that
accepts two integer parameters,
numerator
and denominator
. The
function returns true if numerator/denominator
represents a fraction reduced to lowest terms;
otherwise, the function returns false if the fraction
numerator/denominator can be reduced. For example,
the expression is_reduced(1, 2)
would evaluate
to true, but
is_reduced(2, 4)
would evaluate to false.
Improper fractions are acceptable (7/3 is reduced, but
9/3 is not). The function
should return false when denominator
is zero.
As another example, suppose you have the class
Rational
defined as
Complete the following function named
is_reduced
that
accepts a single Rational
object.
The
function returns true if the Rational
object
represents a fraction reduced to lowest terms;
otherwise, the function returns false.
For example,
the expression is_reduced({1, 2})
would evaluate
to true, but
is_reduced({2, 4})
would evaluate to false.
Improper fractions are acceptable (7/3 is reduced, but
9/3 is not).
For more practice, try this one:
Complete the following sum_nonnegative
function used to compute the sum of all the values in a
vector of integers that are greater than or equal
to zero.
displays 9, since 3 + 5 + 0 + 1 = 9.
Beware! Any problems that states that "the function should
do no I/O"
means you should not get input from the user nor should you print
anything on the screen. This means you must use neither
the std::cout
nor std::cin
objects within
the function.
There may be a few general questions about programming or C++ that do not involve reading or writing code.
Also, take time to review all the code we have developed in class. It has all been uploaded to the course repository on GitHub.