1 midterm review comp 102. tips l eat a light meal before the exam l no electronic devices...

35
1 Midterm Review COMP 102

Post on 21-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

1

Midterm Review

COMP 102

Page 2: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

Tips Eat a light meal before the exam NO electronic devices (including

calculators, dictionaries, phones, pagers, etc.)

Exam time 6:30 -8:30pm. But come a little early to find your seat

assignment!

Page 3: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

3

COMP102 Prog Fundamentals I: Midterm Review /Slide 3

Seating Plan

Lecture Theater A: L2, L4 Lecture Theater B: L1, Lecture Theater C: Lab3a, Lab3b Lecture Theater E: Lab3c, Lab3d

Page 4: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

Know

INTRODUCTION TO COMPUTER SYSTEMS

DATA TYPES, VARIABLES AND CONSTANTS Data types Identifiers, variables and constants Type casting

Page 5: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

Know

STRUCTURE OF C++ PROGRAMS Compiler directive #include I/O operators cin & cout Assignment statements Expressions, operators and operator precedence

SELECTION STRUCTURES Relational and logical operators Logical expressions If, if-else, if-else-if, and nested if statements Dangling else switch statement

Page 6: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

Know LOOPS

for loops while loops do-while loops Increment and decrement operations Nested loops

PROGRAM DESIGN AND FUNCTIONS Top-down design and stepwise refinement Function prototypes and function definitions Parameter Passing: pass-by-value, pass-by-

reference

Page 7: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

Watch Out For

Programming style problems indentation

Nested Loops [EX 1]for(a = 1; a < 10; a++){

for(a =1; a < 10; a++)

cout << a; // what happens here?

}

Page 8: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

8

COMP102 Prog Fundamentals I: Midterm Review /Slide 8

C++ Data Type

A type defines a set of values and a set of operations that can be applied on those values. The set of values for each type is known as the domain for the type.

C++ contains 5 standard types:

vo id in t ch ar floa t b oo l

S tan d ardD ata Typ es

Page 9: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

9

COMP102 Prog Fundamentals I: Midterm Review /Slide 9

Identifiers appear in black in Visual C++. An identifier is a name for a variable, constant, function, etc. It consists of a letter followed by any sequence of letters, digits, and underscores.

Examples of valid identifiers: First_name, age, y2000, y2k Examples of invalid identifiers: 2000y Identifiers cannot have special characters in them. For example: X=Y, J-20,

~Ricky,*Michael are invalid identifiers. Identifiers are case-sensitive. For example: Hello, hello, WHOAMI,

WhoAmI, whoami are unique identifiers.

C++ identifiers

Page 10: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

10

COMP102 Prog Fundamentals I: Midterm Review /Slide 10

Constant declarations Constants are used to store values that never change

during the program execution. Using constants makes programs more readable and

maintainable. Syntax: const <type> <identifier> = <expression>;

Examples: const double US2HK = 7.8;

//Exchange rate of US$ to HK$ const double HK2TW = 4.11;

//Exchange rate of HK$ to TW$ const double US2TW = US2HK * HK2TW;

//Exchange rate of US$ to TW$

Page 11: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

11

COMP102 Prog Fundamentals I: Midterm Review /Slide 11

Variables are used to store values that can be changed during the program execution.

A variable is best thought of as a container for a value.

3445 y -3.14

Syntax: < type > < identifier >;

< type > < identifier > = < expression >;

Examples: int sum;

int total = 3445;

char answer = 'y';

double temperature = -3.14;

Variable declarations

Page 12: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

12

COMP102 Prog Fundamentals I: Midterm Review /Slide 12

Type Conversion

You can change the type of an expression with a cast operation.

Syntax:(type) expression

(type) variable

Example [EX 2]:(char) 65 // returns ‘A’

(char) 7 // returns bell

(int) ‘a’ // returns 97

(int) 10.5 // returns 10

Page 13: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

13

COMP102 Prog Fundamentals I: Midterm Review /Slide 13

General form of a C++ program

// Program description#include directivesint main(){ constant declarations variable declarations executable statements return 0;}

Page 14: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

14

COMP102 Prog Fundamentals I: Midterm Review /Slide 14

Assignment Statement

The value of a variable can be changed through an assignment statement.

The syntax of the assignment statement is:

< variable > = < expression >; An expression is a sequence of operands and

operators. The expression to the right of the equal sign is

evaluated and its value becomes stored in the variable on the left hand side.

Page 15: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

15

COMP102 Prog Fundamentals I: Midterm Review /Slide 15

Operators and Precedence Which of the following is equivalent to mx + b ?

(m * x) + b m * (x + b)

Operator precedence tells the order in which different operators in

an expression are evaluated.

Standard precedence order ( ) Evaluated first, if nested then evaluate

the innermost first. * / % Evaluated second. If there are several,

then evaluate from left-to-right. + - Evaluate third. If there are several,

then evaluate from left-to-right.

Page 16: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

16

COMP102 Prog Fundamentals I: Midterm Review /Slide 16

Standard Input/Output

Page 17: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

17

COMP102 Prog Fundamentals I: Midterm Review /Slide 17

Relational Operators

Relational operators are used to compare two values to form a condition.

Math C++ Plain English

= == equals [example: if(a==b) ]

[ (a=b) means put the value of b into a ]

< < less than

<= less than or equal to

> > greater than

>= greater than or equal to

!= not equal to

Page 18: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

18

COMP102 Prog Fundamentals I: Midterm Review /Slide 18

Operator Precedence

Which comes first?

* / %

+ -

< <= >= >

== !=

=

Answer:

Page 19: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

19

COMP102 Prog Fundamentals I: Midterm Review /Slide 19

The Boolean Type

C++ contains a type named bool for conditions. A condition can have one of two values:

true (corresponds to a non-zero value) false (corresponds to zero value)

Logical operators can be used to form more complex conditional expressions. The and operator is && The or operator is || The not operator is !

Page 20: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

20

COMP102 Prog Fundamentals I: Midterm Review /Slide 20

Operator Precedence

Which comes first?

!* / %+ -

< <= >= >== !=

&&||=

Answer:

Page 21: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

21

COMP102 Prog Fundamentals I: Midterm Review /Slide 21

The if-else Statement Syntax

if (condition) Action_Aelse Action_B

if the condition is true then execute Action_A elseexecute Action_B

Example:if(v == 0)

cout << "v is 0";else

cout << "v is not 0";

condition

Action_A Action_B

true false

Page 22: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

22

COMP102 Prog Fundamentals I: Midterm Review /Slide 22

“Dangling Else” Problem [EX 3]

Use extra brackets { } to clarify the intended meaning, even if not necessary.

int test_number=-1, test_number_2=1;int result = 0;

if(test_number > 0){ if(test_number_2 > 0) result = 1;

else //dangling else result = 2;

}

cout << "Result = " << result << endl;

Page 23: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

23

COMP102 Prog Fundamentals I: Midterm Review /Slide 23

A Loop Statement Syntax

while (condition) action

How it works: if condition is true then

execute action repeat this process until

condition evaluates to false action is either a single

statement or a group of statements within braces.

condition

action

true

false

Page 24: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

24

COMP102 Prog Fundamentals I: Midterm Review /Slide 24

Another Loop Statement Syntax

for (initialization; condition; update)

action How it works:

execute initialization statement while condition is true

–execute action–execute update

condition

action

true

false

initialization

update

Page 25: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

25

COMP102 Prog Fundamentals I: Midterm Review /Slide 25

Yet Another Loop Statement Syntax

do action while (condition)

How it works: execute action if condition is true then execute

action again repeat this process until

condition evaluates to false. action is either a single statement

or a group of statements within braces.

action

true

false

condition

Page 26: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

26

COMP102 Prog Fundamentals I: Midterm Review /Slide 26

Postfix expressions

Two postfix operators: ++ (postfix increment)

-- (postfix decrement)

the effect of a++ is the same as a = a + 1the effect of a-- is the same as a = a - 1

Page 27: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

27

COMP102 Prog Fundamentals I: Midterm Review /Slide 27

Prefix expressions

Two prefix operators: ++ (prefix increment)

-- (prefix decrement)

the effect of ++a is the same as a = a + 1the effect of --a is the same as a = a - 1

Page 28: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

28

COMP102 Prog Fundamentals I: Midterm Review /Slide 28

Top-Down Design

A program is divided into a main module and its related modules. Each module is in turn divided into submodules until the resulting modules are intrinsic; that is, until they are implicitly understood without further division.

Page 29: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

29

COMP102 Prog Fundamentals I: Midterm Review /Slide 29

User-Defined Functions C++ programs usually have the following form:

// include statements // function prototypes // main() function // function definitions

Page 30: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

30

COMP102 Prog Fundamentals I: Midterm Review /Slide 30

Function DefinitionA function definition has the following syntax:

<type> <function name>(<formal_parameter_list>){

<local declarations> <sequence of statements>

}For example: Definition of a function that computes the absolute

value of an integer:

int absolute(int x){ if (x >= 0) return x; else return -x; }

Page 31: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

31

COMP102 Prog Fundamentals I: Midterm Review /Slide 31

Function Prototype The function prototype declares the input and

output parameters of the function.

The function prototype has the following syntax:

<type> <function name>(<type list>);

Example: A function that returns the absolute value of an integer is: int absolute(int);

Page 32: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

32

COMP102 Prog Fundamentals I: Midterm Review /Slide 32

Parameter Passing

There are two ways to pass parameters into a function: Pass by value - The arguments of a function

retain their original values after the function’s execution. (int variable_name)

Pass by reference - The address of a variable rather than its value is sent to the called function. (int& variable_name)

Page 33: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

COMP102 Prog Fundamentals I: Summary of Loops

COMP102 Prog Fundamentals I: Midterm Review /Slide 33

Multiple Selection: The switch Statement

Syntax:switch (<selector expression>) { case <label1> : <sequence of statements>; break; case <label2> : <sequence of statements>; break; case <labeln> : <sequence of statements>; break; default : <sequence of statements>; }

Page 34: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

34

COMP102 Prog Fundamentals I: Midterm Review /Slide 34

Relational Operators and the Type char

'0' through '9' have ASCII code values 48 through 57

'0' < '1' < . . . < '9'

'A' through 'Z' have ASCII code values 65 through 90

'A' < 'B' < . . .< 'Z'

'a' through 'z' have ASCII code values 97 through 122

'a' < 'b' < . . .< 'z'

Page 35: 1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)

35

COMP102 Prog Fundamentals I: Midterm Review /Slide 35

Scope The scope of an identifier does not apply if the same

identifier is declared in an inner block. A global declaration of an identifier is made outside the

bodies of all functions, including the main function. It is normally grouped with the other global declarations and placed at the beginning of the program file.

A local declaration of an identifier is made inside a block of code which could be the body of a function.

Globally declared identifiers can be accessed anywhere in the program.

Locally declared identifiers cannot be accessed outside of the block they were declared in.