Programming Midterm
Programming Midterm
program
Th
Algorithm . Pseudocode Flowchart
② ③
7 -
Algorithm
set of comprehensive step
a
by step instructions to resolve a
problem
-
I-start
2 -
Input
3-task
4 - output
5 End
Example 1
* Create an
algorithm to
computed , display gross salary for an
hourly employee
1-start
2 -
4-
multiply the number of hours worked by hourly rate
.
5
Display the result of calculation that was performed in Step 4 .
6- End
Example 2
* Create
change the temperature from farenheit to centigrade.
an
algorithm to
c =( ·
F -
32)
Therefore : -
1-start
- enter value of
2
temp .
in Fahrenheit
3- subtract 32
4 -
6
-
Display the result .
C
. End
7
&↓
-
eudocodeon Pseudocode Flowchart
each statement in it ;
represents an
operation
that be performed
can in a
computer program!
Example 1
Algorithm VS . Pseudocode
> Start > Start .
in the variable S
use or a
Example
* Write the
algorithm and Pseudocode to find if
any given number is odd/even
Algorithm Pseudocode
. Start
1 . Start
1
.
2 enter value of the number .
2 Input N
in Step .
2
4 If
. if display
4 the result of step 3 is 1 then
then . X = 7
,
. If the result of
5 Step 3 is -1 , . If X
5 = 1 then display
then the odd N is odd
display no .
is
1 . Start
N
.
2
Input
3 .
S =
0
4 .
k = 1
. S
5 =
S + K
6
. If k = N then
display S : End
7 . k = k +1
8 : Go to step 5
Draw flowchart to computed the gross
a
display salary of
-
an
hourly employee !
·
Algorithm
1 -
Start
2 -
4-
multiply the number of hours worked by hourly rate
.
5
Display the result of calculation that was performed in Step 4 .
6- End
·
Flowchart
Start
↓
↓
Lisplay
a
J
Fend
Draw a flowchart to find the sum of 2 numbers .
(Using Pseudocode
·
pseudo code · Flowchart
1 start
Nand M
t
2
Input ↓
-
Enter
M
3 5 N
nam/
.
= +
4 .
Display S
↓
. End
5
#ets
N = + M
Display
↓
Send
Start
↓
But
N
yo numbers when have
Display
I max val .
E
MATLAB - Basics
Predefined constants
Formatting -
Built in functions
Matlab has hundred of functions
• To compute sin x where X has a value in radius, you type sin (x)
• To compute cos x, type cos (x)
• Exponential function e^x is computed from exp(x)
• The natural logarithm, Inx, is computed by typing log(x)
• You compute base-l0 logarithm by typing log10(x)
• To inverse sine, or arcsine, is obtained by typing asin(x). It returns
an answer in radians not degrees
• The function asind(x) returns degrees
Arrays
You can use length to determine how many values are in an array.
Polynomial roots
• if you want to continue a session at a later time you must use the save
and load commands
• Typing save causes matlab to the workspace variables
• To retrieve the workspace type load, you can then continue your work
session
• To save the workspace variables in another five named filename-mat
type, save filename
pptX.
>
- A file that contains all commands and it , it will have
by running
performed the the command window
operation you type
same as when in
When
you type its name in the command window it will run .
>
- When Matlab M-file should be used
Saving programs a
e 1 m
g program
.
: - .
* 2 types of on-files
function firs
- cript files (command files
Creating a
script file
:
-
Advantages : -
commands
* avoid
typing long lengthy
* Do not script file the that of
give a same name as
a command
· exist
(example 1')
= if it shows O then it doesn't exist
I if it shows 1 then it exists
Built-in functions :
they are like building blocks that form the
basis of other Matlab functions
>
-
Debugging
A
process of finding & removing the "bugs" or errors a
program
might have
Errors
may be
:
-
1-
Syntax errors
Styping the
wrong command like putting paranthesis & comma
2- Runtime errors
The
Symbol % designates a comment
,
this isn't executed by Matlab
· used to document files
e
g
.
% This is a comment
3) x = 2+ 3 % so is this
-
to the of %
3) X =
right
5 So its not considered !
Controls Input/output
-disp >
-
short for display (used to
display the value of variables only
-disp(t) >
-
displays variables name as shown in Matlab
can as
-
: -
user
write specifical
to
somthing and then stores it in a variable
input /(please of )
-
X =
enter value X :
# if
you were to type X and press enter
x =
5
-
A string variable is composed of letters (text)
menu >
-
generates menu with choices for user input
known as
'Arrays' as one
single entity
.
We also
specify the location of any value
using Cartesian coordinates
-
can
· A rector can
only have one column
,
or
only one row
· to
type a row vector in MATLAB , you just type the elements (e g .
.
1
,
2
, 3)
inside square brackets [7 .
·
For column vector -
:
you can
by semicolcons !
-
Alternatively you ,
can also create a now rector then use the
or Vice versa.
>
- Row vectors
(horizontally)
(vertically) /
>
-
column vectors
Vectors
·
can
easily be combined to other vectors ; this process is called
"appending"
M=
J
the (: ) vector of
· colon operator generates a
large regularly spaced elements
exp :
typingX = m:
g:n
-
the last value is n.
our n-value .
Demonstration :
> X = 0: 2 : 8 -
> m -
n = 0-8 = 8 which is a
multiple of 21
therefore the vector X =
[0 ,
2
,
9, 6
, 8
while if we type this insted
x = 0 : 2: 7 >
-
m-n = 0 -
7 = 7 not a
multiple of 2X
exp :
v =
10 : - 2 : 4
syntax :
linespace (x1 , X Limit
&
lower limit
no · of
points
·
logspace command creates an of
logarithmically spaced elements
array
Syntax :
logspace (c ,
b ,
n)
Yno of
· prints
no. btw
10d los
called a 'matrix'
denoted by bold letters .
usually case
a size of an
array is determined by the no ·
of rows X no · of columns
·
exp .
3x2 >
- 3 rows a 2 colums
Indices which are subscripts like (ij) are used to indicate an elements
A =
[41]
* When matrix has & the next
a
many elements
you can
press Enter Continue
typing on
:
-
a =
[ = 55] a =
[ -]
· it swiches the rows & columns
vector into
converts a row a column vector
!
Alternatively type (') to
transpose the and still
-
the same
you can
array get
exact result .
Array Indices
>
-
Array indices arerow
& column numbers of an element in
any given array
to track the
location of elements
exp : the notation v(5) refers to the fifth element in the vector .
V
an
array
to of
exp .
you type : -
D (1 3)
,
= 6
Array addressing
4 v (i) all and of vector
represents rows columns
Empty array
no as
exp .
A(3 : ) =
5) deletes the 3rd row in A.
A ( :, 2 4) 5] : =
"and
through 4th columns in A
clear used to clear all variables that
is
you specify
-
exp : A - clears A
clear
only before
proceeding with
any more
commands .
elements.
Vector having all real
A =
C -
2 + 2i
,
4 + i ,
-
1 -
3i] ;
B =
max(A)
Output
B = 4 . 000 + 1 , 000 :
* if A has one or more complex elements the max , min and sort
, ,
functions act on the abs . values of the element that has the
largest magnitudee.
=
[
expi
·
Midterm Review !
Pseudocode P KK24341 K
example Comput
= ...
:
1 .
Start
.
2 Inputn We
*
changed the variable
3 .
4 . i =
1 have .
K
5 .
p =
Paki
j
<
6 .
If EU then
displend
i+
7 .#+ i =
easy elp !
8 to 5 will be
go
.
(know it well
difficult
Focus
* on the labwork !!! nothing more
any factorial
Find the value of number
example 2 :
4) =
4x3X2x1 = 24
I I
. Start
1 1 Start
.
.
2 Input N
.
2 N
Input
.
3 S =
1
3 . 5 =
4 .
k
= 1
4 K = N
first
.
5 . 5 = 55k
5 .
S =
SAK
6 .
If k = N then disp (end
:
6 If K =
1 then disp (end)
:
7
.
4 =
k+1
7. k =
k - 1
8 .
go to 5 8 go .
to 5
No
>
-
explanation
No
* convert from decimal to
something
+ understand.
focus on these !
-> Practice
>
-
coding (Labwork) > start
- with the
>
-
Pseudo code lab work.
> Flow
- Chart
3 understand
(EASY inshallah)MID-
well ! TERM
Application of
Arrays !
Polynomial operations
-
· The function
poly(r) computes the coefficients of the
polynomial
whose roots are
specified by the array r .
p =
poly([1 .