1 ICS 2175 Lecture 2 Intro To Algo To Use1
1 ICS 2175 Lecture 2 Intro To Algo To Use1
Methodology
Fred Omondi
fred@kca.ac.ke
Course Objective
• Assessment
– The continuous assessment test will carry a total of 30%, and
will be examined as follows:
– CAT One will account for 10%
– Written assignment will account for 10%
– Programming Project will account for 10%
– The University final Examination will account for 70%
Content
• A computer is made up of
– Hardware - the actual physical equipment
• Hardware has five distinct components
– Software -a collection of programs (computer
instructions) which allow the hardware to function.
• There are two types of software.
Introduction to Computers
Hardware components
• CPU – the brain of the computer, the CPU (central
processing unit) performs all the instructions
• Primary Storage – also known as Main Memory, used
by the computer to store information and instructions
while the machine is operating.
• Secondary Storage –used by the computer to store
information long term. Powering off a computer does
not affect data in Secondary storage. Example hard
drive
• Input Devices –allow users to enter data into the
computer. The most common are a keyboard and mouse
• Output Devices – output devices allow computer to
pass information to the user. Common ones include the
printer and monitor.
Introduction to Computers
H/W components
Introduction to Computers
H/W components
Introduction to Computers
Software types
• System Software – system software helps
the system run. System software includes
Operating Systems, System Support Software
and System Development Software
Start Stop
Input/ Output
Read Write
Algorithms …
Process box
Use not more than two statements in each box.
Sum:= Sum + 1;
Count:= Count + 1;
ALGORITHMS …
Decision/selection box
ALGORITHMS …
Direction line (next step)
Start:
DO WHILE:
Process;
Do exit action;
ENDWHILE:
Algorithms …
REPETITION
These are steps that must be performed more than once.
Start:
REPEAT:
Process
Until condition adjustment
UNTIL condition holds
Algorithms …
DECISION/ SELECTION
IF condition holds
THEN perform Process
IF condition THEN
…. ELSE …
Algorithms
EXAMPLES OF ALGORITHMS
Algorithm to reads in, displays and exchanges integer values of
two variables.
Say Variable_A = 500 while Variable_B = 300.
Hint use temporary.
1. Start
2. Set up variables
3. Initialize variables
4. Read in value of Variable_A
5. Read in value of Variable_B
6. Display value of Variable_A
7. Display value of Variable_B
8. Copy value of Variable_A into Temporary
9. Copy value of Variable_B into
Variable_A
10.Copy value of Temporary into Variable_B
11.Display value of Variable_A
12.Display value of Variable_B
13.Stop
Algorithms
(Algorithm that reads in, displays and exchanges integer values of two
variables.)
Check if the algorithm works
Algorithms
Algorithm that reads in, displays and exchanges integer values of two
variables.
Pseudo Code
START
VARIABLES Variable_A, Temporary, Variable_B
Variable_A:=0; Temporary:=0; Variable_B:=0;
READ Variable_A;
READ Variable_B;
WRITE Variable_A;
WRITE Variable_B;
Temporary:= Variable_A;
Variable_A:=Variable_B;
Variable_B:=Temporary;
WRITE Variable_A;
WRITE Variable_B;
STOP.
Start
Algorithms
Algorithm That reads in,
displays and exchanges Variables Variable_A:=0
integer values of two :
Variable_A
Variable_B:=0
variables. Variable_B
Temporary
Display
Variable_A
Display Temporary := Varible_A :=
Variable_B Varibale_A Varible_B;
Display
Variable_A Varibale_B :=
Display Temporary
Variable_B
Stop
Algorithms
An Algorithm That Reads & Displays A Set Of 6 Numbers
Planning
Track the number of times reading is done by setting up a
counter.
When 6 numbers have been read, stop.
Algorithm
1. Start
2. Setup variables: Number, Count
3. Initialize variables: Number:=0; Count:=0;
4. Read Number
5. Display Number
6. Increase Count by 1
7. If Count<6 Repeat from 4
8. Stop
Algorithms
An Algorithm That Reads & Displays A Set Of 6 Numbers
Pseudo Code
START
Variables: Number, Count
Number:=0;
Count:=0;
REPEAT
Read Number
Display Number
Increase Count by 1
UNTIL Count>6
STOP
Algorithms
An Algorithm That Reads & Displays A Set Of 6 Numbers
START
Variables: Number, Count
Number:=0;
Count:=0;
DO WHILE Count<6
Read Number;
Display Number;
Count := Count + 1;
ENDWHILE
STOP
Algorithms
An Algorithm That Reads
& Displays A Set Of 6
Numbers
Flow Chart
Algorithms
An Algorithm To Compute The Sum Of N Numbers
Let the numbers be 5,3,4,5,10,30 but they could be any numbers
ALGORITHM
1. Start
2. Set up variables: Sum, Count, Number, Number_of_items
3. Initialize variables ( Sum:=0; Count:=0; Number:=0)
4. Read Number_of_items
5. Read a Number
6. Add 1 to count
7. Add Number to Sum
8. If Count <Number_of_items then repeat from 5
9. Display Sum
10. Stop
Algorithms
An Algorithm To Compute The Sum Of N Numbers
Algorithms
An Algorithm To
Compute The Sum Of N
Numbers
Flowchart
Algorithms
An Algorithm To Compute The Sum Of N Numbers
Pseudo code
1. START
2. Setup variables: Sum, Count, Number, Number_of_items
3. Initialize Variables to 0
4. READ Number_of_items
5. REPEAT
6. READ Number
7. Add 1 to Count
8. Add Number to Sum
9. UNTIL Count> Number_of_items
10. DISPLAY Sum
11. STOP
Algorithms
Algorithm For Computing The Factorial Of A Number Less Than
12
Planning:
0!=1
1!=1
2!=1x2
3!=1x2x3
4!=1x2x3x4
10!=1x…x9x10
n!=1x…x(n-1)xn
Algorithms
Algorithm For Computing The Factorial Of A Number Less
Than 12
Algorithm
1. Start
2. Setup variables: Count, Number, Factorial
3. Initialize variables: Count:=0;Number:=0;Factorial:=1;
4. Read a Number
5. Check exceptions: Number<0(advice and ask again);
Number>12(advice and ask again), Number:=0 or 1 (Factorial:=1);
6. If Number<2 then proceed to 10
7. Add 1 to Count
8. Factorial := Factorial * Count
9. If Count<Number Repeat from 7
10. Display Factorial
11. Stop
Algorithms
Algorithm For Computing The Factorial Of A Number
Less Than 12
Algorithms
Algorithm For Computing The Factorial Of A Number
Less Than 12
flowchart
Algorithms
Pseudocode for the factorial computation algorithm
1. START
2. Setup variables: Count, Number, Factorial
3. Initialize variables: Count:=0; Number:=-10; Factorial:=1;
4. DO WHILE (Number <0 or Number>12)
5. PROMPT ‘Enter a number between 0 and 12’
6. READ Number
7. If Number <0 or Number>12 WRITE ‘Number should be 0..12’
8. ENDWHILE
9. DO WHILE Number>1 and Count<Number
10. Count := Count + 1
11. Factorial := Factorial * Count
12. ENDWHILE
13. WRITE Factorial
14. STOP
Introduction
Arrays
• REAL_NUMBERS
VALUE 5. -19.9 30.0 200.8 10.90 7.60
1
INDEX 1 2 3 4 5 6
•
ADDRESS VALUE Box 50, Box 30, Box 24, Kisumu Box 7, Mombasa
Nyeri Nakuru
INDEX 1 2 3 4
Introduction
Operations on Arrays
INDEX 1 2 3 4 5 6
• Processing steps
• Set index to the first cell value index
• Make the first cell value both the maximum and the
minimum
• Repeat
– Move to the next cell by increasing the index
– Take a cell value
– Examine if it is maximum and set accordingly
– Examine if it is minimum and set accordingly
• Until All values are examined
Introduction
Examples of algorithms that use Arrays
Algorithm (above)
1. Start
2. Set variables: Maximum, Minimum, Index, Size; Assume: int
INT_NUMBERS[6]
3. Initialize variables: Maximum=0; Minimum=0; Index=1,
Size=0
4. Maximum= INT_NUMBERS[1]
5. Minimum= INT_NUMBERS[1]
6. Size=6
7. Index=Index+1
8. IF INT_NUMBERS[Index]> Maximum THEN Maximum=
INT_NUMBERS[Index]
9. IF INT_NUMBERS[Index]< Minimum THEN Minimum=
INT_NUMBERS[Index]
10. IF Index<Size THEN repeat from 7
11. Display Maximum, Minimum