PSP
PSP
• cout<<setw(10)<<setiosflags(ios::left)<<142;
Math Library Functions Revisited
Me tho d De sc rip tio n Exa m p le
ceil( x ) rounds x to the smallest integer ceil( 9.2 ) is 10.0
not less than x ceil( -9.8 ) is -9.0
cos( x ) trigonometric cosine of x cos( 0.0 ) is 1.0
(x in radians)
exp( x ) exponential function ex exp( 1.0 ) is 2.71828
exp( 2.0 ) is 7.38906
fabs( x ) absolute value of x fabs( 5.1 ) is 5.1
fabs( 0.0 ) is 0.0
fabs( -8.76 ) is 8.76
floor( x ) rounds x to the largest integer floor( 9.2 ) is 9.0
not greater than x floor( -9.8 ) is -10.0
fmod( x, y ) remainder of x/y as a floating- fmod( 13.657, 2.333 ) is 1.992
point number
log( x ) natural logarithm of x (base e) log( 2.718282 ) is 1.0
log( 7.389056 ) is 2.0
log10( x ) logarithm of x (base 10) log10( 10.0 ) is 1.0
log10( 100.0 ) is 2.0
pow( x, y ) x raised to power y (xy) pow( 2, 7 ) is 128
pow( 9, .5 ) is 3
sin( x ) trigonometric sine of x sin( 0.0 ) is 0
(x in radians)
sqrt( x ) square root of x sqrt( 900.0 ) is 30.0
sqrt( 9.0 ) is 3.0
tan( x ) trigonometric tangent of x tan( 0.0 ) is 0
(x in radians)
Fig . 3.2 M a th lib ra ry func tio ns.
Mathematical Library Functions
Casts
• dataType(expression)
• int (a*b)
Run time cast
staticCast<dataType>(expression)
staticCast<int>(a*b) is same as compile time
int (a*b)
QUESTION
• the New Jersey state income tax is assessed at
2% of taxable income for incomes less than or
equal to $20,000. For taxable incomes greater
than $20,000, state taxes are 2.5% of the
income exceeding $20,000 plus a fixed
amount of $400.
The expression to be tested is whether taxable
income is less than or equal to $20,000,
• It is only used to solve the problems that require
two or more unrelated decisions or in which all
the decisions must be processed.
if(hours<9)
if (distance>500)
cout<< “snap”;
else
cout<< “pop”;
if-else chain
Input Code Marital Status
M Married
S Single
D Divorced
W Widowed
if else chain
Monthly Sales Income
• Greater than or equal to $50,000
– $375 plus 16% of sales
• Less than $50,000 but greater than or equal to $40,000
– $350 plus 14% of sales
• Less than $40,000 but greater than or equal to $30,000
– $325 plus 12% of sales
• Less than $30,000 but greater than or equal to $20,000
– $300 plus 9% of sales
• Less than $20,000 but greater than or equal to $10,000
– $250 plus 5% of sales
• Less than $10,000
– $200 plus 3% of sales
The switch Statement