Overview Order of presentation different than published handouts Run a program on ccc Finish...
-
date post
20-Dec-2015 -
Category
Documents
-
view
214 -
download
1
Transcript of Overview Order of presentation different than published handouts Run a program on ccc Finish...
Overview
• Order of presentation different than published handouts
• Run a program on ccc• Finish Arithmetic operations• Data types
integercharfloating point
• Write a program
Rules of precedence for Arithmetic Operations
Operator Operation Order of evaluation
( ) Parentheses Evaluated 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, subtraction Evaluated last. If there are several, they are evaluated left to right
Example - precedence for arithmetic operations
algebra: m = a + b + c + d + e -------------------- 5C++: m = (a + b + c + d + e) / 5;
what if parentheses missing? m = a + b + c + d + e -- 5
algebra: m = mx + bC++: m = m * x + b;
Exercises1) 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))));
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 #.
Determining Size and Range// Determining the size and range of the the signed long integer type.
#include <iostream> 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.
Floating point Data Type
Algebraically: number with fractional part3.14
1.2 x 103
1.5 x 10-2
mantissa (1.5) exponent (-2)
mantissa (1.2) exponent (3)
sign bit 8 bits exponent 23 bits mantissa
Implementation: Varies from system to system. Typical:
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
Floating Point Data Types
Type Typical range Typical precision
float 10 e -38 to
10 e 38
6 digits
double 10 e -308 to
10 e 308
15 digits
long double 10 e -4932 to
10 e 4932
19 digits
floating point data type
• implementations differ. Typicaldouble always >= floatlong double always >= double
• Constants3.14 (double)3.14L (long double)3.14f (float)float f1 = 3.14; //compiler error
Mixing float and integer types#include <iostream> using namespace std;
int main(){ float average; int numberOfGrades = 2; int totalOfGrades = 9;
average = totalOfGrades / numberOfGrades;
cout << "The average is " << average << endl; return 0;}
mixing float and integer
• could change totalOfGrades to float to fix. Compiler will promote numberOfGrades
• Could use cast operator to change totalOfGrades temporarilyaverage = (float) totalOfGrades/ numberOfGrades;
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
Exercises
1) create a floating point variable and initialize it to the value 21.6542) create a floating point variable and initialize it to the value 1.6 times 10 to the 12th power3) 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 integervalues. One contains the whole part and one contains the fractional partie. 5.77 -> 5, 77. How would you do it?
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);
char data type
• ASCII set see book 623A 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
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?
Data Types ExercisesGiven 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;
Operations
• Arithmetic
• Relational (a < b)
• Logical (a && b)
Relational operators• Used to compare values• result of relational expression is true or false
a = 1, b = 2a == b falsea < b truea > b false
• Used to change execution order of statements
Relational OperatorsStandard algebraic expression
C++ operator Example of C++ expression
Meaning
> > a > b a is greater than b
< < a < b a is less than b
>= a >=b a is greater than or equal to b
<= a <= b a is less than or equal to b
= == a == b a is equal to b
!= a != b a is not equal to b
Slide relational operators (cont)• precedence < <= >= > higher than == and != a <= b == x > y is the same as (a <= b) == (x > y) • arithmetic operators higher precedence than
relationalif x = 2, y = 3y == 2 * x + 3 evaluated as follows:2 * x (4)4 + 3 (7)3 == 7? (false)
Common programming errors
• Compiler will generate an error if the operators ==, !=, >=, or <= have whitespace
• Cannot reverse => not same as >=
• using = instead of == common mistakem== 4 desired.
m = 4 done.
do 4 == m
relational operators exercises
Identify which of the following expressions are true:Assume a = 0, b = 1, x = 2, y = 31) a == b + x 2) b + x == y3) x - b != y4) a * b + x * y != x * y5) a <= b != x <= y
Logical Operators
Operator Name Example True if
! Not !x x is false
&& And x && y x and y are both true
|| Or x || y x or y is true
Logical Operators
• Precedence !&&|| !a && b same as (!a) && b
• Relational operators have higher precedence than the && and || operators
• a < b && c > d same as (a < b) && (c > d)
Operator Type
() parentheses
! not
* / % multiplication, division, modulus
+ - addition, subtraction
< > <= >= relationals
== != equality
&& and
|| or
Precedence chart
Precedence ExercisesExercises1) Assume a = 1, b = 2, c = 4, d =3 is following expression true or false? a < b && c >d 2) Create C++ expression a < b and > c, or x > y and > z