review for exam2 key ideas 1. key ideas: boolean operators (2 > 3) || (3 < 29.3) a.true...

23
Review for Exam2 Key Ideas 1

Upload: janis-robbins

Post on 03-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True

Review for Exam2

Key Ideas

1

Page 2: Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True

Key Ideas: Boolean Operators

(2 > 3) || (3 < 29.3)

A. True B. False C. Impossible to determine

(22 > 3) && (3 > 29.3)

D. True E. FalseF. Impossible to determine

(22 > x) && (x > 29.3)

A. TrueB. False C. Impossible to determine

(x < 2) || (x >= 2)

D. True E. False F. Impossible to determine

2

• What is the result of the following statement?

Page 3: Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True

3

Key Ideas: if statements

• Decision (Conditional) Structure– To execute some part of code under certain circumstances only– To skip some part of a code

if <condition 1><statements 1>

elseif <condition 2><statements 2>

…else

<statements n>end

Page 4: Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True

4

Key Ideas: switch Statements• Decision (Conditional) Structure

– To compare a single variable with multiple cases (equality)– To execute some part of code under the first matching case– To skip rest of a code

switch variablecase specification 1

<code block 1>...case specification n

<code block n>otherwise

<default block>end

Page 5: Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True

5

Key Ideas: Loops

• Matlab has two loops, one for each method explained previously:

while– A CONDITIONAL loop. Generic, all-purpose. Best used when the

programmer does not know how many times the block of code needs to repeat.

– Repeats while CONDITION is true.

for– A COUNTING loop. Best used when the programmer “knows” how

many times the block of code needs to repeat.

Page 6: Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True

6

Key Ideas: while Loop

• A while loop repeats a block of code while a condition is true.

• Loop variable(s)• Structure: Initialization, while condition, code to be repeated, update of

condition, end

• Common Uses:– trap the user until a valid input is given (relational operators, even,

odd, integers, etc.)– running total until a condition is met

• Infinite loop occurs when the loop condition is never updated to a false condition while in the loop– To stop an infinite loop: Ctrl-C

Page 7: Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True

7

Key ideas: for Loop

• The for loop is a counting loop

• The while loop can always be used BUT…the for loop is meant to count– The startvalue:step:endValue contains all necessary values

for the loop to work.

• Nested for loops are absolutely possible! Actually, any construct (if, switch, while, for) can now be nested within each other.

Page 8: Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True

Review for Exam2

Questions

8

Page 9: Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True

Review Questions

Loops:

Match the following

a. for 1. does not exist in Matlabb. while 2. number of iterations is knownc. do/while 3. unknown number of

iterations

9

Page 10: Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True

Review Questions

After prompting the user how many values should be entered, what loop is most appropriate to prompt for each grade separately?

Nested within the 1st loop, which loop can be used to trap the user until s/he enters a valid value?

10

a. forb. whilec. switchd. if

a. forb. whilec. switchd. if

Page 11: Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True

Review Questions

Loops:

X = -1;while X <=0Y = X + 1;

end

The code above will result in an infinite loop

11

a. Trueb. False

Page 12: Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True

Review Questions

Loops:

X = -1;while X <=0x = X + 1;

end

The code above will result in an infinite loop

12

a. Trueb. False

Page 13: Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True

Review Questions

Loops:

X = -1;while X <=0X = X + 1;

end

The code above will result in an infinite loop

13

a. Trueb. False

Page 14: Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True

Review Questions

X1 = 4;X2 = 7;while ____(x1-x2)>0.0002% code..

end

In numerical methods, what function is commonly used to check if two values are “close enough”?

14

Page 15: Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True

Review Questions

Using the following code:for r = 1:3for c = 1:4

x = input(‘An integer: ‘);end

end

How many times will the input() command execute?

In general, what is the result?

15

Page 16: Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True

Review Questions

Using the following code:

for position = 1:2:14disp(‘*’)

end

How many stars will print to the screen?

16

Page 17: Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True

Review Questions

Using the following code, fill in the blanks to answer.

result = 0;for x = 3:5for y = 1:2

for z = 10:10:20result = result + x*y*z;

endend

end

result = 0 + 3*1*10+ ______ + _____ + ______ + ______ + _____ + ____ + _____ .

17

Page 18: Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True

Review Questions

How many stars will print to the screen? _____

for position = 14:2:1disp(‘*’)

end

18

Page 19: Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True

Review Questions

Library functions:

What is the range of the following equations:

a. floor(rand*12+3)

b. ceil(rand*5+1)

c. rand*10+5

19

Page 20: Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True

Review Questions

Modulus:

The following code

Is true only if x is

20

a. odd b. evenc. fraction

𝑚𝑜𝑑 (𝑥 ,2 )=¿1

Page 21: Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True

21

Example Problem• Below are two formulas for calculating π:

• Write a MATLAB program that – 1) Calculate the value of pi using the first formula and let the term to

add up until the error is within 0.000001. Keep track the number of terms .

– 2) Calculate the value of pi using the second formula with the same number of terms.

– 3) Output pi values from step 1 and 2 (8 decimal places), as well as the error in the step 2 as a percentage (4 decimal places).

Page 22: Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True

22

Example Problem, cont’d

• Write a MATLAB program that – 1) Calculate the value of pi using the first formula and let the term to

add up until the error is within 0.000001. Keep track the number of terms .

– 2) Calculate the value of pi using the second formula with the same number of terms.

– 3) Output pi values from step 1 and 2 (8 decimal places), as well as the error in the step 2 as a percentage (4 decimal places).

Page 23: Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True

23

STUDY HARD!

• Practice tonight. • Take the notes you just took, and code the example!

• The more practice, the easier it gets!