/GIVEN THE USER’S BUDGET.3 #INCLUDE <IOSTREAM>4 #INCLUDE <...
2 //given the user’s budget.3 #include <iostream>4 #include <cmath>5 using namespace std;6 int main( )7 {8 const double COST_PER_SQ_FT = 10.50;9 double budget, area, lengthSide;10 cout << "Enter the amount budgeted for your doghouse $";11 cin >> budget;12 area = budget/COST_PER_SQ_FT;13 lengthSide = sqrt(area);14 cout.setf(ios::fixed);15 cout.setf(ios::showpoint);16 cout.precision(2);17 cout << "For a price of $" << budget << endl18 << "I can build you a luxurious square doghouse\n"19 << "that is " << lengthSide20 << " feet on each side.\n";21 return 0;22 }SAMPLE DIALOGUEEnter the amount budgeted for your doghouse $25.00For a price of $25.00I can build you a luxurious square doghousethat is 1.54 feet on each side.
Usually, all you need do to use a library is to place an
includedirective and a
using#includemay not bedirective for that library in the file with your program. If things work with just these
enoughdirectives, you need not worry about doing anything else. However, for some libraries
on some systems you may need to give additional instructions to the compiler or
explicitly run a linker program to link in the library. The details vary from one system
to another; you will have to check your manual or a local expert to see exactly what is
necessary.
A few predefined functions are described in Display 3.2. More predefined functions
are described in Appendix 4. Notice that the absolute value functions
absand
labsare
abs and labsPredefined Functions 95
F
UNCTIONST
HATR
ETURNAV
ALUEFor a function that returns a value, a function call is an expression consisting of the function name followed by arguments enclosed in parentheses. If there is more than one argument, the argu-ments are separated by commas. If the function call returns a value, then the function call is an expression that can be used like any other expression of the type specified for the value returned by the function.S
YNTAXFunction_Name(Argument_List)where the Argument_List is a comma-separated list of arguments:Argument_1, Argument_2,. . ., Argument_LastE
XAMPLESside = sqrt(area);cout << "2.5 to the power 3.0 is " << pow(2.5, 3.0);in the library with header file
cstdlib, so any program that uses either of these func-
tions must contain the following directive:
#include <cstdlib>Also notice that there are three absolute value functions. If you want to produce the
fabsabsolute value of a number of type
int, use
abs; if you want to produce the absolute
value of a number of type
long, use
labs; and if you want to produce the absolute value
of a number of type
double, use
fabs. To complicate things even more,
absand
labsare in the library with header file
cstdlib, whereas
fabsis in the library with header
file
cmath.
fabsis an abbreviation for floating-point absolute value. Recall that numbers
with a fraction after the decimal point, such as numbers of type
double, are often called
floating-point numbers.Another example of a predefined function is
pow, which is in the library with header
powfile
cmath. The function
powcan be used to do exponentiation in C++. For example, if
you want to set a variable
resultequal to
xy
, you can use the following:
result = pow(x, y);Hence, the following three lines of program code will output the number
9.0to the
screen, because
(3.0)2.0