İNŞ246
Com. Pro. in Civil Eng.
(#2 Algorithms)
Simple Economy Model Link
➢ This model is a very simple model of economic exchange.
➢ It is a thought experiment of a world where, in every time step, each person
gives one dollar to one other person (at random) if they have any money to
give. If they have no money then they do not give out any money.
The SETUP for the model creates
500 agents, and then gives them
each 100 dollars.
HOW TO USE IT
Press SETUP to setup the model, then
press GO to watch the model
develop.
Simple Economy Model
Behind the Model?
Link
Wilensky, U. (1999). NetLogo. [Link] Center for
Connected Learning and Computer-Based Modeling, Northwestern University,
Evanston, IL.
Problems
Trivial
making breakfast,
travelling to the workplace
Sending an e-mail
withdraw money from ATM
Non-trivial
Finding optimal route for a traveling salesman
Discovering task assignment rules of workers
Analysis of gene expression with DNA microarrays
Abu Ja'far Muhammad ibn Musa Al-
Khwarizmi
MacTutor
History of Mathematics
archive,
School of Mathematics and Statistics
University of St Andrews, Scotland
[Link]
10/03/2023
Algorithms
The term algorithm originally referred to any
computation performed via a set of rules applied to
numbers written in decimal form.
Algorithm can be defined as: “A sequence of activities
to be processed for getting desired output from a given
input.”
A formula or set of steps for solving a particular
problem. To be an algorithm, a set of rules must be
unambiguous and have a clear stopping point.
[Link]
Algorithms
Getting specified output is essential after
algorithm is executed.
One will get output only if algorithm stops
after finite time.
Activities in an algorithm to be clearly
defined in other words for it to be
unambiguous.
EXAMPLES OF SIMPLE
ALGORITHMS
Operators for writing an algorithm
symbol task
+ addition
- subtraction
* multiplication
/ division
assignment
10/03/2023
Problem 1:
Find and display the area of a Circle of radius r.
Inputs to the algorithm : Radius r of the Circle.
Expected output : Area of the Circle
Algorithm:
Step 1: Start
Step 2: Read\input the Radius r of the Circle
Step 3: Area Pi*r*r % calculation of area
Step 4: Print Area
Step 5: End
10/03/2023
Problem 2
Write an algorithm to read two numbers, find and display their
sum.
Inputs to the algorithm : First num1. Second num2. .
Expected output : Sum of the two numbers
Algorithm:
Step 1: Start
Step 2: Read\input the first num1.
Step 3: Read\input the second num2.
Step 4: Sum num1 + num2 % calculation of sum
Step 5: Print Sum
Step 6: End
10/03/2023
Problem 3
Convert and display temperature Fahrenheit to Celsius Inputs to
the algorithm
Inputs to the algorithm : Temperature in Fahrenheit, F
Expected output : Temperature in Celsius, C
Algorithm:
Step 1: Start
Step 2: Read Temperature in Fahrenheit F
Step 3: C 5/9*(F-32)
Step 4: Print Temperature in Celsius: C
Step 5: End
10/03/2023
Problem 4
Algorithm
Step 1 : Start
Design an algorithm for Step 2 : Sum=0
adding the test scores as Step 3 : Get the first testscore, 26
given below: Step 4 : Add first testscore to sum, S S + 26
Step 5 : Get the second testscore, 49
26, 49, 98, 87, 62, 75 Step 6 : Add to sum, S S + 49
Step 7 : Get the third testscore, 98
Step 8 : Add to sum, S S + 98
Inputs to the algorithm :
Test Scores, 26, 49, 98, 87,
Step 9 : Get the Forth testscore, 87
62, 75
Step 10: Add to sum, S S + 87
Step 11: Get the fifth testscore, 62
Step 12: Add to sum, S S + 62
Expected output : Step 13: Get the sixth testscore, 75
sum, S
Step 14: Add to sum, S S + 75
Step 15: Output the sum, Print S
Step 16: End
10/03/2023
TYPE OF ALGORITHMS
Type of Algorithms
The algorithm and flowchart, classification to the
three types of control structures. They are:
1. Sequence
2. Branching (Selection)
The branch refers to a binary decision based on some
condition. If the condition is true, one of the two branches
is explored; if the condition is false, the other alternative
is taken.
3. Loop (Repetition)
The loop allows a statement or a sequence of statements
to be repeatedly executed based on some loop condition.
10/03/2023
Problem 1 - branching
write algorithm to find the greater number between two numbers
Inputs to the algorithm : two umbers, A, B
Expected output : greater number, C
Algorithm:
Step 1: Start
Step 2: Read/input A and B
Step 3: If A greater than B then C=A
Step 4: If B greater than A then C=B
Step 5: Print C
Step 6: End
10/03/2023
Problem 2 - branching
−𝑥, 𝑥 < 0
write algorithm to find the result of equation 𝑓 𝑥 = ቊ
𝑥, 𝑥 ≥ 0
Inputs to the algorithm : X
Expected output :F
Algorithm:
Step 1: Start
Step 2: Read/input X
Step 3: If X Less than zero then F=-X
Step 4: If X greater than or equal zero then F=X
Step 5: Print F
Step 6: End
10/03/2023
Problem 1 - loop
An algorithm to calculate even numbers between 0 and 99
Inputs to the algorithm : numbers from 0 to 99,
Expected output : even numbers in given interval,
Algorithm:
Step 1: Start
Step 2: I ← 0
Step 3: Write I in standard output (Display them in
command window)
Step 4: I ← I+2
Step 5: If (I <=98) then go to line 3
Step 6. End
10/03/2023
Problem 2 - loop
Design an algorithm which gets a natural value, n, as its input
and calculates odd numbers equal or less than n. Then write
them in the standard output (Display them in command window):
Inputs to the algorithm :n
Expected output : odd numbers from 0 to n,
Algorithm:
Step 1. Start
Step 2. Read n
Step 3. I ← 1
Step 4. Write I (Display them in command window)
Step 5. I ← I + 2
Step 6. If ( I <= n) then go to line 4
Step 7. End
10/03/2023
Problem 3 - loop
Design an algorithm which generates even numbers between 1000
and 2000 and then prints them in the standard output (Display
them in command window). It should also print total sum:
Inputs to the algorithm : numbers in [1000,2000]
Expected output : even numbers in1000 to 2000, total sum S
Algorithm:
Step 1. Start
Step 2. I ← 1000 and S ← 0
Step 3. Write I (Display them in command window)
Step 4. S ← S + I
Step 5. I ← I + 2
Step 6. If (I <= 2000) then go to line 3 else go to line 7
Step 7. Write S (Display them in command window)
Step 8. End
10/03/2023
Problem 3 - loop
Design an algorithm with a natural number, n, as its input
which calculates the following formula and writes the result in
the standard output (Display them in command window):
1 1 1
𝑆 = + +⋯+
2 4 𝑛
Inputs to the algorithm :n
Expected output : result of given formula, S
Algorithm:
1. Start
2. Read n
3. I ← 2 and S ← 0
4. S= S + 1/I
5. I←I+2
6. If (I <= n) then go to line 4 else write S in standard output
7. End
10/03/2023
PROPERTIES OF
ALGORITHM
Properties of algorithms
Finiteness:
An algorithm must always terminate after a finite number of
steps.
It means after every step one reach closer to solution of the problem
and after a finite number of steps algorithm reaches to an end point.
Definiteness:
Each step of an algorithm must be precisely defined.
It is done by well thought actions to be performed at each step of the
algorithm. Also the actions are defined unambiguously for each
activity in the algorithm.
Input:
Any operation you perform need some beginning value/quantities
associated with different activities in the operation.
So the value/quantities are given to the algorithm before it begins.
10/03/2023
Properties of algorithms
Output:
One always expects output/result (expected
value/quantities) in terms of output from an algorithm.
The result may be obtained at different stages of the algorithm.
If some result is from the intermediate stage of the operation
then it is known as intermediate result and result obtained at
the end of algorithm is known as end result. The output is
expected value/quantities always have a specified relation to the
inputs
Effectiveness:
Algorithms to be developed/written using basic operations.
Actually operations should be basic, so that even they can
in principle be done exactly and in a finite amount of time
by a person, by using paper and pencil only.
10/03/2023
Applications
You are supposed to prepare two different algorithms
(Link will be shared)
Submission due: 12.30