summary of what we learned yesterday basics of c++ format of a program syntax of literals, keywords,...

16
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic operations Assignment operator Today we will look at: Standard input (cin) and output (cout) More data types More complex arithmetic operations More on assignment operator

Upload: erick-wilson

Post on 04-Jan-2016

223 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic

Summary of what we learned yesterdayBasics of C++

Format of a programSyntax of literals, keywords, symbols, variablesSimple data types and arithmetic operations Assignment operator

Today we will look at:Standard input (cin) and output (cout)More data typesMore complex arithmetic operationsMore on assignment operator

Page 2: Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic

Announcements Each section’s capacity is increased by 3 or 4, B2 is closed. HW1 will be assigned this week at SUCourse

Due February 18 Wednesday at 19:00 For homework assignments and submissions, we are using SUCourse Homework will be explained in recitations Moreover, there will be important explanations about the homework

submission procedure in recitations Submission is a tricky process, please do attend the recitations in order not to

make a mistake No late homework without penalty

One late day is allowed at cost of 10% of full grade Plagiarism is not tolerated

Homeworks are to be done personally we use software tools to detect plagiarized homework first case –100 (minus hundred), second fails the course! detailed policy is on the web site of the course

Two Midterm Exams + Final Exam Midterm 1: March 16th Monday 19:40 – 21:30 Midterm 2: April 20th Monday 19:40 – 21:30

Page 3: Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic

Data Typesstring

used it in previous lecturesmore technically, string is a class

charfor single character

digits, letters, symbolsuses up one byte

range 0 ... 255 why? one byte (8 bits) can store 28 = 256 different values.

stores the code of the character e.g. 65 for ‘A’ character arithmetic is possible (will see later)

char literals are in single quotes (strings are in double quotes ")'z' 'T' '4' '&'

bool boolean (will see later)

Page 4: Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic

Numeric Typesto represent integer and real numbersint

integer numbersno infinity in computers limited range

2 or 4 bytes (16 or 32 bits) depending on the computer and compiler you use

in our case: 4 bytes (32 bits)integer range:

– 32,768 ... 32,767 for 16-bit computers why?

–2,147,483,648 ... 2,147,483,647 for 32-bit computers why?

Page 5: Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic

Numeric Types (cont’d)short int (or just short )

always 16-bit integerlong int (or just long )

always 32-bit integersigned versus unsigned integers

you can put signed or unsigned keywords before the type

we discussed signed integerssigned is default

unsigned integers use the same amount of bits but ranges are different16-bit: 0 ... 65,535 (216 –1)32-bit: 0 ... 4,294,967,295 (232 –1)

Page 6: Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic

Numeric Types (cont’d)Real numbers in C++Real literals

3.14159 -2.5 5.43e21Real data types (their difference is in precision)

float consumes 4 bytes Range: 0 U [-1.175494351e–38 ... -3.402823466e+38] U [1.175494351e–38 ... 3.402823466e+38] Tapestry does like float

double consumes 8 bytes Range: 0 U [-2.2250738585072014e–308 ... -1.7976931348623158e+308] U [2.2250738585072014e–308 ... 1.7976931348623158e+308]

Standard but a bit complex representation see “floating point representation” item in MSDN Library index:

http://msdn.microsoft.com/en-us/library/0b34tf65.aspx

Page 7: Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic

More on C++ types Check out these items in MSDN Library Index

Fundamental types more on C++ types

LIMITS.H header file (Microsoft Visual Studio\VC98\Include)limits of integer and char typesMSDN help on Integer Limits

floating limits (FLOAT.H)limits for the floating points numbers (float, double)MSDN help on Floating Limits

floating point representationif you are interested in to learn how the real numbers are

represented in computersMSDN help

Page 8: Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic

Arithmetic OperationsOperators: + - * / % Operands: values that operator combines

variables or literalsCombination of operators and operands is called

expressionSyntax and semantics for arithmetic operations:

AdditionSubtraction

Multiplication Division Modulus

23 + 4 23 * 4 21 / 4 is 5 21 / 4 is 5

x + y x * 3.0 21 / 4.0 is 5.25

18 % 2 is 0

d – 14.0 + 23 d * 23.1 * 4 x / 4 x % 4

5 - 3 + 2 5 – 3 * 2 x / y x % y

Page 9: Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic

Arithmetic Operations (cont’d)Mixed type expressions

what if one operator is int other is double?integer is converted into double before operation

5.0 * 8 is 40.0

5 / 10 is 0 (integer division)

5.0 / 10 is 0.5 (real division)

10 – 8 is 2 (integer)

10 – 8.0 is 2.0 (double)

Page 10: Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic

Expressions with several operatorsYou can use parentheses in expressions to group them

Open ( and close ) parentheses should matchRule 1: Parenthesized sub-expressions are evaluated first

inside to outRule 2: Within an expression/subexpression if there are

several operators, use operator precedence, evaluate * / % before + -

Rule 3: If the operators are in the same expression/subexpression and at the same precedence level, then associativity rule appliesevaluate operators from left-to-right

Examples(5 - 3 * (7 - 3)) * 8 is -5610 / 2 * 6 / 2 + (5 - 3 * (2 - 1)) is 17

Page 11: Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic

Expressions with several operatorsAre the following expressions equivalent?

(40 – 32) * 5 / 9(40 – 32) * (5 / 9)

NO, first one is 4, second one is 0 What about this?

(40 – 32.0) * 5 / 9Operations are double operations. Result is 4.44444

Are these operators sufficient?how to calculate square root?

Later we’ll study functions like sqrt, cos, sin, pow, …For complicated mathematical operations that you cannot

easily do using basic operatorsAccessible using #include <cmath> (or <math.h>)

Page 12: Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic

Integer vs. Real

Real values can be assigned to Integer variables, but this is not recommended since we loose precision

int a;double r;r = 125.879;a = r;

What is the value of a? 125Real value is truncated before storing in an integer variableAvoid doing thisVS 2010 Compiler warns you but does not give errorBe careful when passing arguments to parameters as well

passing an integer argument to a double parameter causes the same precision problem

Page 13: Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic

Integer vs. Real (More on Precision) What is the difference between red and blue parts in the

following program (fahrcels.cpp)? red: integer arithmetic (low precision) blue: real arithmetic (high precision)

int main(){ int ifahr; double dfahr; cout << "enter a Fahrenheit temperature "; cin >> ifahr; cout << ifahr << “ F = “ << (ifahr - 32) * 5/9 << " Celsius" << endl;

cout << "enter another temperature "; cin >> dfahr; cout << dfahr << “ F = “ << (dfahr - 32) * 5/9 << " Celsius" << endl;

return 0;}

See Figure 3.4 in the textbook.

Page 14: Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic

Overflow See daysecs.cppOverflow occurs when the result of an integer

expression is outside the limits Run the program with

365 days result: 31536000 seconds correct result and output

14500 days result: 1252800000 seconds correct result and output

25129 days result: -2123821696 seconds incorrect result and output

due to overflow65400 days

result: 1355592704 seconds incorrect result and output due to overflow

Page 15: Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic

More on Assignment operator (4.1 & 4.3.4) Assignment operator is =

to store values in variablesvariable = expression;

first the right hand side expression is evaluated using the current values then the evaluated expression is stored in variable

Types should be compatible, otherwise a syntax error may occur (e.g. string variable, integer expression), or precision is lost (e.g. integer variable, real expression)

Example: what is the value of a after the assignment?int a, b;b = 25; a = 8;a = b – 3 * a + 2;

Answer: 3 A rule: Right hand side expression is evaluated before the

assignment operator is executed. If you use the left hand side variable in the right hand side expression as

well, then the current value is used in the expression. See Program 4.1 in textbook.

Page 16: Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic

More on Assignment operator (4.1 & 4.3.4)

Assigning single expression to several variablesvariable1 = variable2 = variable3 = ... variablen = expression;all variables are assigned the same value of expressionexample:

int x, y, z;x = y = z = 5;

x, y and z contain 5

Arithmetic assignment operators+= -= *= /= %=

Combines arithmetic operation and assignment in one operatorvariable += expression is the same as

variable = variable + expressionExample: x += 1 is the same as x = x + 1Same for -= *= /= and %=

x -= 1 x *= 3 x /= 2 and x %= 2