Professional Documents
Culture Documents
Bogotá, Colombia - July 11th, 2016: Microprocessors" With Me
Bogotá, Colombia - July 11th, 2016: Microprocessors" With Me
yes
d<n-‐1
no
i
=
d
+
1
yes
i
<
n
no
mtx[d][d]
must
be
tmp
=
mtx[i][d]
/
mtx[d][d]
different
to
0.0.
If
so
det=0.0
and
exit
det
=
1.0
d
=
d
+
1
j
=
d
If
(j=0)
“Diagonal
PreparaIon”
will
become
“Gaussian
d
=
0
EliminaIon”
yes
j
<
n
no
yes
mtx[i][j]
-‐=
d
<
n
tmp
*
mtx[d][j]
no
det
*=mtx[d][d]
i
=
i
+
1
j
=
j
+
1
Write
d
=
d
+
1
det
End
d<n-‐1
no
¡
Before
the
4th
week,
students
i
=
d
+
1
must
be
able
to
code
a
flowchart
in
C
language
or
to
draw
a
flowchart
from
a
given
code
!
yes
i
<
n
no
tmp
=
mtx[i][d]
/
mtx[d][d]
det
=
1.0
d
=
d
+
1
j
=
d
d
=
0
yes
j
<
n
no
yes
mtx[i][j]
-‐=
d
<
n
tmp
*
mtx[d][j]
no
det
*=mtx[d][d]
i
=
i
+
1
j
=
j
+
1
Write
d
=
d
+
1
det
End
yes
d
>
0
no
VariaIons
to
previous
flowchart
i
=
d
-‐
1
could
be
used
to
design
algorithms
for
Gaussian
EliminaIon
&
Matrix
Inversion
yes
i
>=
0
no
mtx[d][d]
must
be
tmp
=
mtx[i][d]
/
mtx[d][d]
different
to
0.0.
d
=
d
-‐
1
j
=
0
If
(j<2n)
flowchart
can
be
used
with
Jordan
to
compute
Mtx_Inversion
yes
j
<n+1
no
mtx[i][j]
-‐=
tmp
*
mtx[d][j]
i = i -‐ 1
j = j + 1
End
DesIny
Nodes
Source
Nodes
yes
i
<
n
no
tmp
=
mtx[i][d]
/
mtx[d][d]
det
=
1.0
d
=
d
+
1
j
=
d
d
=
0
yes
j
<
n
no
yes
mtx[i][j]
-‐=
d
<
n
tmp
*
mtx[d][j]
no
det
*=mtx[d][d]
i
=
i
+
1
j
=
j
+
1
Write
d
=
d
+
1
det
End
yes
d<n-‐1
no
i
=
d
+
1
yes
i
<
n
no
tmp
=
mtx[i][d]
/
mtx[d][d]
det
=
1.0
d
=
d
+
1
j
=
d
d
=
0
yes
j
<
n
no
yes
mtx[i][j]
-‐=
d
<
n
tmp
*
mtx[d][j]
no
det
*=mtx[d][d]
i
=
i
+
1
j
=
j
+
1
Write
d
=
d
+
1
det
End
no
i
=
d
+
1;
i
<
n
yes
no
tmp
=
mtx[i][d]
/
mtx[d][d];
det
=
1.0;
d
=
d
+
1;
j
=
d;
d
=
0;
j
<
n
yes
d<n
no
yes
mtx[i][j]
-‐=
tmp
*
mtx[d][j];
no
det
*=
i
=
i
+
1;
mtx[d][d];
j
=
j
+
1;
Write
det;
d
=
d
+
1;
End
d<n-‐1
yes
no
i
=
d
+
1;
i
<
n
yes
no
det
=
1.0;
d
=
d
+
1;
tmp
=
mtx[i][d]
/
mtx[d][d];
d
=
0;
j
=
d;
j
<
n
yes
d<n
no
yes
mtx[i][j]
-‐=
tmp
*
mtx[d][j];
no
det
*=
j
=
j
+
1;
mtx[d][d];
i
=
i
+
1;
d
=
d
+
1;
Write det;
End
Begin
Source
Nodes
A
Read
mtx
&
n;
d
=
0;
d<n-‐1
yes
B
no
C
i
=
d
+
1;
i
<
n
yes
D
no
J
det
=
1.0;
I
d
=
d
+
1;
tmp
=
mtx[i][d]
/
mtx[d][d];
d
=
0;
E
j
=
d;
j
<
n
yes
F
d<n
no
K
yes
mtx[i][j]
-‐=
tmp
*
mtx[d][j];
G
no
det
*=
j
=
j
+
1;
L
mtx[d][d];
H
i
=
i
+
1;
d
=
d
+
1;
M Write det;
End
“Nodes
will
be
Links,
and
Links
will
be
Nodes”
To
convert
the
flowchart
into
a
Finite
State
Begin
Machine,
every
link
is
idenIfied
as
a
possible
node
or
state
in
FSM.
ExcepIons
will
be,
links
between
a
condiIonal
and
following
Read
mtx
&
n
instrucIons.
d = 0
yes
d<n-‐1
no
i
=
d
+
1
yes
i
<
n
no
tmp
=
mtx[i][d]
/
mtx[d][d]
det
=
1.0
d
=
d
+
1
j
=
d
d
=
0
yes
j
<
n
no
yes
mtx[i][j]
-‐=
d
<
n
tmp
*
mtx[d][j]
no
det
*=mtx[d][d]
i
=
i
+
1
j
=
j
+
1
Write
d
=
d
+
1
det
End
yes
d<n-‐1
no
i
=
d
+
1
yes
i
<
n
no
tmp
=
mtx[i][d]
/
mtx[d][d]
det
=
1.0
d
=
d
+
1
j
=
d
d
=
0
yes
j
<
n
no
yes
mtx[i][j]
-‐=
d
<
n
tmp
*
mtx[d][j]
no
det
*=mtx[d][d]
i
=
i
+
1
j
=
j
+
1
Write
d
=
d
+
1
det
End
Read
mtx
&
n
d = 0
yes
d<n-‐1
no
i
=
d
+
1
yes
i
<
n
no
tmp
=
mtx[i][d]
/
mtx[d][d]
det
=
1.0
d
=
d
+
1
j
=
d
d
=
0
yes
j
<
n
no
yes
mtx[i][j]
-‐=
d
<
n
tmp
*
mtx[d][j]
no
det
*=mtx[d][d]
i
=
i
+
1
j
=
j
+
1
Write
d
=
d
+
1
det
End
no
yes
no
yes
no
yes
no
End
yes
no
yes
no
yes
no
yes
no
End
NULL
/
Read
mtx
&
n;
Now,
it
might
be
lirle
bit
clear
the
reason
I
fused
condiIonal
and
following
node
in
just
one
node.
NULL / d = 0;
d<n-‐1 / i = d + 1;
i
>=
n
/
d
=
d
+
1;
NULL
/
j
=
d;
NULL / d = 0;
j
<
n
/
d<n
/
d>=n
/
mtx[i][j]
-‐=
det
*=
Write
tmp
*
mtx[d][j];
mtx[d][d];
det;
j
>=
n
/
i
=
i
+
1;
NULL
/
j
=
j
+
1;
NULL / d = d + 1;
d>=n-‐1
/
det
=
1.0;
i
<
n
/
tmp
=
mtx[i][d]
/
mtx[d][d];
i
>=
n
/
d
=
d
+
1;
NULL
/
j
=
d;
NULL / d = 0;
j
<
n
/
d<n
/
d>=n
/
mtx[i][j]
-‐=
det
*=
Write
tmp
*
mtx[d][j];
mtx[d][d];
det;
j
>=
n
/
i
=
i
+
1;
NULL
/
j
=
j
+
1;
NULL / d = d + 1;
NULL / d = 0;
d<n-‐1 / i = d + 1;
d>=n-‐1
/
det
=
1.0;
i
<
n
/
tmp
=
mtx[i][d]
/
mtx[d][d];
i
>=
n
/
d
=
d
+
1;
NULL
/
j
=
d;
NULL / d = 0;
j
<
n
/
d<n
/
d>=n
/
mtx[i][j]
-‐=
det
*=
Write
tmp
*
mtx[d][j];
mtx[d][d];
det;
j
>=
n
/
i
=
i
+
1;
NULL
/
j
=
j
+
1;
NULL / d = d + 1;
NULL
/
Read
mtx
&
n;
d
=
0;
d<n-‐1 / i = d + 1;
d>=n-‐1
/
det
=
1.0;
d
=
0;
i
<
n
/
tmp
=
mtx[i][d]
/
mtx[d][d];
j
=
d;
i
>=
n
/
d
=
d
+
1;
j
<
n
/
mtx[i][j]
-‐=
d>=n
/
tmp
*
mtx[d][j];
Write
j
=
j
+
1;
det;
j
>=
n
/
d<n
/
i
=
i
+
1;
det
*=
mtx[d][d];
d
=
d
+
1;
d>=n-‐1
/
det
=
1.0;
d
=
0;
i
>=
n
/
d
=
d
+
1;
i
<
n
/
tmp
=
mtx[i][d]
/
mtx[d][d];
j
=
d;
j
>=
n
/
i
=
i
+
1;
d<n
/
det
*=
j
<
n
/
mtx[d][d];
mtx[i][j]
-‐=
d
=
d
+
1;
tmp
*
mtx[d][j];
j
=
j
+
1;
d>=n
/
Write
det;
d<n-‐1
/
i
=
d
+
1;
0
d>=n-‐1
/
det
=
1.0;
d
=
0;
1
i
>=
n
/
d
=
d
+
1;
i
<
n
/
tmp
=
mtx[i][d]
/
mtx[d][d];
j
=
d;
j
>=
n
/
i
=
i
+
1;
2
d<n
/
det
*=
3
mtx[d][d];
j
<
n
/
mtx[i][j]
-‐=
d
=
d
+
1;
tmp
*
mtx[d][j];
j
=
j
+
1;
d>=n
/
Write
det;
B
d<n-‐1
/
i
=
d
+
1;
0
C
d>=n-‐1
/
det
=
1.0;
d
=
0;
E
1
i
>=
n
/
D
d
=
d
+
1;
i
<
n
/
tmp
=
mtx[i][d]
/
mtx[d][d];
j
=
d;
G
j
>=
n
/
i
=
i
+
1;
H
2
F
d<n
/
det
*=
3
mtx[d][d];
j
<
n
/
mtx[i][j]
-‐=
d
=
d
+
1;
tmp
*
mtx[d][j];
j
=
j
+
1;
I
d>=n
/
Write
det;
0
RUNNING_SUBMTX
1
RUNNING_ROWS
2
RUNNING_COLS
3
COMPUTING_TRACE
vs.
A
FSM
represented
by
an
Adjaceny
Matrix
Research
quesIon:
¿Could
this
process
be
automaIcally
systemaIzed?
Of
course,
It
can
be…
¿Why
not?
Aver
many
exercises,
the
RULE
OF
Begin
THUMB
that
students
may
infer
about:
Read
mtx
&
n
Could
it
be
possible
to
esRmate
the
number
of
states
from
a
flowchart?
d
=
0
Count
the
number
of
condiIonals
yes
d<n-‐1
no
i
=
d
+
1
yes
i
<
n
no
tmp
=
mtx[i][d]
/
mtx[d][d]
det
=
1.0
d
=
d
+
1
j
=
d
d
=
0
yes
j
<
n
no
yes
mtx[i][j]
-‐=
d
<
n
tmp
*
mtx[d][j]
no
det
*=mtx[d][d]
i
=
i
+
1
j
=
j
+
1
Write
d
=
d
+
1
det
End
…
but
I
reccomend
the
use
of
“enum”
type,
because
you
do
not
have
to
think
of
labeling
every
idenIfier
with
numbers:
! !
typedef enum STATE_T STATE_T;! typedef enum STATE_T STATE_T;!
enum STATE_T {! enum STATE_T {!
! !
!RUNNING_SUBMTX,! !RUNNING_ROWS,!
! !
!RUNNING_ROWS,! !COMPUTING_TRACE,!
! !
!RUNNING_COLS,! !RUNNING_SUBMTX,!
! !
!COMPUTING_TRACE! !RUNNING_COLS!
! !
};! };!
! !
I
suggest
to
introduce
a
NEW
“BOOLEAN”
variable
“finish”
to
indicate
the
2 temporary
halt
in
the
execuIon
of
the
Finite
State
Machine
to
QUIT
the
repeIIon
sequence
when
MACHINE
reaches
the
ending
point.
!
typedef enum BOOLEAN BOOLEAN;!
enum BOOLEAN {!
!
!FALSE,!
!
!TRUE!
!
};!
!
double!
There
are
some
strategies
to
3
Mtx_Determinant( double mtx[][COLS], int n )!
{!
int diagonal, i, j;!
implement
a
FSM
but
the
use
of
double tmp, determinant = 1.0;! a
“switch-‐case”
proposiIon
is
int finish = FALSE;!
STATE_T state;! very
easy
to
use,
teach,
!
diagonal = 0;! implement,
and
test.
state = RUNNING_SUBMTX;!
!
while( ! finish ) {!
!
switch( state ) {!
!
case RUNNING_SUBMTX:!
if( diagonal < n-1 ) {!
i = diagonal + 1;!
state = RUNNING_ROWS;!
} else {!
determinant = 1.0;!
diagonal = 0;!
state = COMPUTING_TRACE;!
}!
break;!
!
case RUNNING_ROWS:!
if( i < n ) {!
tmp = mtx[i][diagonal] /!
mtx[diagonal][diagonal];! This
“while”
loop
is
THE
ENGINE
j = diagonal;!
state = RUNNING_COLS;! of
the
Finite
State
Machine:
} else {!
diagonal = diagonal + 1;!
state = RUNNING_SUBMTX;!
}! ExecuIon
is
done
unIl
an
event
break;!
! defined
with
variable
“finish”
is
case RUNNING_COLS:!
if( i < n ) {! reached,
otherwise…
mtx[i][j] -=!
( tmp / mtx[diagonal][j] );!
j = j + 1;!
state = RUNNING_COLS;! “while
NOT
finish,
…
FSM
will
} else {!
i = i + 1;! keep
working”.
state = RUNNING_ROWS;!
}!
break;!
!
case COMPUTING_TRACE:!
if( diagonal < n ) {!
determinant *=!
mtx[diagonal][diagonal];!
diagonal = diagonal + 1;!
state = COMPUTING_TRACE;!
} else {!
finish = TRUE;!
}!
break;!
!
} /* switch */!
!
} /* while */!
!
return determinant;!
!
} /* Mtx_Determinant */!
!
double! double!
Mtx_Determinant( double mtx[][COLS], int n )! Mtx_Determinant( double mtx[][COLS], int n )!
{! {!
int diagonal, i, j;! int diagonal, i, j;!
!
} /* switch */!
These
could
be
omired
but
I
} /* while */!
!
recommend
ALWAYS
using
them,
return determinant;!
!
no
marer
if
only
one
proposiIon
} /* Mtx_Determinant */!
!
is
used
INSIDE.
Although
both
segments
are
RIGHT,
I
recommend
my
students
to
use
the
one
at
the
right!!!
! !
case RUNNING_SUBMTX:! case RUNNING_SUBMTX:!
if( diagonal < n-1 ) {! if( diagonal < n-1 ) {!
i = diagonal + 1;! i = diagonal + 1;!
state = RUNNING_ROWS;! state = RUNNING_ROWS;!
} else if( diagonal >= n-1 ) {! } else {!
determinant = 1.0;! determinant = 1.0;!
diagonal = 0;! diagonal = 0;!
state = COMPUTING_TRACE;! state = COMPUTING_TRACE;!
}! }!
break;! break;!
! !
!
case ANY_STATE:!
if( event_0() ) {!
instruction_0();! 1st
priority
state = NEXT_STATE_0;!
} else if( event_1() ) {!
instruction_1();!
2nd
priority
state = NEXT_STATE_1;!
} else if( event_2() ) {!
instruction_2();!
state = NEXT_STATE_2;! 3rd
priority
} else { // everything else!
instruction_3();!
state = ANY_STATE;!
Last
priority
}!
break;!
!
6.
Summary
SUMMARY
(1/4)