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
.
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.
E.6 Write a program that uses macro MINIMUM2
to determine the smaller of two numeric values. Input the values from the keyboard.
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.
E.8 Write a program that uses macro PRINT
to print a string value.
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.
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.
E.11 Rewrite the solutions to Exercises E.4–E.10 as inline
functions.
E.12 For each of the following macros, identify the possible problems (if any) when the preprocessor expands the macros:
#define SQR
(x) x * x
#define SQR
(x) (x * x)
#define SQR
(x) (x) * (x)
#define SQR
(x) ((x) * (x))