Upload
unity-pope
View
17
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Topic Review for Exam 1. 0.Infrastructure: You must be able to use MATLAB, Blackboard Exams, and Blackboard submissions Computer 101 Developing algorithms and programs Variables and data types User I/O Operators Conditionals (IF). 1. Computer 101. - PowerPoint PPT Presentation
Citation preview
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
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
Exam Topics
Software: OS, apps, compilers
1. Describe the purpose of a compiler.2. What differentiates the Operating System (OS)
from an application (app)?
Answer: Next slide
3
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
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.
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
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
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
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.
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.
Functions
Common functions sin(), cos(), tan(), sind(), cosd(), tand(), asin(), acos(), atan(), asind(), acosd(), atand() exp(), sqrt(), log(), log10() abs(), round()
13
“Suppressing” the output
Second version, with semi-colons
14
Using a semicolon on these lines “suppresses the output”.
In other words, it does not show the result of the command.
But…did the commands do anything?
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
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
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
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
19
The fprintf() function
The format string
Placeholders
Format modifiers
left justification
width specifier
precision specifier
Escape sequences
Output
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
Stop for vocabulary
Just a quick recall about the vocabulary.
fprintf(‘The value in result is %f meters.’, result);
“function call”
‘format string’
“placeholder”(part of format string)
variable to be printed
21
Most common placeholders
Each data-type has a placeholder Integer %d Floats %f Strings %s A single letter %c
22
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
Format Modifiers
Once the base placeholder is ready, modifiers further change how the values are displayed.
Complete Format Modifier form:
%-7.2f
Left-justify the value
TOTAL width to occupy
Nb. of decimal places
24
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
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
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
Operators: Output values
Type Input values Output values
Arithmetic: Numbers Numbers
e.g. 5 * 3 15
Relational: Numbers Logical
e.g. 5 < 3 false
Boolean: Logical Logical
e.g. ~true false28
Unary vs Binary operators&, &&, |, ||, +, -, *, /, ~, <, =, ==
From the set of operators above…
Which one(s) can be used as either unary or binary operators?
Which one(s) are only unary operators?
29
Name these operators:
Relational<, >, <=, >=, ==, ~=
Boolean&&, ||,
~
Know the true and false keywords30
6. IF: General template
if <logical expression 1><code block 1>
elseif <logical expression 2><code block 2>
.
.
.elseif <logical expression n>
<code block n>else
<default code block>end
32
% 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?
Using Logical OR% ask user for dayday = input(‘What day number is it (1-7)? ’);
% find which day it isif day == 7 || day == 1 %Saturday or Sunday
state = ‘weekend’;else %any other day
state = ‘weekday’;end
34
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
36
Q: ConditionalsBoolean expressions
Using &&, ||, and ~
What is the output of this code?a = true;b = 1;c = -4;if a && ~(b>c)
disp('IF');else
disp('ELSE');end