algorithms - introduction to computer programming

81
Computer Programming Day 1 - Ijasali Manalody

Category:

Technology


4 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Algorithms - Introduction to computer programming

Computer ProgrammingDay 1

- Ijasali Manalody

Page 2: Algorithms - Introduction to computer programming

Relevance of computer Programming

• To automate tasks done by Humans

Hard Work

Difficult to generate reports

Delay in handling bulk

data

Easy to Maintain

Single click for reports

Can handle any amount

of data

Page 3: Algorithms - Introduction to computer programming

What is programming?

• Series of instructions to a computer to accomplish a task;– Same like instructions given to human to

perform a task• Eg : Go straight , Take right etc

• Group of instructions will make a program to perform that particular taskGroup of instructions to make Tea is,

Instruction 1 : Boil WaterInstruction 2 : Put Tea powder

Instruction 3 : Put Sugar

Group of instructions make a program

Page 4: Algorithms - Introduction to computer programming

How to give instructions to computer?

• Instructions must be written in a way the computer can understand

漢鼎 31 繁 81 印篆

?????

Add 31 and 81

????

Page 5: Algorithms - Introduction to computer programming

Language of Computer

• Computer only understands Machine language which is of 0 and 1

• So any instructions to computer must be of Machine Language

Page 6: Algorithms - Introduction to computer programming

Mother Board

How does a Computer program get executed?

Instruction Address11011110 00110000

00010010 00110001

10000000 00001010

01001000 10000001

00001100 10000100

11000001 00101011

01011011 01011001

11101011 11111000

Ram

Loads instruction for execution

Some instruction (eg: ADD 2 + 3)

CPU

Page 7: Algorithms - Introduction to computer programming

Ram

Example

Machine Instruction Machine Operation

0000 0000 Stop

0000 0001 Rotate bristles left

0000 0010 Rotate bristles right

0000 0100 Go back to start of program

• Let us say that an electric toothbrush has a processor and main memory. • The processor can rotate the bristles left and right, and can check the on/off switch

Address

Machine Instructions

0 0000 0001

1 0000 0010

2 0000 0100

3

4

Page 8: Algorithms - Introduction to computer programming

Types of Languages

• Machine level Languages• Machine language programs were made

up of instructions written in binary code. • This is the “native” language of the

computer.

• Assembly Level Languages• Assembly language programs are made

up of instructions written in mnemonics.(Uses convenient alphabetic abbreviations to represent instructions)

• High Level Languages• Use statements that resemble English

phrases combined with mathematical terms needed to express the problem or task being programmed.

Page 9: Algorithms - Introduction to computer programming

“All programs must be translated to Machine language before their instructions can be executed ”

Page 10: Algorithms - Introduction to computer programming

Assembler

• Assembled languages: – Assembler: a program used to

translate Assembly language programs.– Produces one line of binary code per

original program statement.– The entire program is assembled before

the program is sent to the computer for execution.

......clc

add ax,bx mov di,

offset result.....

Assembler

......0101010101010101

......

Page 11: Algorithms - Introduction to computer programming

Compiler– Compiler is a program used to

translate high-level programs into Machine level program.

– It translates the entire program into binary code before anything is sent to the CPU for execution.

......c=a+b ;Print a;

......

Compiler

......0101010101010101

......

Page 12: Algorithms - Introduction to computer programming

Interpreter• Interpreted Languages:– Interpreter: A program used to

translate high-level programs.– Translates one line of the program into

binary code at a time:• An instruction is fetched from the original

source code.• The Interpreter checks the single instruction

for errors. (If an error is found, translation and execution ceases. Otherwise…)• The instruction is translated into binary

code.• The binary coded instruction is executed.• The fetch and execute process repeats for

the entire program.

......c=a+b ;Print a;

......

Interpretor

......0101010101010101

......

Page 13: Algorithms - Introduction to computer programming

Compiler Vs Interpreter• Takes Entire Program as input

• Intermediate object code is generated

• Program need not be compiled every time we run the program

• Errors are displayed after entire program is checked

• Example : C compiler

• Takes Single instruction as input

• No intermediate object code is generated

• Every time we run the program it will be converted to low level program(interpreted)

• Errors are displayed after every instruction interpreted

• Example : BASIC, PHP interpreter

Page 14: Algorithms - Introduction to computer programming

Programming languages Vs Scripting Languages

• Some programming languages (like Java or C++) require the code to be compiled (translated to binary) before it can be started.

• Others (like JavaScript,PHP) are interpreted, meaning that each command is translated separately when the program is started.

Page 15: Algorithms - Introduction to computer programming

How to write a program?

Step 1: Decide what steps are needed to complete the task !

Step 2: Write the steps as Algorithms(written in English) or as a flowchart (graphic symbols)

Step 3: Translate into the programming language

Step 4: Try out the program and “debug” it (fix if necessary)

Page 16: Algorithms - Introduction to computer programming

What is Algorithms?

• List of steps written in English• Like the instructions for a recipe• Must be in the right sequence– Imagine saying “put the tea powder”

and then “take water and boil”

Page 17: Algorithms - Introduction to computer programming

Sample Algorithm

• Task: add two numbers• Algorithm:– Start – Get two numbers– Add them– Print the answer– End

Page 18: Algorithms - Introduction to computer programming

What does a flowchart look like?

• The algorithm from the previous slide would look like this as a flowchart:Start

Get 2 numbers

Add them

Print answer

End

Page 19: Algorithms - Introduction to computer programming

What are those symbols?

• START/END

• INPUT/OUTPUT

• PROCESS

• DECISION

Page 20: Algorithms - Introduction to computer programming

What are those symbols?

• START/END• Used at the beginning and end of

each flowchart.

Page 21: Algorithms - Introduction to computer programming

What are those symbols?

• INPUT/OUTPUT• Shows when information/data

comes into a program or is printed out.

Page 22: Algorithms - Introduction to computer programming

What are those symbols?

• PROCESS• Used to show calculations, storing of

data in variables, and other “processes” that take place within a program.

Page 23: Algorithms - Introduction to computer programming

What are those symbols?

• DECISION• Used to show that the program must

decide whether something (usually a comparison between numbers) is true or false. YES and NO (or T/F) branches are usually shown.

Y

N

X>7?

Page 24: Algorithms - Introduction to computer programming

Another Sample: Calculating Age

• Algorithm:– Start – Get year born– Calculate age– Print age– If age > 50 print OLD– End

Page 25: Algorithms - Introduction to computer programming

Another Sample: Calculating Age

Get yr

Calc age

Print age

Age>50?OLD Y

N

Start

End

Page 26: Algorithms - Introduction to computer programming

Questions?“A good question deserve a

good grade…”

Page 27: Algorithms - Introduction to computer programming

Self Check !!

Page 28: Algorithms - Introduction to computer programming

Self-Check

• A computer program is…

– A series of instructions to accomplish something

–Written in High level language–Written in machine language– All of the above

Page 29: Algorithms - Introduction to computer programming

Self-Check

• A computer program is…

– A series of instructions to accomplish something

–Written in High level language–Written in machine language– All of the above

Page 30: Algorithms - Introduction to computer programming

Self-Check

• To “compile” a program means to…

– Translate it into mnemonics– Translate it into binary code– Executes the program– Run the program instruction

Page 31: Algorithms - Introduction to computer programming

• To “compile” a program means to…

– Translate it into mnemonics– Translate it into binary code– Executes the program– Run the program instruction

Self-Check

Page 32: Algorithms - Introduction to computer programming

Self-Check

• Algorithm is…

– The program as it is written in a programming language

– The results of a program that makes secret codes

– The logic of a program written in English– The logic of a program shown in a chart

Page 33: Algorithms - Introduction to computer programming

• Algorithm is…

– The program as it is written in a programming language

– The results of a program that makes secret codes

– The logic of a program written in English– The logic of a program shown in a chart

Self-Check

Page 34: Algorithms - Introduction to computer programming

Self-Check

• The flowchart symbol to perform a calculation is…

Page 35: Algorithms - Introduction to computer programming

Self-Check

• The flowchart symbol to perform a calculation is…

Page 36: Algorithms - Introduction to computer programming

Self-Check

• The flowchart symbol to perform a Decision is…

Page 37: Algorithms - Introduction to computer programming

• The flowchart symbol to perform a Decision is…

Self-Check

Page 38: Algorithms - Introduction to computer programming

Self-Check

• Look at the flowchart section below. If the variable X is 5, what will print (K or 1st)?

X > 5?YN

Print “1st”Print “K”

Page 39: Algorithms - Introduction to computer programming

Self-Check

• Look at the flowchart section below. If the variable X is 5, what will print (K or 1st)?

X > 5?YN

Print “1st”Print “K”

K will be printed. The answer to the question “Is X greater than 5?” is NO, since X is equal to (not greater than) 5.

Page 40: Algorithms - Introduction to computer programming

Self-Check

• Choose the correct flowchart symbol for each of these statements.

• AGE>65?

• Calc. Tax

• START

• Print NAME

Page 41: Algorithms - Introduction to computer programming

Self-Check

• Choose the correct flowchart symbol for each of these statements.

• AGE>65?

• Calc. Tax

• START

• Print NAME

Page 42: Algorithms - Introduction to computer programming

“End of Day 1”

Page 43: Algorithms - Introduction to computer programming

Check list Day1

• Relevance of Computer programming

• Instructions are loaded into ____• Instructions are executed by

____• Assembly level Language Vs

Machine level language Vs High level language

• Assembler Vs compiler Vs interpreter

Page 44: Algorithms - Introduction to computer programming

Elements of a ProgramDay 2

Page 45: Algorithms - Introduction to computer programming

How does human perform simple task; for Eg: add 456 and 44

Add 456 and 44 500

Page 46: Algorithms - Introduction to computer programming

How does human perform simple task; Eg: add 456 and 44

1 We Hear it through our input senses

2 We store the numbers 456 and 44 in our memory

456

44

456+44 3 We calculate the result in our brain and store it in memory

5003 We say the answer through our output senses

Page 47: Algorithms - Introduction to computer programming

1 Computer use keyboard to receive inputs

2 Computer store the numbers 456 and 44 in Ram

456

44

456+44

3Computer calculate the result in CPU (ALU within CPU) and stores result back in ram

5004 Computer use monitor to display outputs

How does computer perform simple task; Eg: add 456 and 44

Page 48: Algorithms - Introduction to computer programming

Elements of a Program

• During programming we may need to do the following

– Stores Data temporarily

– Control the normal flow of instruction execution

– Repeat one or more instructions several time

– Repeat group of instructions at several parts of program

Page 49: Algorithms - Introduction to computer programming

Elements of a Program

• During programming we may need come across following situations

– Stores Data temporarily - Variables

– Control the normal flow of instruction execution - Decisions

– Repeat one or more instructions several time - Loops

– Repeat particular instructions at several parts of program - Functions

Page 50: Algorithms - Introduction to computer programming

Variables

Page 51: Algorithms - Introduction to computer programming

N Y

Start

i=1

If i<100

Print iStop

Print all the numbers up to 100

i=i+1

Page 52: Algorithms - Introduction to computer programming

N Y

Start

i=1

If i<100

Print iStop

Print all the numbers upto 100

i=i+1

1i

Ram

Page 53: Algorithms - Introduction to computer programming

• Variables are part of almost every program.

• A variable is a “place to put data” and is usually represented by a letter or a word. (Think of a variable as a Tupperware container with a label on it.)

• Variable names cannot contain spaces.

RAM

Variables

A B c

Page 54: Algorithms - Introduction to computer programming

Variables

• The most common way to put information into a variable is to use the equal sign (=). • Eg a=10;

• C = A + 7 means take the value of A, add 7, and put it into C.

• COUNT=COUNT + 2 means take the current value of COUNT, add 2 to it, and make it the new value of COUNT.

Page 55: Algorithms - Introduction to computer programming

Variables

• Sometimes you must specify the type of data that will be placed in a variable.

• Here are some examples of data types:– String (text, “strings of letters”)– Integer (whole numbers)– Float(decimal numbers)– Boolean (true/false)

Page 56: Algorithms - Introduction to computer programming

Variables

• Variables may be classified as global or local.

• A global variable is one that can be shared by all parts of a program, including any functions or sub-programs.

• A local variable is one that is used only within a certain part of the program, for example, only in one function or sub-program.

Page 57: Algorithms - Introduction to computer programming

Decisions

Page 58: Algorithms - Introduction to computer programming

N Y

Start

i=1

If i<100

Print iStop

Print all the numbers up to 100

i=i+1

Page 59: Algorithms - Introduction to computer programming

Decisions

• A program often needs to decide whether something is true or false in order to see which way to continue.

• Programs often use IF (or IF or IF ELSE) statements to show a decision.

Page 60: Algorithms - Introduction to computer programming

Decisions

• An IF statement always has a condition to check, often a comparison between a variable and a number.

• The IF statement also must specify what to do if the condition/comparison is true.

Page 61: Algorithms - Introduction to computer programming

Decisions

• In an IF statement, when the condition is false, the program simply ignores the commands within IF and continues to the next line.

• In an IF ELSE statement, commands are given for both the true and false conditions.

Page 62: Algorithms - Introduction to computer programming

Loops

Page 63: Algorithms - Introduction to computer programming

N Y

Start

i=1

If i<100

Print iStop

Print all the numbers up to 100

i=i+1

Page 64: Algorithms - Introduction to computer programming

Loops

• A loop is a repetition of all or part of the commands in a program.

• A loop often has a counter (a variable) and continues to repeat a specified number of times.

• A loop may also continue until a certain condition is met (e.g., until the end of a file or until a number reaches a set limit)

Page 65: Algorithms - Introduction to computer programming

Functions

Page 66: Algorithms - Introduction to computer programming

Print all the numbers up to 100

N Y

Start

i=1If i<100

Print iStop

i=i+1

Start

STOP

Page 67: Algorithms - Introduction to computer programming

Functions

• In most programming languages, small sub-programs are used to perform some of the tasks.

• These may be called functions, subroutines, handlers, or other such terms.

• Functions often have names (e.g., getName or CALCTAX).

Page 68: Algorithms - Introduction to computer programming

Functions

• A function generally gets information from the main program, performs some task, and returns information back to the program.

• Functions follow the same rules of syntax, etc. as the main program.

Page 69: Algorithms - Introduction to computer programming

Hints for Writing Code

• “Code” means writing the program in the appropriate language

• Be sure the code is exact (spelling, capitals/lower case, punctuation, etc).

• Write part of the code, try it, then write more.

Page 70: Algorithms - Introduction to computer programming

Debugging

• To “debug” means to try a program, then fix any mistakes.

• Virtually no program works the first time you run it. There are just too many places to make errors.

• When you are debugging a program, look for spelling and punctuation errors.

• Fix one error at a time, then try the program again.

Page 71: Algorithms - Introduction to computer programming

Questions?“A good question deserve a

good grade…”

Page 72: Algorithms - Introduction to computer programming

Self Check !!

Page 73: Algorithms - Introduction to computer programming

Self-Check

• A function in a program is…

– Something from trigonometry, like COSINE

– A sub-program, usually performing one task

– A way to check the accuracy of a program (a “function check”)

Page 74: Algorithms - Introduction to computer programming

• A function in a program is…

– Something that stores a value– A sub-program, usually performing one

task– A way to check the accuracy of a

program (a “function check”)

Self-Check

Page 75: Algorithms - Introduction to computer programming

Self-Check

• A variable in a program is…

– A letter or word that represents a place to store data

– A decision made within a program– A small sub-program used to find errors

Page 76: Algorithms - Introduction to computer programming

Self-Check

• A variable in a program is…

– A letter or word that represents a place to store data

– A decision made within a program– A small sub-program used to find errors

Page 77: Algorithms - Introduction to computer programming

• A global variable in a program is…

– Store data Permanently– Store Data that can not be accessed

from functions– Store data than can be accessed from

anywhere in the program

Self-Check

Page 78: Algorithms - Introduction to computer programming

• A global variable in a program is…

– Store data Permanently– Store Data that can not be accessed

from functions– Store data than can be accessed from

anywhere in the program

Self-Check

Page 79: Algorithms - Introduction to computer programming

• A loop is used in a program when…

– One or more instructions repeats at several part of the program

– You want to skip particular instructions– You want to repeat particular

instructions until a condition to satisfy– All of the above

Self-Check

Page 80: Algorithms - Introduction to computer programming

• A loop is used in a program when…

– One or more instructions repeats at several part of the program

– You want to skip particular instructions– You want to repeat particular

instructions until a condition to satisfy– All of the above

Self-Check

Page 81: Algorithms - Introduction to computer programming

“End of Day 2”