Exercises

  1. E.4 Write a program that defines a macro with one argument to compute the volume of a sphere. The program should compute the volume for spheres of radii from 1 to 10 and print the results in tabular format. The formula for the volume of a sphere is

    
    ( 4.0 / 3 ) * π * r3
    

    where π is 3.14159.

  2. E.5 Write a program that produces the following output:

    
    The sum of x and y is 13
    

    The program should define macro SUM with two arguments, x and y, and use SUM to produce the output.

  3. E.6 Write a program that uses macro MINIMUM2 to determine the smaller of two numeric values. Input the values from the keyboard.

  4. E.7 Write a program that uses macro MINIMUM3 to determine the smallest of three numeric values. Macro MINIMUM3 should use macro MINIMUM2 defined in Exercise E.6 to determine the smallest number. Input the values from the keyboard.

  5. E.8 Write a program that uses macro PRINT to print a string value.

  6. E.9 Write a program that uses macro PRINTARRAY to print an array of integers. The macro should receive the array and the number of elements in the array as arguments.

  7. E.10 Write a program that uses macro SUMARRAY to sum the values in a numeric array. The macro should receive the array and the number of elements in the array as arguments.

  8. E.11 Rewrite the solutions to Exercises E.4E.10 as inline functions.

  9. E.12 For each of the following macros, identify the possible problems (if any) when the preprocessor expands the macros:

    1. #define SQR(x) x * x

    2. #define SQR(x) (x * x)

    3. #define SQR(x) (x) * (x)

    4. #define SQR(x) ((x) * (x))

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset