/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

include

directive and a

using#includemay not be

directive for that library in the file with your program. If things work with just these

enough

directives, 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

abs

and

labs

are

abs and labs

Predefined Functions 95

F

UNCTIONS

T

HAT

R

ETURNA

V

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_Last

E

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

fabs

absolute 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,

abs

and

labs

are in the library with header file

cstdlib

, whereas

fabs

is in the library with header

file

cmath

.

fabs

is 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

pow

file

cmath

. The function

pow

can be used to do exponentiation in C++. For example, if

you want to set a variable

result

equal to

x

y

, you can use the following:

result = pow(x, y);

Hence, the following three lines of program code will output the number

9.0

to the

screen, because

(3.0)

2.0

is

9.0

:

double result, x = 3.0, y = 2.0;cout << result;

96 Function Basics

Display 3.2 Some Predefined FunctionsNAME DESCRIPTION TYPE OF EXAMPLE VALUE LIBRARYTYPE OF HEADERVALUE ARGUMENTSRETURNEDsqrt Square root double double sqrt(4.0) 2.0 cmathpow Powers double double pow(2.0,3.0) 8.0 cmathabs Absolute int int abs(-7)7cstdlibabs(7)value forintlabs Absolute long long labs(-70000)70000 cstdliblabs(70000) 70000longcmath