21
Chapter 2: Representation of Algorithms IT 12 | Fundamentals of Programming and Database Theory and Applications Ms. Jennifer O. Calleja | IT Instructor

Chapter 2 representation of algorithms

Embed Size (px)

Citation preview

Page 1: Chapter 2 representation of algorithms

Ms. Jennifer O. Calleja | IT Instructor

Chapter 2: Representation of AlgorithmsIT 12 | Fundamentals of Programming and Database Theory and Applications

Page 2: Chapter 2 representation of algorithms

Ms. Jennifer O. Calleja | IT Instructor

Objectives

•At the end of this module you should be able to:

Differentiate between high level, low level and machine language

Explain why it is necessary to translate a program written in a high level programming language into machine language

Formulate an algorithm given a problem Construct pseudocodes and flowcharts

Page 3: Chapter 2 representation of algorithms

Ms. Jennifer O. Calleja | IT Instructor

Machine Language

•Programs must be written in machine language so that the computer may be able to comprehend.

Page 4: Chapter 2 representation of algorithms

Ms. Jennifer O. Calleja | IT Instructor

Machine language• Machine language instructions are expressed as

binary numbers. A binary number is made up of just two possible digits, zero and one

• Each particular sequence encodes some particular instruction. The data that the computer manipulates is also encoded as binary numbers.

• A computer can work directly with binary numbers because switches can readily represent such numbers: Turn the switch on to represent a one; turn it off to represent a zero.

Page 5: Chapter 2 representation of algorithms

Ms. Jennifer O. Calleja | IT Instructor

Machine language•Machine language instructions are stored in

memory as patterns of switches turned on or off. •When a machine language instruction is loaded

into the CPU, all that happens is that certain switches are turned on or off in the pattern that encodes that particular instruction.

•The CPU is built to respond to this pattern by executing the instruction it encodes; it does this simply because of the way all the other switches in the CPU are wired together.

Page 6: Chapter 2 representation of algorithms

Low level Language

•A machine language or an assembly language. Low-level languages are closer to the hardware than are high-level programming languages, which are closer to human languages

SET r1, 10 ; set r1 to immediate value 10 STORE X, r1 ; store r1 into variable xLOAD r1, X ; load variable x into r1 STORE Y, r1 ; store r1 into variable y

Ms. Jennifer O. Calleja | IT Instructor

Page 7: Chapter 2 representation of algorithms

Ms. Jennifer O. Calleja | IT Instructor

High-Level Programming Language•Consists of English – like statements that

make it easier for us to write programs.•Examples :

C language Pascal FORTRAN COBOL

Page 8: Chapter 2 representation of algorithms

Ms. Jennifer O. Calleja | IT Instructor

C program that multiplies 2 numbers:

#include <stdio.h>int x,y,z; main(){

printf(“Enter two numbers: ”);scanf(“%d %d”, &x, &y);z = x*y;printf(“product = %d\n”, z);

}

Page 9: Chapter 2 representation of algorithms

Ms. Jennifer O. Calleja | IT Instructor

Translator

•Referred to computer programs that translate programs written in high level language into machine language.

TRANSLATORHigh level language program

Machine language program

Page 10: Chapter 2 representation of algorithms

Ms. Jennifer O. Calleja | IT Instructor

SAQ 2-2

•What is a machine language?

Page 11: Chapter 2 representation of algorithms

Ms. Jennifer O. Calleja | IT Instructor

ASAQ 2-1

•Machine language is the language which the computer can directly execute. The language consists of a sequence of O’s and 1’s

Page 12: Chapter 2 representation of algorithms

Ms. Jennifer O. Calleja | IT Instructor

Problem – Solving Process using computer Problem

Algorithm

Program written in a high level language

Program written in machine language

Output

analyze

program

translate

execute

Page 13: Chapter 2 representation of algorithms

Ms. Jennifer O. Calleja | IT Instructor

What is an Algorithm?

•is a solution to the problem and it is specified by the following a sequence of steps.

Page 14: Chapter 2 representation of algorithms

Ms. Jennifer O. Calleja | IT Instructor

Example: Algorithm on how to cook a hard boiled egg1. Prepare all the materials needed2. Turn on the stove3. Place the casserole into the stove4. Place the egg in the casserole5. Add water to the casserole until the egg is fully

submerged6. Heat the casserole until the water boils7. Boil the egg for 4 minutes8. Wait until the egg cooked9. Turn off the stove10.Remove egg from pan and submerge it in cold

water to arrest further cooking.

Page 15: Chapter 2 representation of algorithms

Ms. Jennifer O. Calleja | IT Instructor

SAQ 2-2

•Write an algorithm to determine the largest value among three numbers.

Page 16: Chapter 2 representation of algorithms

Ms. Jennifer O. Calleja | IT Instructor

ASAQ 2-2• There are number of possible algorithms that will correctly determine the largest. here is one:1. Obtain the first number2. Obtain the second number3. Obtain the third number4. Compare the first and second number and select

the larger value5. Compare the larger value obtained in step 4 with

the third number and again select the larger value. The value selected is the largest

6. Display the largest.

Page 17: Chapter 2 representation of algorithms

Ms. Jennifer O. Calleja | IT Instructor

Activity 2-1

•Write an algorithm that will cook a sunny side up egg.

Page 18: Chapter 2 representation of algorithms

Ms. Jennifer O. Calleja | IT Instructor

Example: Algorithm on how to cook a sunny side up egg

1. Prepare all the materials needed2. Turn on the stove3. Place the pan into the stove.4. Put a little amount of oil into the pan5. Wait until the oil is hot6. Crack the egg into the pan, put the egg shell into the

trash can.7. Put a small amount of salt into the egg yolk.8. Wait until the egg is cooked9. Turn off the stove10. Remove the egg into the pan and place it into a plate.11. Serve the sunny side up egg.

Page 19: Chapter 2 representation of algorithms

Ms. Jennifer O. Calleja | IT Instructor

Representation of algorithm

•Flowchart – a graphical representation using standardized symbols

•Pseudocode – consist of English – like phrases written in a sequence of lines.

Page 20: Chapter 2 representation of algorithms

Ms. Jennifer O. Calleja | IT Instructor

Representation of algorithm

•Flowchart – a graphical representation using standardized symbols

•Pseudocode – consist of English – like phrases written in a sequence of lines.

Page 21: Chapter 2 representation of algorithms

Ms. Jennifer O. Calleja | IT Instructor