general computer science for engineers cisc 106 lecture 04 roger craig computer and information...
Post on 20-Dec-2015
216 views
TRANSCRIPT
![Page 1: General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009](https://reader030.vdocument.in/reader030/viewer/2022033108/56649d425503460f94a1d675/html5/thumbnails/1.jpg)
General Computer General Computer Science Science
for Engineersfor EngineersCISC 106CISC 106
Lecture 04Lecture 04
Roger CraigComputer and Information Sciences
9/11/2009
![Page 2: General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009](https://reader030.vdocument.in/reader030/viewer/2022033108/56649d425503460f94a1d675/html5/thumbnails/2.jpg)
Lecture ObjectivesLecture ObjectivesIf statements (nested)Logical OperatorsMatlab Output
![Page 3: General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009](https://reader030.vdocument.in/reader030/viewer/2022033108/56649d425503460f94a1d675/html5/thumbnails/3.jpg)
IF StatementsNested if statements can be used
when two things have to be true for a command to execute
![Page 4: General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009](https://reader030.vdocument.in/reader030/viewer/2022033108/56649d425503460f94a1d675/html5/thumbnails/4.jpg)
IF StatementsUsing nested if
statements
if (x < 5)
if ( x > 2)
y = 500
else
y = 300
end
else
y= 200
end
![Page 5: General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009](https://reader030.vdocument.in/reader030/viewer/2022033108/56649d425503460f94a1d675/html5/thumbnails/5.jpg)
IF StatementsUsing nested if
statements
if (x < 5)
if ( x > 2)
y = 500
else
y = 300
end
else
y= 200
end
Using multiple conditions in one if statement
if ( x < 5 & x > 2)
y = 500
else
y = 200
end
![Page 6: General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009](https://reader030.vdocument.in/reader030/viewer/2022033108/56649d425503460f94a1d675/html5/thumbnails/6.jpg)
Logical Operators - AND
&
&&
A & BA
T F
BT T F
F F F
![Page 7: General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009](https://reader030.vdocument.in/reader030/viewer/2022033108/56649d425503460f94a1d675/html5/thumbnails/7.jpg)
Logical Operators - OR
|
||
A | BA
T F
BT T T
F T F
![Page 8: General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009](https://reader030.vdocument.in/reader030/viewer/2022033108/56649d425503460f94a1d675/html5/thumbnails/8.jpg)
Logical Operators - NOT~
~A
T F
F T
![Page 9: General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009](https://reader030.vdocument.in/reader030/viewer/2022033108/56649d425503460f94a1d675/html5/thumbnails/9.jpg)
Numbers as True/FalseWhat do you think would happen
here:
if (5)
disp(‘5 is a true value’);
else
disp(‘5 is a false value’);
end
![Page 10: General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009](https://reader030.vdocument.in/reader030/viewer/2022033108/56649d425503460f94a1d675/html5/thumbnails/10.jpg)
Numbers as True/False Group activity: evaluate the
following as true or false for x=5:
1. x < 7 & 3 < x2. ˜(x / 2 > 1 & x < 0)3. x ˜= 5 & x + 2 > 6 | x > 54. x + x == 2 * x && ˜x
![Page 11: General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009](https://reader030.vdocument.in/reader030/viewer/2022033108/56649d425503460f94a1d675/html5/thumbnails/11.jpg)
Matlab Output◦ How do we see information about what is going on
in our program?
![Page 12: General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009](https://reader030.vdocument.in/reader030/viewer/2022033108/56649d425503460f94a1d675/html5/thumbnails/12.jpg)
Matlab Output◦ How do we see information about what is going on
in our program?
◦ disp()
![Page 13: General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009](https://reader030.vdocument.in/reader030/viewer/2022033108/56649d425503460f94a1d675/html5/thumbnails/13.jpg)
Matlab OutputFormatted OutputExample:
◦ “There are 5 widgets in inventory”◦ fprintf(‘There are %d widgets in inventory’,
widgets);
![Page 14: General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009](https://reader030.vdocument.in/reader030/viewer/2022033108/56649d425503460f94a1d675/html5/thumbnails/14.jpg)
Matlab OutputFormatted OutputExample:
◦ “There are 5 widgets in inventory”◦ fprintf(‘There are %d widgets in inventory’,
widgets);%d acts as a place holder for widget variable at end
of command and is known as a conversion character.
Conversion characters specify the notation of the output.
Note the variable widgets as the second argument.
![Page 15: General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009](https://reader030.vdocument.in/reader030/viewer/2022033108/56649d425503460f94a1d675/html5/thumbnails/15.jpg)
Matlab Output
Lets look at the syntax◦count = fprintf(fid, format, A, ...)◦count – the number of bytes written◦fid – refers to opened file◦format – is a format string in between
single quotesIf fid is set to one or omitted the output goes
to the screen.
![Page 16: General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009](https://reader030.vdocument.in/reader030/viewer/2022033108/56649d425503460f94a1d675/html5/thumbnails/16.jpg)
Arrays
![Page 17: General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009](https://reader030.vdocument.in/reader030/viewer/2022033108/56649d425503460f94a1d675/html5/thumbnails/17.jpg)
ArraysArrays
![Page 18: General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009](https://reader030.vdocument.in/reader030/viewer/2022033108/56649d425503460f94a1d675/html5/thumbnails/18.jpg)
ArraysArraysFundamental unit of data in MATLABCollection of data into rows and
columns(MATrix LABoratory)1 dimensional, vector2 dimensional, matrix0 dimensional, scalar (a 1x1 array)
Row vector, column vector , transpose
![Page 19: General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009](https://reader030.vdocument.in/reader030/viewer/2022033108/56649d425503460f94a1d675/html5/thumbnails/19.jpg)
ArraysArrays
![Page 20: General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009](https://reader030.vdocument.in/reader030/viewer/2022033108/56649d425503460f94a1d675/html5/thumbnails/20.jpg)
Array commandsArray commandsa = [1 2 3 4]b = [1; 2; 3; 4]c = [1 2; 3] (error)d = [1 2; 3 4]f = d(1,2)g(4,5) = 7
![Page 21: General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009](https://reader030.vdocument.in/reader030/viewer/2022033108/56649d425503460f94a1d675/html5/thumbnails/21.jpg)
Arrays continuedArrays continuedzeros(), ones(), eye()scalar vs. array arithmetic
+, - , * (.*) , / (./) ^ etc.