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