56
COMP 111 – PROGRAMMING PRINCIPLES I ASSIGNMENT, FORMATTING, AND INTERACTIVE INPUT Instructor: Dr Dionysiou

COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

COMP 111 – PROGRAMMING PRINCIPLES I

ASSIGNMENT, FORMATTING, AND INTERACTIVE INPUT

Instructor: Dr Dionysiou

Page 2: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

ADMINISTRATIVE

¢ This week’s lecture   [LIANG10] Chapter 2

¢  Assignment operations, formatting output, math functions, input operation, symbolic constants

Copyrigh

t © D

ionysiou

2012

2

Page 3: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

LECTURE OUTLINE

¢ Assignment Operations ¢ Mathematical Library Functions ¢  Input Operation ¢ Symbolic Constants ¢ Formatting Output

3

Copyrigh

t © D

ionysiou

2012

Page 4: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

ASSIGNMENT EXPRESSION

variableName = expression;

Assignment Operator

Lower precedence than any other arithmetic operator

Variable name

Expression Combination of constants, variables, functions that are evaluated to yield a result

4

Copyrigh

t © D

ionysiou

2012

Page 5: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

ASSIGNMENT EXPRESSION (CONT.)

num1 = 45;

num1

1652

num2

2222

A

2260

num1 is assigned the value 45 Steps: 1.  Evaluate the value of the

operand to the right of the assignment operator

2.  Store (assign) that value in the memory location associated with the variable to the left of the assignment operator

5

Copyrigh

t © D

ionysiou

2012

Page 6: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

ASSIGNMENT EXPRESSION (CONT.)

num1 = 45;

45

num1

1652

num2

2222

A

2260

what happens if we now execute the statement num1 = 25;

6

Copyrigh

t © D

ionysiou

2012

Page 7: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

ASSIGNMENT EXPRESSION (CONT.)

num1 = 45; num1 = 25;

25

num1

1652

num2

2222

A

2260

7

Copyrigh

t © D

ionysiou

2012

Page 8: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

ASSIGNMENT EXPRESSION (CONT.)

num1 = 15 + num2 * 2;

11

num1

1652 3

num2

2222

A

2260

Consider the above memory configuration. What happens if we now execute the statement:

8

Copyrigh

t © D

ionysiou

2012

Page 9: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

ASSIGNMENT EXPRESSION (CONT.)

num1 = 15 + num2 * 2;

21

num1

1652 3

num2

2222

A

2260

Consider the above memory configuration. What happens if we now execute the statement:

9

Copyrigh

t © D

ionysiou

2012

Page 10: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

ASSIGNMENT EXPRESSION (CONT.)

num1 = num2 + A;

19

num1

1652 2

num2

2222

A

2260

Consider the above memory configuration. What happens if we now execute the statement:

No initial value! !!

10

Copyrigh

t © D

ionysiou

2012

Page 11: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

ASSIGNMENT EXPRESSION (CONT.)

Draw the memory contents every time they change during the runtime execution of this program.

11

Copyrigh

t © D

ionysiou

2012

Page 12: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

ASSIGNMENT EXPRESSION (CONT.)

Draw the memory contents every time they change during the runtime execution of this program.

12

Copyrigh

t © D

ionysiou

2012

Page 13: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

ASSIGNMENT EXPRESSION (CONT.)

Draw the memory contents every time they change during the runtime execution of this program. What happens when a variable does not have a value?

13

Copyrigh

t © D

ionysiou

2012

Page 14: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

ASSIGNMENT EXPRESSION – ASSIGNMENT OPERATOR

¢ An assignment expression has a value!   Value of the assignment expression is the same as the

contents of the variable on the left

cout << “The value of the expression is “ << (num1 = 5+7);

Output will be The value of the expression is 12

14

Copyrigh

t © D

ionysiou

2012

Page 15: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

ASSIGNMENT EXPRESSION – ASSIGNMENT OPERATOR

¢ Equal sign (=) is the assignment operator   Lower precedence than any other arithmetic operator

¢ Multiple assignments are possible   It has right-to-left associativity

num1 = 45; num2 = 25;

num1 = num2 = 45;

num1 = (num2 = 45) is evaluated in this order: 1.  Assign 45 to num2 2.  Assign the result of the assignment statement

(num2 = 45) to num1 num2 = 45; num1 = num2;

15

Copyrigh

t © D

ionysiou

2012

Page 16: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

ASSIGNMENT EXPRESSION - COERCION

¢ Data type conversions take place across assignment operators

¢ Coercion   Value assigned to variable on left of assignment operator

forced into data type of variable it is assigned to ¢  Integer value assigned to real variable ¢  Real value assigned to integer variable ¢  Pay attention to mixed-mode expressions!!

16

Copyrigh

t © D

ionysiou

2012

Page 17: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

ASSIGNMENT EXPRESSION – COERCION

Explain the output of this program.

17

Copyrigh

t © D

ionysiou

2012

Page 18: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

ASSIGNMENT EXPRESSION – COERCION

What is the output?

18

Copyrigh

t © D

ionysiou

2012

Page 19: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

ASSIGNMENT EXPRESSION – MORE EXPRESSIONS!

num1 = num1 + 5;

10

num1

1652

num2

2222

A

2260

19

Copyrigh

t © D

ionysiou

2012

Page 20: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

ASSIGNMENT EXPRESSION – MORE EXPRESSIONS!

num1 = num1 + 5;

10 15

num1

1652

num2

2222

A

2260

num1 is assigned the value 15 Steps: 1.  Evaluate the value of the operand to

the right of the assignment operator num1 + 5 10 + 5 15

2.  Store (assign) that value in the memory location associated with the variable to the left of the assignment operator Overwrite whatever is in num1 by

15 20

Copyrigh

t © D

ionysiou

2012

Page 21: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

ASSIGNMENT EXPRESSIONS – MORE EXPRESSIONS!

21

Copyrigh

t © D

ionysiou

2012

Page 22: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

ASSIGNMENT EXPRESSIONS – MORE EXPRESSIONS!

¢ Assignment expressions that use the same variable on both sides of the operator may be written using shortcut assignment operators

+= -= *= /= %=

Complete expression Using shortcut assignment operator

sum = sum +5; sum += 5;

sum = sum - 5; sum -= 5;

sum = sum * (5 + num); sum *= 5 + num;

sum = 2 * (sum + num); cannot use it!

22

Copyrigh

t © D

ionysiou

2012

Page 23: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

ASSIGNMENT EXPRESSION – ACCUMULATING

¢  Assignment statement of the form   sum = sum + 10;   sum += 10;

¢  Accumulating subtotals   Useful with repetition statement (chapter 5)

sum = 0; cout << “The value of sum is “ << sum << endl; sum = sum + 10; cout << “The value of sum is “ << sum << endl; sum +=100; cout << “The value of sum is “ << sum << endl;

23

Copyrigh

t © D

ionysiou

2012

Page 24: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

ASSIGNMENT EXPRESSION – COUNTING

¢  Assignment statement of the form   i = i + 1;   count += 2;

¢  The value of the variable is increased by a fixed amount   Also useful with repetition statement (chapter 5)

count = 0; cout << “The value of count is “ << count << endl; count = count + 1; cout << “The value of count is “ << count << endl; count = count + 1; cout << “The value of count is “ << count << endl;

24

Copyrigh

t © D

ionysiou

2012

Page 25: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

ASSIGNMENT EXPRESSIONS – INCREMENT OPERATOR

¢ C++ provides the unary increment operator   Increase the value by 1

variable++; (postfix increment) ++variable; (prefix increment)

Expression Alternative

i= i + 1 i++ or ++i

n= n + 1 n++ or ++n

count= count + 1 count++ or ++count

25

Copyrigh

t © D

ionysiou

2012

Page 26: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

ASSIGNMENT EXPRESSIONS – INCREMENT OPERATOR

¢  Is there a difference between postfix and prefix increment operator?   Yes, when the variable being incremented is used in an

assignment statement

n = n + 1; // increment n first k = ++n; k = n; // assign n’s value to k

k = n; // assign n’s value to k k = n++; n = n + 1; // and then increment n

26

Copyrigh

t © D

ionysiou

2012

Page 27: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

ASSIGNMENT EXPRESSIONS – DECREMENT OPERATOR

¢ C++ provides the unary decrement operator   Decrease the value by 1

variable--; (postfix decrement) --variable; (prefix decrement)

Expression Alternative

i= i - 1 i--or --i

n= n - 1 n-- or --n

count= count - 1 count-- or --count

27

Copyrigh

t © D

ionysiou

2012

Page 28: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

ASSIGNMENT EXPRESSIONS – DECREMENT OPERATOR

¢  Is there a difference between postfix and prefix decrement operator?   Yes, when the variable being decremented is used in an

assignment statement

n = n - 1; // decrement n first k = --n; k = n; // assign n’s value to k

k = n; // assign n’s value to k k = n--; n = n - 1; // and then decrement n

28

Copyrigh

t © D

ionysiou

2012

Page 29: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

IN-CLASS EXERCISE

29

Copyrigh

t © D

ionysiou

2012

Page 30: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

IN-CLASS EXERCISE

30

Copyrigh

t © D

ionysiou

2012

Page 31: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

LECTURE OUTLINE

¢ Assignment Operations ¢ Mathematical Library Functions ¢  Input Operation ¢ Symbolic Constants ¢ Formatting Output

31

Copyrigh

t © D

ionysiou

2012

Page 32: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

USING MATH LIBRARY FUNCTIONS

¢ Advanced mathematical operations provided via standard pre-programmed functions   e.g. trigonometric operations, square root, square

¢ Before using, you need to know:   name of desired mathematical function   what the mathematical function does   type of data required by the function   data type of result returned by the function   how to include the library

¢  #include<cmath>

32

Copyrigh

t © D

ionysiou

2012

Page 33: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

USING MATH LIBRARY FUNCTIONS (CONT.)

functionname (arguments) We want to calculate the square root of integer value 123 1.  call math function that performs the calculation

sqrt(123);

2.  Display the returned value cout << “The square root of 123 is “ << sqrt(123) << endl;

Returned value

33

Copyrigh

t © D

ionysiou

2012

Page 34: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

USING MATH LIBRARY FUNCTIONS (CONT.)

Example Returned value

sqrt(123) 11.0905

sqrt(100 + 23) 11.0905

abs(-3) 3

pow(2.0, 5.) 32

34

Copyrigh

t © D

ionysiou

2012

Page 35: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

USING MATH LIBRARY FUNCTIONS (CONT.)

We want to calculate the expression 4 * sqrt(4.5 * 10.0 – 9.0) – 2.0 STEPS 1. Perform multiplication in the function argument

4 * sqrt(45– 9.0) – 2.0 2. Complete argument calculation

4 * sqrt(36.0) – 2.0 3. Return function value

4 * 6.0 – 2.0 4. Perform multiplication

24.0 – 2.0 5. Perform subtraction

22.0

35

Copyrigh

t © D

ionysiou

2012

Page 36: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

USING MATH LIBRARY FUNCTIONS - CASTING

¢  Implicit casting   Automatically made with

mixed expressions ¢  E.g. assigning a real

number to an int variable ¢  Result is ??

¢ Explicit casting   Forcing the conversion of

a value to another type static_cast <datatype> (expression);

36

Copyrigh

t © D

ionysiou

2012

Page 37: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

LECTURE OUTLINE

¢ Assignment Operations ¢ Mathematical Library Functions ¢  Input Operation ¢ Symbolic Constants ¢ Formatting Output

37

Copyrigh

t © D

ionysiou

2012

Page 38: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

INTERACTIVE INPUT

cin object enter data into a program during runtime cout object display data from a program during runtime

38

Copyrigh

t © D

ionysiou

2012

Page 39: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

INTERACTIVE INPUT – cin

¢ Programs that do same calculation on same set of numbers every time not very useful

¢  cin object   Used to enter data into program while executing

¢  (“get from”) operator, >> ¢  cin object puts computer into temporary pause state until user

types a value

¢ Always use a prompt   String telling person at terminal what should be typed

39

Copyrigh

t © D

ionysiou

2012

Page 40: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

INTERACTIVE INPUT – cin

¢ Any number of values can be input using single cin statement   cin extraction operation able to make some data type

conversions

40

Copyrigh

t © D

ionysiou

2012

Page 41: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

INTERACTIVE INPUT – cin

41

Copyrigh

t © D

ionysiou

2012

Page 42: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

INTERACTIVE INPUT – cin

42

Copyrigh

t © D

ionysiou

2012

Page 43: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

INTERACTIVE INPUT – cin

43

Copyrigh

t © D

ionysiou

2012

Page 44: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

INTERACTIVE INPUT – cin

Debug once ! Run everywhere! Test it on different platforms!

44

Copyrigh

t © D

ionysiou

2012

Page 45: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

LECTURE OUTLINE

¢ Assignment Operations ¢ Mathematical Library Functions ¢  Input Operation ¢ Symbolic Constants ¢ Formatting Output

45

Copyrigh

t © D

ionysiou

2012

Page 46: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

SYMBOLIC CONSTANTS

¢ Literal data   Data explicitly identifying themselves

¢ Magic numbers   Numbers used repeatedly in program

¢  Potential source of error if constant must change

¢ Can give constants symbolic name   Symbolic constants or named constants

¢  const declaration qualifier

  Value change need only be made once

46

Copyrigh

t © D

ionysiou

2012

Page 47: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

SYMBOLIC CONSTANTS (CONT.)

47

Copyrigh

t © D

ionysiou

2012

Page 48: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

PLACEMENT OF STATEMENTS

48

Copyrigh

t © D

ionysiou

2012

Page 49: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

LECTURE OUTLINE

¢ Assignment Operations ¢ Mathematical Library Functions ¢  Input Operation ¢ Symbolic Constants ¢ Formatting Output

49

Copyrigh

t © D

ionysiou

2012

Page 50: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

FORMATTING NUMBERS FOR OUTPUT

¢  Important for program to present results attractively

¢  iomanip header file must be included when manipulator requiring an argument is used

#include<iomanip>

¢ Parameterized manipulator   Manipulator method that uses arguments

cout.setprecision( n ); // Where n can be any number

50

Copyrigh

t © D

ionysiou

2012

Page 51: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

FORMATTING NUMBERS FOR OUTPUT (CONT.)

51

Copyrigh

t © D

ionysiou

2012

Page 52: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

FORMATTING NUMBERS FOR OUTPUT (CONT.)

52

Copyrigh

t © D

ionysiou

2012

Page 53: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

FORMATTING NUMBERS FOR OUTPUT (CONT.)

53

Copyrigh

t © D

ionysiou

2012

Page 54: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

FORMATTING NUMBERS FOR OUTPUT (CONT.)

54

Copyrigh

t © D

ionysiou

2012

Page 55: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

FORMATTING NUMBERS FOR OUTPUT (CONT.)

55

Copyrigh

t © D

ionysiou

2012

Page 56: COMP 111 – PROGRAMMING RINCIPLES ASSIGNMENT … · ASSIGNMENT EXPRESSION – MORE EXPRESSIONS! num1 = num1 + 5; 10 15 num1 1652 num2 2222 A 2260 num1 is assigned the value 15 Steps:

FORMATTING NUMBERS FOR OUTPUT (CONT.)

Must include iomanip

Two decimal digits

Do not use e notation

The width of the next “field”

56

Copyrigh

t © D

ionysiou

2012