www.gibmonks.com

Main Page




Previous Page
Next Page

[Page 1283 (continued)]

Exercises

F.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 ) * p * r3

where p is 3.14159.

F.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.

F.6

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

F.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 F.6 to determine the smallest number. Input the values from the keyboard.

F.8

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

F.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.


[Page 1284]
F.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.

F.11

Rewrite the solutions to Exercise F.4 to Exercise F.10 as inline functions.

F.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 ) )
    


Previous Page
Next Page