Concept explainers
define keyword is used to define macros
An object-like macros is defined i.e.
#define pi 3.14
A function-like macros is defined i.e.
#define VOL( x ) ( 4.0 / 3 ) * pi * ( x * x * x )
Variable r is declared to store the value of radius.
printf (): used to print the data onto output screen.
Program Description: Purpose of the program is to define macros tofind the volume of sphere for radius values ranging from 1 to 10 in tabular form.
Explanation of Solution
Program: Following is C++ program that defines macros to find the volume of sphere for radius values ranging from 1 to 10.
#include<stdio.h>//header file for input output //defining macros #define pi 3.14 #define VOL( x ) ( 4.0 / 3 ) * pi * ( x * x * x ) //start of main intmain() { //defining variable for storing value of radius int radius; //tabular reprsentation of data printf( "Radius\tVolume of sphere\n" ); printf( "\n" ); //for loop to iterate over the value of radius ranging from 1 to 10 for (radius = 1; radius < 11; radius++ ) { printf( "%d\t%f\n", radius, VOL( radius ) ); } }//end of main
Explanation:
The given C++ program calculates the volume of sphere.
For declaring the macros statements the define keyword is used for VOL(x) and pi.
Then using the for loop to iterate over the radius variable from 1 to 10 and to print one by one via “\n”. The “\t” is used to print six spaces between the radius and their volume.
Sample Output:
Want to see more full solutions like this?
Chapter 13 Solutions
C How to Program (8th Edition)
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr