topic review for exam 1 0.infrastructure: you must be able to use matlab, blackboard exams, and...

37
Topic Review for Exam 1 0. Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions 1. Computer 101 2. Developing algorithms and programs 3. Variables and data types 4. User I/O 5. Operators 6. Conditionals (IF) 1

Upload: belinda-moody

Post on 18-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Topic Review for Exam 1 0.Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions 1. Computer 101 2. Developing algorithms

Topic Review for Exam 1

0. Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions

1. Computer 101

2. Developing algorithms and programs

3. Variables and data types

4. User I/O

5. Operators

6. Conditionals (IF)

1

Page 2: Topic Review for Exam 1 0.Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions 1. Computer 101 2. Developing algorithms

1. Computer 101

The Von Neumann computer architecture is mostly what we use today. The architecture separates a computer in 3 major parts: The Central Processing Unit (CPU) The computer memory The Input/Output (I/O) devices

2

CPU + memoryScreen=output

Speakers=output Mouse=input

Keyboard=input

?

?

Knob=input

History 4/4

Page 4: Topic Review for Exam 1 0.Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions 1. Computer 101 2. Developing algorithms

Categories of software

Software contains the instructions the CPU uses to run programs. There are several categories, including:

Operating systems (OS) – manager of the computer system as a whole

Software applications – commercial programs that have been written to solve specific problems

Compilers / Interpreters - to ‘translate’ programs written by people into something understandable by the machine 4

Page 5: Topic Review for Exam 1 0.Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions 1. Computer 101 2. Developing algorithms

Generations of Languages used to write software

1) Machine language – also called binary language. Sequenceof 0’s and 1’s.

2) Assembly language – each line of code produces a single machine instruction (add, subtract…), see bottom of page 11.

3) High-level language – slightly closer to spoken languages.

5

add b,cadd a,b

a= a + b + c;

This line does the same as the two above.

Page 6: Topic Review for Exam 1 0.Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions 1. Computer 101 2. Developing algorithms

Finally… MATLAB Is an interpreted language – does not require

compilation, but instead has an interpreter running behind the scenes

Has an interactive environment – “In the MATLAB environment, you can develop and execute

programs that contain MATLAB commands. You can execute a MATLAB command, observe the results, and then execute another MATLAB command that interacts with the information in memory, observe its results and so on.”

6

Page 7: Topic Review for Exam 1 0.Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions 1. Computer 101 2. Developing algorithms

2. Develop a Solution

Follow these steps in order:

1. State the problem clearly

2. Identify the givens vs. the results wanted This will be referred as the I/O diagram

3. Manually solve the problem

4. Computerize the solution

5. Test, test, test!!!

7

Page 8: Topic Review for Exam 1 0.Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions 1. Computer 101 2. Developing algorithms

3. MATLAB interface, Variable and Data Types

Know variable and file naming constraints

Understand the functions listed in the lecture notes [e.g. sin() , cos() , sind() , cosd() , sqrt() , etc] – know the difference between sin() and sind(), for example.

What is ans?

What does clear do?

What about clc?

How do you suppress default output?8

Page 9: Topic Review for Exam 1 0.Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions 1. Computer 101 2. Developing algorithms

9

Frame #1“The Command Window”

Frame #2“Current

Directory”

Frame #3“Workspace”

Frame #4“Command History”

“Your Calculator Screen”

You can do exactly as on your calculator: add, subtract, divide, multiple, use parentheses to override the order of operations…

Later on, you will realize you can do a LOT more in this frame.

This frame shows the files (excel, text, Matlab files) that can immediately be used.

It will show the variables that have been created.

It will show all the commands executed previously.

Page 12: Topic Review for Exam 1 0.Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions 1. Computer 101 2. Developing algorithms

Variables

A variable is a name given for a memory location “Assigning a value to a variable” means to place a value

in the memory location associated with the variable name

Matlab usually shows what the variables are - in the Command Window, but also in the Workspace.

Page 15: Topic Review for Exam 1 0.Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions 1. Computer 101 2. Developing algorithms

Data Types

A data type is a category of information. The data type determines how information is stored in the computer and how it can be used.

Integerswhole numbers

Floatsnumbers with fractional portion

Stringssequences of characters

15

Page 16: Topic Review for Exam 1 0.Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions 1. Computer 101 2. Developing algorithms

4. Inputs and Outputs

What data-type is the user asked for?

num_apples = input('How many WHOLE apples? ');

The user is asked to provide a number – specifically an integer.

name = input('Enter your name: ', 's');

The user is asked to provide a sequence of characters – i.e. a string.

These are the only 2 forms of using the input() built-in function.

16

Page 17: Topic Review for Exam 1 0.Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions 1. Computer 101 2. Developing algorithms

Form #1. input() In the first form, only 1 prompt is inside the parentheses:

num_apples = input('How many WHOLE apples? ');

There is only one argument to the function. Arguments are inputs to the function – information being sent into the function so it can do its job.

The one argument is the prompt string:'How many WHOLE apples:'

17

Page 18: Topic Review for Exam 1 0.Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions 1. Computer 101 2. Developing algorithms

Form #2. input(…,‘s’) In the second form of the input() function, there are TWO

arguments: the prompt string, and another string: ‘s’name = input('Enter your name: ', 's');

If this argument is present, it must be the letter 's' and no other letter!

1st argument 2nd argument

18

Page 20: Topic Review for Exam 1 0.Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions 1. Computer 101 2. Developing algorithms

Using fprintf()

1. fprintf(...) % is the built-in function

2. fprintf(‘format String InSeRtEd hErE!’)

% The format string allows you to put words and specify a format (UPPER CASE vs. Lower case, and punctuation only)

3. fprintf(‘format string with placeholders’, list of variables to print are inserted here. If more than one variable is to be printed, each is separated by a comma from the previous one)

% Placeholders allow a specific format to be set (aligned right, and 2 decimal places for example)

20

Page 23: Topic Review for Exam 1 0.Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions 1. Computer 101 2. Developing algorithms

Special Characters

Escape sequences can also be used within the format string:\n - this will create a new line when printing the string

\t - tab (tabs the text to the right)

'' - this will place one apostrophe in the final sentence displayed

Example of all three:>> fprintf('%s''s age:\t\t%d years old\n\n', name, age);

Fred's age: 47 years old

>>

23

Page 25: Topic Review for Exam 1 0.Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions 1. Computer 101 2. Developing algorithms

5. Operators & Operands

Operators work on operands. There are 2 types of operands:1. Numerical 1, 3.5, -47

2. Logical true, false

1. Arithmetic (+,-,/,*,^) and relational (<,<=,>,>=,==,~=) operators work with numerical operands

2. Boolean (&&,||,~) operators work on logical operands

25

Page 26: Topic Review for Exam 1 0.Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions 1. Computer 101 2. Developing algorithms

Boolean Operators

These operators take logical values and perform some operation on them to yield a logical value

Two Boolean operators allow to COMBINE relational expressions && Logical AND || Logical OR

One Boolean operator allows to NEGATE the result ~ Logical NOT “Negates”: turns true values into false, and false values into

true26

Page 27: Topic Review for Exam 1 0.Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions 1. Computer 101 2. Developing algorithms

Operators: unary vs. binary

Operators can be unary – taking only one operand:

y = -x;

opposite = ~result;

Or binary operators – taking two operands:z = x * y;

z = x + y;

z = x – y; %both unary and binary!

z = x / y;

z = x ^ y;

x >= 7

(x<3) && (3>y)27

Page 33: Topic Review for Exam 1 0.Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions 1. Computer 101 2. Developing algorithms

% ask user for day

day = input(‘What day # is it? ’);

% find which day it is

if day == 7 %saturday

state = ‘weekend’;

elseif day == 1 %sunday

state = ‘weekend’;

else %any other day

state = ‘weekday’;

end

33

Example: weekend? weekday?

Page 35: Topic Review for Exam 1 0.Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions 1. Computer 101 2. Developing algorithms

Common Mistakes

WHAT YOU CANNOT DO: is shortcut the expression

%if angle is between 0 and 90 degrees

if 0<=angle<90

quadrant = 1;

elseif 90<angle<=180 %quadrant 2

quadrant = 2;

end

35

DOES NOT WORK RIGHT

Instead, rewrite each condition separately!

if 0<=angle && angle<90quadrant = 1;

elseif 90<angle && angle<=180quadrant = 2;

end