tailieunhanh - A Complete Guide to Programming in C++ part 22
A Complete Guide to Programming in C++ part 22. This book provides both novice and experienced programmers with a comprehensive resource manual for the C++ programming language. Readers gain experience in all aspects of programming, from elementary language concepts to professional software development, with in depth coverage of all the language elements en route. These elements are carefully ordered to help the reader create useful programs every step of the way. | EXERCISES 189 Exercise I a. Write the function sum with four parameters that calculates the arguments provided and returns their sum. Parameters Four variables of type long. Returns The sum of type long. Use the default argument 0 to declare the last two parameter of the function sum .Test the function sum by calling it by all three possible methods. Use random integers as arguments. b. Now restructure your program to store the functions main and sum in individual source files for example and . Exercise 2 a. Write an inline function Max double x double y which returns the maximum value of x and y. Use Max instead of max to avoid a collision with other definitions of max. Test the function by reading values from the keyboard. Can the function Max also be called using arguments of the types char int or long b. Now overload Max by adding a further inline function Max char x char y for arguments of type char . Can the function Max still be called with two arguments of type int Exercise 3 The factorial n of a positive integer n is defined as n 1 2 3 . . . n-1 n Where 0 1 Write a function to calculate the factorial of a number. Argument A number n of type unsigned int. Returns The factorial n of type long double. Formulate two versions of the function where the factorial is a. calculated using a loop b. calculated recursively Test both functions by outputting the factorials of the numbers 0 to 20 as shown opposite on screen. 190 CHAPTER 10 FUNCTIONS Exercise 4 Write a function pow double base int exp to calculate integral powers of floating-point numbers. Arguments The base of type double and the exponent of type int. Returns The power baseexp of type double. For example calling pow 3 returns the value This definition of the function pow means overloading the standard function pow which is called with two double values. Test your function by reading one value each for the base and the exponent from the keyboard. Compare the .
đang nạp các trang xem trước