You are on page 1of 10

Computer Programming

MT114 – 2020/2021

Idrissa S. A.
Amour.Idrissa@udsm.ac.tz

Introduction to Computer
Programming – 1/2
Department of Mathematics - UDSM
November 4, 2021
Algorithms and Design
Short Discussion
Ask your friend how to prepare a tea!

Algorithms
An algorithm is an outline of unambiguous steps to be
followed to solve a particular task.

How it Works
Al-Khwarizmi: worked on steps to achieve goal.
Algorithms solves general problems, not specific.
Step-by-step methods a machine performs.
Before computers! Human vs Algorithm vs
Computers.
ISA MT114 – 2020/2021 2 / 10
Algorithms and Design

Day of the week Algorithm for a given date


1 Let century year be equal to the first two digits of
the year.
2 Let year digits be equal to the last two digits of
the year.
3 Let value = year digits + floor(year digits/4)
4 If century year equals 18 then add 2 to value, else
If century year equals 20 then add 6 to value.

ISA MT114 – 2020/2021 3 / 10


Algorithms and Design
Algorithm cont.
5 If month is Jan and year is not a leap year, then
add 1 to value, else
If month is Feb and year is a leap year, then add 3
to value; if not a leap year then add 4 to value, else
If month is March or Nov, then add 4 to value, else
If month is May, then add 2 to value, else
If month is June, then add 5 to value, else
If month is Aug, then add 3 to value, else
If month is Oct, then add 1 to value, else
If month is Sept or Dec, then add 6 to value.
6 Set value = (value + day) mod 7
ISA MT114 – 2020/2021 4 / 10
Algorithms and Design
Algorithm cont.
7 If the value is 0, then the day is Saturday, else
If the value is 1, then the day is Sunday, else
If the value is 2, then the day is Monday, else
If the value is 3, then the day is Tuesday, else
If the value is 4, then the day is Wednesday, else
If the value is 5, then the day is Thursday, else
If the value is 6, then the day is Friday

Exercise 1
Determine the weekday that Tanganyika became
independent.
ISA MT114 – 2020/2021 5 / 10
Algorithms and Design

Algorithms Design
3 ways to represent an algorithm:
1 Steps: Just like the weekday algorithm
2 Flowcharts: Algorithm is presented pictorially
3 Pseudocode: Code like steps for solving the
problem

ISA MT114 – 2020/2021 6 / 10


Algorithms and design

Flowcharts
A flowchart: pictorial representation of algorithm or
workflow or process.
Shapes connected with arrows to show processes
and directions.
Analyzing/designing/documenting/managing
process.
The first ”flow process chart”, introduced in 1921.
Many categories: Here we focus on program
flowcharts.

ISA MT114 – 2020/2021 7 / 10


Algorithms and design
Flowchart Common Blocks

ISA MT114 – 2020/2021 8 / 10


Fixing Fault Bulb
Example 1: Fixing Fault Bulb

ISA MT114 – 2020/2021 9 / 10


Algorithms and Design
Exercise 2
Design a complete algorithm that you are going to use to
fix a non-functioning light in your room. Represents
using a flowchart.

Exercise 3
Design a complete algorithm that will be used to
determine if a given number is even or odd. Represents
using a flowchart.

Exercise 4
Design a flowchart algorithm that will be used to
determine the sum of integers between 1 and 1000.
ISA MT114 – 2020/2021 10 / 10

You might also like