Mon Multipl: L T (Hint
Mon Multipl: L T (Hint
Mon Multipl: L T (Hint
1. Class member names have class scope and are generally not recognized outside
the class without qualification. Can you think of any class members that are
recognized without being qualified?
2. Write a gcd that is nonrecursive and that can take any int arguments, not just
nonnegative ones.
3. Write a function lcm that takes two integer arguments and returns the least com-
mon multiple of all the arguments (e.g., lcm (-15,12) is 60). Modify operator-
and operator+ in Fraction to use lcm. (Hint: Use gcd.)
5. Add to the class Fraction a member function floor, which returns the largest
integer less than or equal to the fraction. (Hint: Consider both positive and
negative fractions.) Also add const declarations in every appropriate place for
Fraction.
8. Add a member to CirBuf so that the capacity of a circular buffer object can
be increased dynamically. The member prototype should be int grow(int n),
which causes the capacity to increase by n characters. Any unconsumed char-
acters in the buffer remain. A false is returned when grow() fails.
9. Examine closely the function parti tion used in our quicksort (Section 3.4).
Can you show that, after the while loop, the element a [i] is not less than pe?
Is it an improvement to the partition code to modify the exchange call to
exchange (a, i++, j--)?
10. Given an array of distinct integers, write an efficient program to find the median,
the element of the array whose value is in the middle. Namely, roughly half of
the elements are over and half are under the median. (Hint: Modify parti tion.)
11. List the explicit conversion notations and explain their meaning using your
own words.
12. Write a function sumthat produces the total of an indefinite number of argu-
ments uniformly of type int, float, or double. Make sumalways return double
for now.
13. Consider the following code for the function swap.Is there any syntax problem
here? Does the function achieve its intended purpose? Why?
void swap(int& a, int& b)
{ int& tmp = bi
b = ai
14. In a function header, does it make sense to declare as const any formal param-
eter other than a pointer or a reference? Why? Is it possible to use the same
identifier both as a variable and as a function name? What about an enumtag?
A typedef name?
15. Write a program that creates an array frac3rr with new,initializes each ele-
ment frac_arr [i] with the Fraction object i~l' displays the array, and then
deletes it.
16. A stack is a first-in/last-out buffer. If the numbers 1.0 2.0 3.0 are entered
I I
into the buffer in that order, then they are taken out of the stack in the sequence
3.0, 2.0 I 1. O.The operation push enters an item on the top of a stack, and
the operation pop removes an item from the top of the stack. These are the
only two operations that are allowed to modify a stack. Following the circular
buffer example in Section 3.12, implement a class Stack for type double.
17. Exercise RP-1: Use the Stack object in Exercise 16 to implement a program rp
to evaluate a reverse Polish expression. For example,
displays the value of 3.1 / -4.2 + (5.3 - 6.4) * 7.5. (Hint: Use the library
function strtod.)
19. (a) If you have a function sumat file scope and a function sumin a class, is the
function sumconsidered to be overloaded? Why? (b) If the file scope sumhas
a different signature than the class scope sum,can you call the file scope sum
from a class member function without using the scope operator : :?
20. A function compareis a natural candidate for overloading. Write versions that
compare integers, floating-point numbers, strings, characters, and fractions.
21. In C++, the return type of a function is not considered part of the signature
of a function. Does this prevent you from defining functions that, in effect,
make the return value part of the signature? If not, explain the mechanism you
would use.