Download presentation
Presentation is loading. Please wait.
1
CSE 100 <math.h> Input: cin type casting
2
Math Library Functions
math.h sqrt(n) fabs(n) cos(n) log10(n) log(n) pow(b, n) etc. * * *
3
Math Library Functions
name of the function what it does data type of argument data type of returned value
4
Math Library Functions
Ex. sqrt(49) pow(2.1, 3) abs(-34.5) cos(30) abs(34.5) Syntax: function_name (argument); *
5
Math Library Functions
sqrt( pow ( fabs (-4), 3) ) = sqrt( pow ( , 3) ) = sqrt( 64.0 ) = 8.0 nested functions * * *
6
Type Casting The explicit conversion of a value from one data type to another. Syntax: data_type (expression) int (5.34 * 1.68) int (8.9712) This returns a value of 8. * *
7
Type Casting someInt = someDouble - 8.2;
someInt = int(someDouble - 8.2); These are identical statements. * *
8
Type Coercion The implicit (automatic) conversion of a value from one data type to another. someDouble = 42; is stored as someInt = 11.9; is stored as 11 *
9
Math Function Example #include <math.h> // needed for the exp function int year; long int population; year = 1995; population = 5.5 * exp(0.02*(year-1990)); cout << "The estimated population for " << year << " is "<<population << "."; year = 2012; cout << "The estimated population for " << year << " is "<< population <<".";
10
Math Function Example 5.5 e .02(year-1900) 5.5 * exp(0.02*(year-1990))
11
Math Function Example Output: The estimated population for 1995 is 6.
12
cin cin >> my_num; The keyboard entry is stored into a local
variable called my_num. Read as: “get from my_num”.
13
cin Syntax: cin >> var1;
Examples: cin >> last_name; cin >> ch; cin >> my_id#; *
14
Syntax: cin >> var1 >> var2 >> ...
cin (concatenation) Syntax: cin >> var1 >> var2 >> ... cin >> first >> last >> my_num; cin >> qz1 >> qz2 >> qz3 >> qz4; *
15
cin & cout main() { cout << cin >> }
standard device standard device (keyboard) (screen)
16
cin & cout cout cin << >> insertion extraction
<< >> insertion extraction “put to” “get from” whitespace characters ignored
17
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';
18
cin Example 1 Output: The average is
19
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’;
20
cin Example 2 Output: Enter the radius of a circle: 14
The circum ... circle of radius 14 is
21
cin Example 3 double celsius, faren;
cout <<"Enter the temperature in degrees Fahrenheit: "; cin >> faren; celsius = 5.0/9.0 * (faren ); cout << faren <<" degrees Fahrenheit equals "<< celsius << " degrees celsius.\n";
22
cin Example 3 Output: Enter the temperature in degrees Farenheit: 82
82 degrees Farenheit equals degrees celsius.
23
const Qualifier Syntax: const DataType Name = Literal Value;
Examples const double PI = ; const double OT_RATE = 1.5; *
24
const Qualifier double radius, area; const double PI = cout << “Enter the radius: “; cin >> radius; cout << “The area is your circle is “ << PI * radius * radius; *
25
Common Programming Errors
not initializing variables before use forgetting >> to separate variables in cin applying ++ or -- to an expression (x-y)++
26
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 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.