1 I.Introduction to Algorithm and Programming Algoritma dan Pemrograman – Teknik Informatika UK...

21
1 I. Introduction to Algorithm and Programming Algoritma dan Pemrograman – Teknik Informatika UK Petra 2009

Transcript of 1 I.Introduction to Algorithm and Programming Algoritma dan Pemrograman – Teknik Informatika UK...

1

I. Introduction to Algorithm and Programming

Algoritma dan Pemrograman – Teknik Informatika UK Petra 2009

2

Computer Programming

A form of problem solving Or, more accurately, a way to solve

problems What we will be studying is “How to solve

problems” using computers

3

What’s a Problem?

A question that someone wants an answer to

A problem usually has information that represents a starting point, initial assumptions, facts, or data that will be needed in answering the question

For example, “What is my grade point average?”

4

How Do We Solve the Problem? With pencil and paper we can compute

our grade point average if we know all of our grades and the units for each of the courses taken

What are the advantages of writing a computer program to compute our grade point average?

5

How Do We Solve Problems?

Four Steps1. Understand the problem

2. Devise a plan to solve the problem

3. Implement the plan

4. Evaluate the solution

From George Polya’s book, “How to Solve It” written in 1945.

6

What is an Algorithm?

A plan for solving a problemA series of steps to follow to arrive at a solutionA highly technical and precise “recipe” for

solving a problemA “good” algorithm is “correct”, “complete”, and

“unambiguous” “Good” algorithms are also general rather than

specific

7

What Does an Algorithm Look Like?

It can be written in (precise) English/Indonesia It can be drawn as a diagram (e.g., a

flowchart) with steps and a path to follow from one step to the next

To make it precise (and computer executable) algorithms are translated into a computer understandable form (e.g., machine language)

8

A Simple Example

Problem: Calculate the average of a list of numbers.Let the number of numbers in the list be nAssume n is greater than or equal to 1Calculate the sum of the n numbersCall this sum sThen the average is given by s/n

9

Example Expanded

Problem: Calculate the average of a list of numbers. Let the number of numbers in the list be n. Assume n is greater than or equal to 1. Pick off the first number on the list and place it on the

blackboard. As long as there are more numbers on the list, pick the

next number off the list and add it to the number on the blackboard and replace the number on the blackboard with this sum. (Repeat this step until the list is empty.)

Call the number on the blackboard s. Then the average is given by s/n.

10

Final Note about Algorithms

To qualify as an algorithm, the instructions must be expressed so completely and precisely that somebody could follow the instructions without having to make any assumptions or ask for more information or help of any kind.

11

Computer Basics

Hardware and Memory Programs Programming Languages Compilers

12

Hardware and Memory

The processor or CPU (Central Processing Unit)Executes instructions

MemoryMain memory (RAM)Secondary (auxiliary) memory

Disk drives (hard drives) Diskettes (floppies) Compact disks (and DVDs) USB Flash Drives

13

Bits, Bytes, and Memory Organization A bit is a single binary digit (i.e., it can assume

the value 0 or 1) A byte is a string of 8 bits (enough to hold the

representation of a character) The number of a byte is called its “address” or

“memory location” When more than one byte is needed to store

information the computer uses a string of adjacent bytes

14

Why Do Computers Use Just Zeros and Ones? It’s easier to make a physical device that

has only two physical states (e.g., “on” and “off”).

There is nothing special about zero and one. The only important thing is that the physical device have two stable states.

15

Programs

A program is simply a set of instructions for a computer to follow.

A computer “executes” the instructions in the program to process the “data” or information provided as input in order to produce the results or output desired.

It is actually the “operating system” (just another program) running on the computer that executes your program.

16

Programming Languages

High level languages (languages designed for people to use)Pascal, FORTRAN, C, C++, Visual Basic,

Java

Low level languages (languages that computers can understand)Machine languages, assembly languages

17

Compilers

Compilers are programs that translate high level languages into low level languages

Compilers are generally machine dependent because different machines have different machine languages

The input to the compiler is called the “source code” and the output is called the “object code”

18

Testing and Debugging

A mistake in a program is called a bug. The process of removing bugs from a

program is called debugging. There are three main classes of bugs or

errors in programs:Syntax errorsRuntime errorsLogic errors

19

Syntax Errors

A syntax error is a grammatical mistake in a program.

There are strict grammatical rules for writing programs including required punctuation marks.

The compiler will catch syntax errors and give its best guess as to the problem, but is often wrong regarding the actual mistake made.

20

Run-Time Errors

Errors caught when the program is run. The computer will give an error message,

but such messages are often hard to understand.

You can be sure that there is an error in your program but it may take some time to find.

21

Logic Errors

If the program runs successfully to completion but produces the wrong result, you have a logic error.

What you have written is a valid program, but it doesn’t do what it is supposed to do.

These are the most difficult errors to find.