Download presentation
Presentation is loading. Please wait.
1
Overview Order of presentation different than published handouts Run a program on ccc Finish Arithmetic operations Data types integer char floating point Write a program
2
Rules of precedence for Arithmetic Operations OperatorOperationOrder of evaluation ( )ParenthesesEvaluated first. If the parentheses are nested the innermost pair is evaluated first. If there are several pairs on the same level they are evaluated left to right. * / %Division, mulitiplication, modulus Evaluated second. If there are several they are evaluated left to right. + -Addtition, subtractionEvaluated last. If there are several, they are evaluated left to right
3
Example - precedence for arithmetic operations algebra: m = a + b + c + d + e -------------------- 5 C++: m = (a + b + c + d + e) / 5; what if parentheses missing? m = a + b + c + d + e -- 5 algebra: m = mx + b C++: m = m * x + b;
4
Exercises 1) int a, x, b, c, y; a = 2; x = 5; b = 3; c = 7; y = a * x * x + b * x + c; What is y after this program executes? 2) Evaluate the following c++ expressions: x = 7 + 3 * 6 / 2 - 1; y = 2 % 2 + 2 * 2 - 2/2; z = (3 * 9 * (3 + (9 * 3/ (3))));
5
Integer Data types Size and range differ from system to system. Typical: short (2 bytes) unsigned short 0 to 65535 signed short -32768 to 32765 int (4 bytes) unsigned int 0 to 4294967295 signed int -2147483648 to 2147483647 long (4 bytes) unsigned long 0 to 4294967295 signed long -2147483648 to 2147483647 int is shorthand for signed int same operations valid for all why so many? conserve memory. Database of all US citizens, age, social security #.
6
Determining Size and Range // Determining the size and range of the the signed long integer type. #include using namespace std; int main() { cout << "signed long type is " << sizeof (signed long) << "bytes\n"; cout << "largest value of signed long type is " << LONG_MAX << endl; cout << "smallest value of signed long type is " << LONG_MIN << endl; return 0; } page 45 of book gives other constants. Find out size and range for WPI.
7
Floating point Data Type Algebraically: number with fractional part 3.14 1.2 x 10 3 1.5 x 10 -2 mantissa (1.5) exponent (-2) mantissa (1.2) exponent (3) sign bit 8 bits exponent23 bits mantissa Implementation: Varies from system to system. Typical:
8
floating point data type C++: Use when: fractions too big for integer (1 x 10 to the 100th) Precision - number of places to right of decimal point. Limited by size of mantissa
9
Floating Point Data Types TypeTypical rangeTypical precision float10 e -38 to 10 e 38 6 digits double10 e -308 to 10 e 308 15 digits long double10 e -4932 to 10 e 4932 19 digits
10
floating point data type implementations differ. Typical double always >= float long double always >= double Constants 3.14 (double) 3.14L (long double) 3.14f (float) float f1 = 3.14; //compiler error
11
Mixing float and integer types #include using namespace std; int main() { float average; int numberOfGrades = 2; int totalOfGrades = 9; average = totalOfGrades / numberOfGrades; cout << "The average is " << average << endl; return 0; }
12
mixing float and integer could change totalOfGrades to float to fix. Compiler will promote numberOfGrades Could use cast operator to change totalOfGrades temporarily average = (float) totalOfGrades/ numberOfGrades;
13
Things to remember about floating point data types Mantissa is limited in size so value may not be exact (1/3 =.3333333333...) Arithmetic is faster using integer types than floating point types Don’t use equality operators Modulus operator not valid for floating point Don’t use floating point #’s for loop control
14
Exercises 1) create a floating point variable and initialize it to the value 21.654 2) create a floating point variable and initialize it to the value 1.6 times 10 to the 12th power 3) Why would the compiler complain about this statement? float f = 32.56; 4) Suppose you have a floating point number and you want to separate the whole part from the fractional part so that you have two integer values. One contains the whole part and one contains the fractional part ie. 5.77 -> 5, 77. How would you do it?
15
Homework Use cin to read in a value double total; cin >> total; To print floating point using cout cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint); cout << setprecision (2);
16
char data type ASCII set see book 623 A is 65 B is 66 regularity. A + 1 = B A < B is true initialize with ‘ char ch1 = ‘A’; // stores whole number 65 in storage cell named ch1 char ch1; ch1 one byte
17
char data type routines treat char differently than int char ch1 = ‘A’; int int1 = 65; cout << “ch1 is << ch1 << endl; //displays “ch1 is A” cout << “int1 is “ << int1 << endl; //displays “ch1 is 65” char is shorthand for either unsigned char (0 to 255) or signed char (-127 to 127) varies from system. What is it on ours?
18
Data Types Exercises Given the following declarations, determine the value of the variable on the left-hand-side of each assignment statement. int int1, int2, int3; float f1=1.0f, f2=2.5f, f3=5.0f; double d1, d2; char ch1, ch2; int1 = 5; d2 = 5.0; ch1 = '5'; int3 = f2; int2 = int1 / int3; d1 = f3 - -f1 * 6 / int3 + 8.0 * (int1 - d2); ch2 = ch1 - 2; int3 = 'a' - 'A'; ch1 = 'W' + int3;
19
Operations Arithmetic Relational (a < b) Logical (a && b)
20
Relational operators Used to compare values result of relational expression is true or false a = 1, b = 2 a == b false a < b true a > b false Used to change execution order of statements
21
Relational Operators Standard algebraic expression C++ operatorExample of C++ expression Meaning >>a > ba is greater than b <<a < ba is less than b >=a >=ba is greater than or equal to b <=a <= ba is less than or equal to b ===a == ba is equal to b !=a != ba is not equal to b
22
Slide relational operators (cont) precedence = > higher than == and != a y is the same as (a y) arithmetic operators higher precedence than relational if x = 2, y = 3 y == 2 * x + 3 evaluated as follows: 2 * x (4) 4 + 3 (7) 3 == 7? (false)
23
Common programming errors Compiler will generate an error if the operators ==, !=, >=, or <= have whitespace Cannot reverse => not same as >= using = instead of == common mistake m== 4 desired. m = 4 done. do 4 == m
24
relational operators exercises Identify which of the following expressions are true: Assume a = 0, b = 1, x = 2, y = 3 1) a == b + x 2) b + x == y 3) x - b != y 4) a * b + x * y != x * y 5) a <= b != x <= y
25
Logical Operators OperatorNameExampleTrue if !Not!xx is false &&Andx && yx and y are both true ||Orx || yx or y is true
26
Logical Operators Precedence ! && || !a && b same as (!a) && b Relational operators have higher precedence than the && and || operators a d same as (a d)
27
OperatorType ()parentheses !not * / %multiplication, division, modulus + -addition, subtraction =relationals == !=equality &&and ||or Precedence chart
28
Precedence Exercises Exercises 1) Assume a = 1, b = 2, c = 4, d =3 is following expression true or false? a d 2) Create C++ expression a c, or x > y and > z
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.