Chapter 1 INTRODUCTION TO COMPUTER AND …author.uthm.edu.my/uthm/www/content/lessons/4811/lecture...

24
Computer programming BTI 10202 Compiled by SIA CHEE KIONG DEPARTMENT OF MATERIAL AND DESIGN ENGINEERING FACULTY OF MECHANICAL AND MANUFACTURING ENGINEERING Lecture # 3 Chapter 1 INTRODUCTION TO COMPUTER AND PROGRAMMING

Transcript of Chapter 1 INTRODUCTION TO COMPUTER AND …author.uthm.edu.my/uthm/www/content/lessons/4811/lecture...

Page 1: Chapter 1 INTRODUCTION TO COMPUTER AND …author.uthm.edu.my/uthm/www/content/lessons/4811/lecture 3.pdf•The flow of data ... step 4 and 5 (max 5 times). (c) If more than 5 times,

Computer programming

BTI 10202

Compiled bySIA CHEE KIONG

DEPARTMENT OF MATERIAL AND DESIGN ENGINEERING

FACULTY OF MECHANICAL AND MANUFACTURING ENGINEERING

Lecture # 3

Chapter 1INTRODUCTION TO COMPUTER

AND PROGRAMMING

Page 2: Chapter 1 INTRODUCTION TO COMPUTER AND …author.uthm.edu.my/uthm/www/content/lessons/4811/lecture 3.pdf•The flow of data ... step 4 and 5 (max 5 times). (c) If more than 5 times,

INTRODUCTION TO COMPUTER AND PROGRAMMING (2 hours)

1. Introduction To Computer And Programming,2. Computer Evolution, 3. Software Development Life Cycle, 4. Top Down Design With Function Systems Structure, 5. Program Design Step And Methodology:

- Structure Chart, - Flow Chart, - Pseudo Code, - Debugging And Documentation Techniques.

2

Content

Lecture # 3C.K.Sia

Lecture # 3

Page 3: Chapter 1 INTRODUCTION TO COMPUTER AND …author.uthm.edu.my/uthm/www/content/lessons/4811/lecture 3.pdf•The flow of data ... step 4 and 5 (max 5 times). (c) If more than 5 times,

5.0 Algorithm

• The solution to any computing problem involves executing a series of actions in a specific order. A procedure for solving a problem in terms of

1. the actions to be executed, and

2. the order in which these action are to be executed

Is called an algorithm

3

C.K.SiaLecture # 3

Page 4: Chapter 1 INTRODUCTION TO COMPUTER AND …author.uthm.edu.my/uthm/www/content/lessons/4811/lecture 3.pdf•The flow of data ... step 4 and 5 (max 5 times). (c) If more than 5 times,

Algorithm

• In other word, A sequence of instructions is called an algorithm. Algorithms are a fundamental part of computing.

• There are two commonly used tools to help to document program logic (the algorithm).

• These are flowcharts and Pseudocode.

• Generally, flowcharts work well for small problems but Pseudocode is used for larger problems.

4

C.K.SiaLecture # 3

Page 5: Chapter 1 INTRODUCTION TO COMPUTER AND …author.uthm.edu.my/uthm/www/content/lessons/4811/lecture 3.pdf•The flow of data ... step 4 and 5 (max 5 times). (c) If more than 5 times,

Algorithm properties

• Algorithm are not computer program, as they cannot be executed by a computer. Properties of algorithm are;

1. There must be no ambiguity in any instruction.

2. They should not be any uncertainty about which instruction is to be executed next.

3. The description of the algorithm must be finite. An algorithm cannot be open-ended.

4. The execution of the algorithm should conclude after a finite number of steps.

5. The algorithm must be general enough to deal with any contingency.

Lecture # 3C.K.Sia

Page 6: Chapter 1 INTRODUCTION TO COMPUTER AND …author.uthm.edu.my/uthm/www/content/lessons/4811/lecture 3.pdf•The flow of data ... step 4 and 5 (max 5 times). (c) If more than 5 times,

V

6

Overall

Algorithm can be represented by

Pseudo-code Flowchart

C.K.SiaLecture # 3

Page 7: Chapter 1 INTRODUCTION TO COMPUTER AND …author.uthm.edu.my/uthm/www/content/lessons/4811/lecture 3.pdf•The flow of data ... step 4 and 5 (max 5 times). (c) If more than 5 times,

5.1 Flowchart

• Some of the common symbols used in flowcharts are shown (right);

• With flowcharting, essential steps of an algorithm are shown using the shapes.

• The flow of data between steps is indicated by arrows, or flowlines.

Terminator

Input/output

Process

Decision

Preparation

Connector

7

C.K.SiaLecture # 3

Page 8: Chapter 1 INTRODUCTION TO COMPUTER AND …author.uthm.edu.my/uthm/www/content/lessons/4811/lecture 3.pdf•The flow of data ... step 4 and 5 (max 5 times). (c) If more than 5 times,

Lecture # 3C.K.Sia

Page 9: Chapter 1 INTRODUCTION TO COMPUTER AND …author.uthm.edu.my/uthm/www/content/lessons/4811/lecture 3.pdf•The flow of data ... step 4 and 5 (max 5 times). (c) If more than 5 times,

Guidelines for preparing flowchart

1. The flowchart must be clear, neat and easy to follow. 2. The flowchart must have a logical start and finish.3. In drawing a proper flowchart , all necessary

requirements should be listed in logical order.4. The direction of the flow of a procedure should

always be from left to right or top to bottom. 5. Only one flow line should come out from a process

symbol.

Or

Lecture # 3C.K.Sia

Page 10: Chapter 1 INTRODUCTION TO COMPUTER AND …author.uthm.edu.my/uthm/www/content/lessons/4811/lecture 3.pdf•The flow of data ... step 4 and 5 (max 5 times). (c) If more than 5 times,

6. Only one flow line should enter a decision symbol. However, two or three flow line (one for each possible answer) may leave the decision symbol.

7. Only one flow line used with terminal symbol

Guidelines for preparing flowchart(continue)

< 0 > 0

= 0

Or

Lecture # 3C.K.Sia

Page 11: Chapter 1 INTRODUCTION TO COMPUTER AND …author.uthm.edu.my/uthm/www/content/lessons/4811/lecture 3.pdf•The flow of data ... step 4 and 5 (max 5 times). (c) If more than 5 times,

8. Within standard symbols, write briefly. If necessary, use the annotation symbol to describe data or process more clearly.

9. In case of complex flowchart, connector symbols are use to reduce the number of flow lines.

10. Intersection of flow lines should avoided to make it a more effective and better way of representing communication.

11. It is useful to test the validity of the flowchart by passing through it with normal/unusual test data.

Guidelines for preparing flowchart(continue)

SUM = A+BThis process will add two numbers and store the value in SUM

Lecture # 3C.K.Sia

Page 12: Chapter 1 INTRODUCTION TO COMPUTER AND …author.uthm.edu.my/uthm/www/content/lessons/4811/lecture 3.pdf•The flow of data ... step 4 and 5 (max 5 times). (c) If more than 5 times,

Algorithm/ flowchart (start the car engine)

1. Start2. Insert the key3. Free gear4. Turn the key – (start)5. (a) If the engine start within

6 second, release the key to the ignition position. (b) If not start, repeat the

step 4 and 5 (max 5 times).(c) If more than 5 times, call

mechanics.6. Finish

12

Start

Insert key

Free gear

Turn the key

Release the key to ignition position

Call mechanics

Finish

Engine started within 6 second

> 5 times

N

N

Y Y

C.K.SiaLecture # 3

Page 13: Chapter 1 INTRODUCTION TO COMPUTER AND …author.uthm.edu.my/uthm/www/content/lessons/4811/lecture 3.pdf•The flow of data ... step 4 and 5 (max 5 times). (c) If more than 5 times,

Algorithm / flowchart (To library)

1. Start

2. Exit the room

3. If use bus, go to the nearest bus stand, wait the arrival of the bus and take bus to University.

If use own vehicle , drive/ ride to the university.

If not, walk to the university.

13

Start

Exit the room

Take bus ?

Use own vehicle?

Walk to uni.

To bus stand

wait

Take bus

Drive/ride to uni.

1

N

Y

N

Y

C.K.SiaLecture # 3

Page 14: Chapter 1 INTRODUCTION TO COMPUTER AND …author.uthm.edu.my/uthm/www/content/lessons/4811/lecture 3.pdf•The flow of data ... step 4 and 5 (max 5 times). (c) If more than 5 times,

4. Destination in library 5. If take lift

a. Push the lift buttonb. wait the door openc. if the door open,

i. enter and push the lift button

ii. Wait until reach the selected floor, exit if not wait.

if not, use stair6. Arrived 7. Finish

14

Use own

vehicle?

Destination in library.

1

Use stair

finish

Push the lift button

Wait the door open

Lift door

open?

Enter and push the lift

button

Reach the

selected floor

wait

Exit the lift

Arrived

N

Y

N Y

N

Y

Continue …….

C.K.SiaLecture # 3

Page 15: Chapter 1 INTRODUCTION TO COMPUTER AND …author.uthm.edu.my/uthm/www/content/lessons/4811/lecture 3.pdf•The flow of data ... step 4 and 5 (max 5 times). (c) If more than 5 times,

Benefit of flowcharts

1. Makes logic clear

2. Communication

3. Effective analysis

4. Useful in coding

5. Proper testing and debugging

6. Appropriate documentation

Lecture # 3C.K.Sia

Page 16: Chapter 1 INTRODUCTION TO COMPUTER AND …author.uthm.edu.my/uthm/www/content/lessons/4811/lecture 3.pdf•The flow of data ... step 4 and 5 (max 5 times). (c) If more than 5 times,

Limitation of flowcharts

1. Complex; the major disadvantages in using flowcharts may continue for many pages, making them hard to follow. Flowchart tend to get large very quickly and it is difficult to follow the represented process. It is also very laborious to draw a flowchart for a larger program.

2. Costly; drawing a flowcharts a viable only if the problem solving logic is straight forward and not very lengthy. However, if flowcharts are drawn for a huge applications, the time and cost factor of program development may get out of proportional, making a costly affair.

3. Difficult to modify; due to its symbolic nature, any changes or modification to a flowchart usually require redrawing the entire logic again, and redrawing a complex flowchart is not a simple task. It is not easy to draw thousand of flow line and symbols along with proper spacing, especially for a large complex program.

4. No update; usually program are updated regularly. However, the corresponding the update of the flowchart may not take place, especially in a case of large programs. As a results, the logic used in the flowchart may not match with the actual program’s logic. The inconsistency in flowchart update defeats the main purpose of the flowcharts, that is, to give the user the basic idea about the program’s logic.

Lecture # 3C.K.Sia

Page 17: Chapter 1 INTRODUCTION TO COMPUTER AND …author.uthm.edu.my/uthm/www/content/lessons/4811/lecture 3.pdf•The flow of data ... step 4 and 5 (max 5 times). (c) If more than 5 times,

5.2 Pseudocode (pronounced soo-doh-kohd)

• Pseudocode is an artificial and informal language that helps you develop algorithms.

• Pseudocode is a "text-based" detail (algorithmic) design tool.

• The rules of Pseudocode are reasonably straightforward. All statements showing "dependency" are to be indented. These include while, do, for, if, switch.

17

C.K.SiaLecture # 3

Page 18: Chapter 1 INTRODUCTION TO COMPUTER AND …author.uthm.edu.my/uthm/www/content/lessons/4811/lecture 3.pdf•The flow of data ... step 4 and 5 (max 5 times). (c) If more than 5 times,

1. Initialize total to zero2. Initialize counter to zero3. .4. Input the first grade5. While the user has not as yet entered the sentinel6. Add this grade into the running total7. Add one to the grade counter8. Input the next grade (possibly the sentinel)9. .10. If the counter is not equal to zero11. Set the average to the total divided by the counter12. Print the average13. Else14. Print “ No grade were entered)

18

Example -Pseudocode algorithm that uses sentinel-controlled repetition to solve the class average problem

C.K.SiaLecture # 3

Page 19: Chapter 1 INTRODUCTION TO COMPUTER AND …author.uthm.edu.my/uthm/www/content/lessons/4811/lecture 3.pdf•The flow of data ... step 4 and 5 (max 5 times). (c) If more than 5 times,

Pseudocode Guideline

Writing pseudocode is not difficult task. Even if you not know anything about the computer of computer language, you still can develop effective and efficient pseudocodes, if you write in an organized manner. Although there are no established standard for pseudocode construction, here are a few guidelines for developing pseudocodes.

1. Statement must be written in simple english (or any preferable language) and should be language independent. Remember that pseudocode only describe the logic plan to develop a program, it is not programming.

2. Steps must be understandable, and when the steps (instruction) are followed, they must produce a solution to a specified problem. If the pseudocode is difficult for a person to read or translate into code, then something is wrong with the level of detail you have chosen to use.

Lecture # 3C.K.Sia

Page 20: Chapter 1 INTRODUCTION TO COMPUTER AND …author.uthm.edu.my/uthm/www/content/lessons/4811/lecture 3.pdf•The flow of data ... step 4 and 5 (max 5 times). (c) If more than 5 times,

Pseudocode Guideline (continue)

3. Pseudocode should be concise.

4. Each instruction should be written in a separate line and each statement in pseudocode should express just one action for computer. If the task list is properly drawn, then in most cases each talk will correspond to one line of pseudocode.

5. Capitalise keywords such as READ, PRINT and so on.

6. Each set of instruction is written from top to bottom , with only one entry and one exit.

7. It should allow for easy transition from design to implementation in programming language.

Lecture # 3C.K.Sia

Page 21: Chapter 1 INTRODUCTION TO COMPUTER AND …author.uthm.edu.my/uthm/www/content/lessons/4811/lecture 3.pdf•The flow of data ... step 4 and 5 (max 5 times). (c) If more than 5 times,

Benefits of pseudocode

1. Since it language independent, it can be use by most programmers. It allows the developer to express the design in plan natural language.

2. It is easier to develop a program from a pseudocodethan with a flowchart. Programmer do not have to think about syntaxes; they simply have to concentrate on the underlying logic. The focus is on the steps to solve a problem rather than on how to use the computer language.

3. Often, it is easy to translate pseudocode into a programming language, a step which can be accomplished by less experienced programmers.

Lecture # 3C.K.Sia

Page 22: Chapter 1 INTRODUCTION TO COMPUTER AND …author.uthm.edu.my/uthm/www/content/lessons/4811/lecture 3.pdf•The flow of data ... step 4 and 5 (max 5 times). (c) If more than 5 times,

• The use of word and phrases in pseudocode, which are in line with basic computer operations, simplifies the translation from the pseudocodealgorithm to a specific programming language.

• Unlike flowchart, pseudocode is compact and does to tend to run over many pages. Its simple structure and readability make it easier to modify.

• Pseudocode allow programmers who work in different computer languages to talk to each other. They can be reviewed by groups more easily than real code.

Benefits of pseudocode (continue)

Lecture # 3C.K.Sia

Page 23: Chapter 1 INTRODUCTION TO COMPUTER AND …author.uthm.edu.my/uthm/www/content/lessons/4811/lecture 3.pdf•The flow of data ... step 4 and 5 (max 5 times). (c) If more than 5 times,

Although pseudocode is a very simple mechanism to simplify problem-solving logic, it has it limitations. Some of the notable limitation are;

1. the main disadvantages of using pseudocode is that it does not provide visual representation of the program’s logic.

2. They are no accepted standards for writing pseudocodes. Programmer use their own style of writing pseudocode.

3. Pseudocode cannot be compiled nor executed, and there are no real formatting or syntax rules . It is simply one step, an important one, in producing the final code.

Limitations of pseudocode

Lecture # 3C.K.Sia

Page 24: Chapter 1 INTRODUCTION TO COMPUTER AND …author.uthm.edu.my/uthm/www/content/lessons/4811/lecture 3.pdf•The flow of data ... step 4 and 5 (max 5 times). (c) If more than 5 times,

Flowchart and pseudocode in control structure.

Selection control structure.

Repetition control structure.

Sequence control structure.

Lecture # 3C.K.Sia