When two or more functions have the same name how does the


C++ Examples #1

1.When two or more functions have the same name, how does the compiler determine which one to use for a particular function call?

2. Write a void function called Orderme that takes two integer reference parameters a and b, and sets to the larger of the two ints, and b to the smaller. Write another Order function that takes two double arguments. Write a short main function that calls both functions and demonstrates that they work. Cout statements should only appear in function main.

3. Rewrite your answer for the previous question, using a single Order template instead of two Order functions. If done correctly,the main() function should not change.

4. What value is returned by the call F(3) where F is the recursive function given by the following code? Explain how you arrived
at your answer.

int F (int N)
{
if (N == 0)
return 1;
else
return (2 * F(N - 1));
}

5. Consider the following function:

int surprise(int number) {
if (number == 1)
return 1;
else
return number * surprise(number -1 );
}

a. What value does function surprise return when called with a value of 4?

b. What is the base case for function surprise?

6. Write an iterative (non-recursive) function that takes a number of years and a compound interest rate (as a percentage) and computes what a $1000 investment would have grown to in that many years at that interest. For example, given 3 for years and .1 (representing 10%) as the rate, the result would be $1331. Use a loop to calculate the result (don't use the pow function).

7a. Rewrite your function for the previous question as a recursive function.

7b. Which function do you think is easier to read?

7c. Which function do you find easier to write?

8. What does it mean to modularize a program? What are the benefits?

9. Consider a program that reads two whole numbers from the user, computes the greatest common divisor, and displays the results to the user.Write a program for this task where all code is within the main function.

10a. Rewrite your solution to the previous question as a modular program, using additional functions as much as possible.

10b. Which version of the program is shortest? Which version is easiest to read--and why?

Solution Preview :

Prepared by a verified Expert
C/C++ Programming: When two or more functions have the same name how does the
Reference No:- TGS01246141

Now Priced at $20 (50% Discount)

Recommended (99%)

Rated (4.3/5)