0% found this document useful (0 votes)
68 views9 pages

MECN2012: Computing Skills and Software Development: Summary of Algorithms and Translation of Logic Into Code

This document discusses algorithms and translating logic into code. It provides examples of non-looping and looping algorithms like switch-case, if-then-else, for loops, and while loops. It then gives an example of identifying prime numbers using the trial division method, outlining the basic logic and conditions that need to be considered when writing code. This includes identifying input limits, default values, and adapting the basic prime number checking operation into a loop to test a range of numbers.

Uploaded by

Ali Gh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views9 pages

MECN2012: Computing Skills and Software Development: Summary of Algorithms and Translation of Logic Into Code

This document discusses algorithms and translating logic into code. It provides examples of non-looping and looping algorithms like switch-case, if-then-else, for loops, and while loops. It then gives an example of identifying prime numbers using the trial division method, outlining the basic logic and conditions that need to be considered when writing code. This includes identifying input limits, default values, and adapting the basic prime number checking operation into a loop to test a range of numbers.

Uploaded by

Ali Gh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

MECN2012: Computing

Skills and Software


Development
Lecture 7.5:
Summary of algorithms and
translation of logic into code

Broad Division of Algorithms


Type

Algorith
m

Where used

Nonlooping

SwitchCase

Discrete cases / values (Is it equal to...?)

If-ThenElse

Ranges of values (Yes-No questions Is it


greater than...?, Is it less than...?)

Looping For

While

Running a loop for a KNOWN NUMBER of


iterations (Do this 20 times...,Do this for
every element in the array...)
Running a loop until a set point is reached
(UNKNOWN NUMBER of iterations) (Do this
until the value is equal to / greater than / less
than...,Do this until this many numbers have
been found...)

Translating Logic to
Algorithms
e.g. Identifying prime numbers by the trial
division method
Identify the conditions under which operations
must be performed
Develop code for the most basic operation(s)
you want to perform, possibly several of these
for different conditions [e.g. Checking if one
number is a prime number]
Adapt operation(s) for repetition if needed [e.g.
Checking a range of numbers for being prime]

Identify Conditions
Need to get limits from user (can be passed
to function)
If the range of numbers includes 2 and 3,
these numbers cannot be tested by the
trial division method and must be included
in the reported set
All numbers bigger than 3 can be tested by
the trial division method
If the upper limit is less than 5 no trial
division is necessary

Basic Operations
For a number to be a prime number, the
modulo of all numbers between 2 and its
square root and the number itself must NOT
be zero
Test number: n
Range of numbers: x = 2:1:floor(sqrt(n))
if min(mod(n,x)) ~= 0
(Prime number) Print to screen
else
(Not a prime number) Do nothing
end

Adapt Operation
If lower limit is less than 4 print default numbers:
if lowerlimit <= 3

But, if upper limit is less than 5, might not print out


both:
if upperlimit <= 5

To print out default numbers, combine these and use


for loop:
for counter = 2:1:3
if counter >= lowerlimit && counter <= upperlimit
counter
end
end

Adapt Operation (2)


To test any numbers bigger than 4, LOOP
the method used to identify prime
number:
Lower limit might be bigger than 4
if upperlimit > 4
if lowerlimit > 4
lowerlooplimit = lowerlimit
else
lowerlooplimit = 4
end
(end) Will need to add more in here

Adapt Operation (3)


Create the loop that tests each
number:
for counter2 =
lowerlooplimit:1:upperlimit
x = 2:1:floor(sqrt(counter2))
if min(mod(counter2,x)) ~= 0
counter2
end
end

Testing and Debugging


Components can be developed in the
workspace using representative
dummy variable and then
implemented in the function once
they work
Assemble full code and test
Debug as necessary

You might also like