Programming Exercises

I l @ ve RuBoard

Programming Exercises

  1. Devise a function min(x,y) that returns the smaller of two double values, and test the function with a simple driver.

  2. Devise a function chline(ch,i,j) that prints the requested character in columns i through j . Test it in a simple driver.

  3. Write a function that takes three arguments: a character and two integers. The character is to be printed. The first integer specifies the number of times that the character is to be printed on a line, and the second integer specifies the number of lines that are to be printed. Write a program that makes use of this function.

  4. The harmonic mean of two numbers is obtained by taking the inverses of the two numbers , averaging them, and taking the inverse of the result. Write a function that takes two double arguments and returns the harmonic mean of the two numbers.

  5. Write a function that replaces the contents of two variables with two new values: the sum and the difference of the original contents.

  6. Write a program that reads characters from the standard input to end-of-file. For each character, have the program report whether it is a letter. If it is a letter, also report its numerical location in the alphabet. For example, c and C would each be letter 3. Incorporate a function that takes a character as an argument and returns the numerical location if the character is a letter and that returns -1 otherwise .

  7. In Chapter 6, "C Control Statements: Looping," (refer to Listing 6.18) we wrote a power() function that returned the result of raising a type double number to a positive integer value. Improve the function so that it correctly handles negative powers. Also, build into the function that 0 to any power is 0 and that any number to the 0 power is 1. Use a loop. Test the function in a program.

  8. Redo Exercise 7, but this time use a recursive function.

  9. Generalize the to_binary() function of Listing 9.9 so that it takes a second argument in the range 2 “10. It then prints the number that is its first argument to the number base given by the second argument.

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net