cse 100 s s input: cin s type casting math library functions math.h sqrt(n) fabs(n) cos(n) log10(n)...
TRANSCRIPT
Math Library Functions
name of the function
what it does
data type of argument
data type of returned value
Math Library Functions
Syntax: function_name (argument);function_name (argument);
Ex. sqrt(49) pow(2.1, 3)
abs(-34.5) cos(30)
abs(34.5)
*
Math Library Functions
nested functions
sqrt( pow ( fabs (-4), 3) ) =
sqrt( pow ( 4.0 , 3) ) =
sqrt( 64.0 ) = 8.0
* * *
Type Casting
The explicit conversion of a value from one data type to another.
Syntax: data_type (expression)data_type (expression)
* *
int (5.34 * 1.68)
int (8.9712)This returns a value of 8.
Type Casting
someInt = someDouble - 8.2;
someInt = int(someDouble - 8.2);
These are identical statements.These are identical statements.
* *
Type Coercion
The implicit (automatic) conversion of a value from one data type to another.
someDouble = 42; is stored as 42.0
someInt = 11.9; is stored as 11
*
Math Function Example
#include <math.h> // needed for the exp functionint year;int year;long int population;long int population;
year = 1995;year = 1995;
population = 5.5 * population = 5.5 * exp(exp(0.02*(year-1990)0.02*(year-1990)));;cout << "The estimated population for " << yearcout << "The estimated population for " << year
<< " is "<<population << "."; << " is "<<population << ".";
year = 2012;year = 2012;cout << "The estimated population for " << yearcout << "The estimated population for " << year
<< " is "<< population <<"."; << " is "<< population <<".";
Math Function Example
Output:
The estimated population for 1995 is 6.
The estimated population for 2012 is 8.
cin
cin >> my_num;
The keyboard entry is stored into a local
variable called my_num.
Read as: “get from my_num”.
cin(concatenation)
Syntax: cin >> var1 >> var2 >> ...cin >> var1 >> var2 >> ...
cin >> first >> last >> my_num;
*
cin >> qz1 >> qz2 >> qz3 >> qz4;
cin Example 1
int num1, num2, num3;double average;
cout << "Enter three integer numbers: ";cin >> num1 >> num2 >> num3;average = (num1 + num2 + num3) / 3.0;cout << "The average is " << average;cout << '\n';
cin Example 2
double radius, circumference;
double pi = 3.1416;
cout << "Enter the radius of a circle: ";
cin >> radius;
circumference = 2 * pi * radius;
cout << "The circumference of a circle of radius "
<< radius << " is " << circumference <<‘\n’;
cin Example 2
Output:
Enter the radius of a circle: 14
The circum ... circle of radius 14 is 87.9648
cin Example 3
double celsius, faren;
cout <<"Enter the temperature in degrees Fahrenheit: ";cin >> faren;
celsius = 5.0/9.0 * (faren - 32.0);
cout << faren <<" degrees Fahrenheit equals "<< celsius << " degrees celsius.\n";
cin Example 3
Output:
Enter the temperature in degrees Farenheit: 8282 degrees Farenheit equals 27.7777 degrees celsius.
const Qualifier
Syntax: const DataType Name = Literal Value;const DataType Name = Literal Value;
Examples
const double PI = 3.1416;
const double OT_RATE = 1.5;
*
const Qualifier
double radius, area;
const double PI = 3.1416
cout << “Enter the radius: “;
cin >> radius;
cout << “The area is your circle is “
<< PI * radius * radius;
*
Common Programming Errors
not initializing variables before useforgetting >>>> to separate variables in cin
applying ++++ or ---- to an expression
(x-y)++
Why isn’t phonetic spelled as it sounds?
Why do they put Braille dots on the keypad of a drive-up ATM?
How many Microsoft technicians does it How many Microsoft technicians does it take to change a light bulb?take to change a light bulb? Three: two holding the ladder and one
to screw the bulb into a faucet.
“Lack of brains hinders research” The Columbus Dispatch, 16 April 1996