Programming in C

(Barry) #1

162 Chapter 8 Working with Functions


Exercises



  1. Type in and run the 16 programs presented in this chapter. Compare the output
    produced by each program with the output presented after each program in the
    text.

  2. Modify Program 8.4 so the value of triangularNumberis returned by the func-
    tion.Then go back to Program 5.5 and change that program so that it calls the
    new version of the calculateTriangularNumberfunction.

  3. Modify Program 8.8 so that the value of epsilonis passed as an argument to the
    function.Try experimenting with different values of epsilonto see the effect that
    it has on the value of the square root.

  4. Modify Program 8.8 so that the value of guessis printed each time through the
    whileloop. Notice how quickly the value of guessconverges to the square root.
    What conclusions can you reach about the number of iterations through the loop,
    the number whose square root is being calculated, and the value of the initial
    guess?

  5. The criteria used for termination of the loop in the squareRootfunction of
    Program 8.8 is not suitable for use when computing the square root of very large
    or very small numbers. Rather than comparing the differencebetween the value of
    xand the value of guess^2 , the program should compare the ratioof the two values
    to 1.The closer this ratio gets to 1, the more accurate the approximation of the
    square root.
    Modify Program 8.8 so this new termination criteria is used.

  6. Modify Program 8.8 so that the squareRootfunction accepts a double precision
    argument and returns the result as a double precision value. Be certain to change
    the value of the variable epsilonto reflect the fact that double precision variables
    are now being used.

  7. Write a function that raises an integer to a positive integer power. Call the func-
    tion x_to_the_ntaking two integer arguments xand n.Have the function return
    a long int,which represents the results of calculating xn.

  8. An equation of the form
    ax^2 + bx + c = 0


is known as a quadraticequation.The values of a,b,and cin the preceding example
represent constant values. So
4x^2 - 17x - 15 = 0
Free download pdf