What is an computer program ?
- Computer program =
a series of instructions/commands that is
executed by the
computer
Note:
- A computer is
a machine and
cannot think !!!
|
- Some
computer programs are
used to solve
complex problems
- These
computer programs are
called "algorithms"
|
- Algorithm
= a step-by-step procedure
for solving a problem
or accomplishing some task
- A computer algorithm
must be spelled out
in simple steps that
a (non-thinking) machine
(like a computer) can
understand
|
What is not a
computer algorithm
- Consider the
following
steps to
change a light bulb:
Remove the light bulb
Insert a new light bulb
|
- A computer cannot
execute these
steps because:
- A computer
does
not understand
how to
remove a
light bulb
- A computer
does
not understand
how to
insert a
light bulb
|
-
Remember that:
- A computer algorithm
must be spelled out
in
simple steps that
a (non-thinking) machine
(like a computer) can
understand
|
|
What does a computer algorithm
look like ?
- A computer algorithm to
change a light bulb
would look like this:
repeat until the light bulb comes out of socket
{
turn light bulb in counter-clockwise direction
}
pick a new light bulb
repeat until the light bulb is secure in socket
{
turn light bulb in clockwise direction
}
|
- You can see that
computer algorithms uses
very simple operations
|
A famous algorithm to find the
Greatest Common Divisor of
2 numbers
-
Euclid's Algorithm to
find the
Greatest Common Divisor of
numbers
A and
B:
Repeat until (A == 0 or B == 0):
{
if ( A > B )
replace A with the value (A - B)
otherwise
replace B with the value (B - A)
}
if ( A is not equal to 0 )
The Greatest Common Divisor = A
otherwise
The Greatest Common Divisor = B
|
- Recall that
a
computer can
perform
arithmetic operations, this
includes
comparing
2 numbers !
|
A famous algorithm to find the
Greatest Common Divisor of
2 numbers
A famous algorithm to find the
Greatest Common Divisor of
2 numbers
A famous algorithm to find the
Greatest Common Divisor of
2 numbers
A famous algorithm to find the
Greatest Common Divisor of
2 numbers
A famous algorithm to find the
Greatest Common Divisor of
2 numbers
A famous algorithm to find the
Greatest Common Divisor of
2 numbers
A famous algorithm to find the
Greatest Common Divisor of
2 numbers
❮
❯