INTRODUCTION TO PROGRAMMING 305171 Computer Programming Rattapoom Waranusast

  • Slides: 34
Download presentation
INTRODUCTION TO PROGRAMMING 305171 Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering

INTRODUCTION TO PROGRAMMING 305171 Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University

Algorithm is a process or a set of rules to be followed in calculations

Algorithm is a process or a set of rules to be followed in calculations or other problem-solving operations. Ø The instructions should be simple and clear enough so that each step can be done without confusion. Ø 2

Ø This is an example of an algorithm for sorting cards with colors on

Ø This is an example of an algorithm for sorting cards with colors on them into piles of the same color: – Pick up all of the cards. – Pick a card from your hand look at the color of the card. – If there is already a pile of cards of that color, put this card on that pile. – If there is no pile of cards of that color, make a new pile of just this card color. – If there is still a card in your hand, go back to the second step. – If there is not still a card in your hand, then the cards are sorted, you are done. 3

4

4

5

5

Ø Ø A program is a very specific set of steps and rules. A

Ø Ø A program is a very specific set of steps and rules. A program is an implementation of an algorithm in a particular language to run on a computer (usually a particular kind of computer) – Algorithm = what we want to do – Program = what we actually did Ø The process of creating a program is called programming. Ø Programming is breaking a task down into small steps and rules. 6

Ø Ø Put these words in alphabetical order: apple, zebra, abacus Count the number

Ø Ø Put these words in alphabetical order: apple, zebra, abacus Count the number of words in these sentences: – “Programming really can be fun. ” – “John, do it again. ” – “Some people just love to type really short lines, instead of using the full width of the page. ” 7

START Ø A flowchart is a diagram that depicts the “flow” of a program.

START Ø A flowchart is a diagram that depicts the “flow” of a program. Display message “How many hours did you work? ” Read Hours Display message “How much do you get paid per hour? ” Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END 8

START Display message “How many hours did you work? ” Ø Terminals – represented

START Display message “How many hours did you work? ” Ø Terminals – represented by rounded rectangles – indicate a starting or ending point Read Hours Display message “How much do you get paid per hour? ” Terminal Read Pay Rate Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END 9

START Display message “How many hours did you work? ” Ø Input/Output Operations –

START Display message “How many hours did you work? ” Ø Input/Output Operations – represented by parallelograms – indicate an input or output operation Read Hours Display message “How much do you get paid per hour? ” Read Pay Rate Input/Output Operations Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END 10

START Display message “How many hours did you work? ” Ø Processes – represented

START Display message “How many hours did you work? ” Ø Processes – represented by rectangles – indicates a process such as a mathematical computation or variable assignment Read Hours Display message “How much do you get paid per hour? ” Read Pay Rate Process Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END 11

START Display message “How many hours did you work? ” Read Hours Display message

START Display message “How many hours did you work? ” Read Hours Display message “How much do you get paid per hour? ” Read Pay Rate Variable Contents: Hours: ? Pay Rate: ? Gross Pay: ? Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END 12

START Display message “How many hours did you work? ” How many hours did

START Display message “How many hours did you work? ” How many hours did you work? Read Hours Display message “How much do you get paid per hour? ” Read Pay Rate Variable Contents: Hours: ? Pay Rate: ? Gross Pay: ? Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END 13

START Display message “How many hours did you work? ” How many hours did

START Display message “How many hours did you work? ” How many hours did you work? 40 Read Hours Display message “How much do you get paid per hour? ” Read Pay Rate Variable Contents: Hours: 40 Pay Rate: ? Gross Pay: ? Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END 14

START Display message “How many hours did you work? ” How many hours did

START Display message “How many hours did you work? ” How many hours did you work? 40 How much do you get paid per hour? Read Hours Display message “How much do you get paid per hour? ” Read Pay Rate Variable Contents: Hours: 40 Pay Rate: ? Gross Pay: ? Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END 15

START Display message “How many hours did you work? ” How many hours did

START Display message “How many hours did you work? ” How many hours did you work? 40 How much do you get paid per hour? 300 Read Hours Display message “How much do you get paid per hour? ” Read Pay Rate Variable Contents: Hours: 40 Pay Rate: 300 Gross Pay: ? Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END 16

START Display message “How many hours did you work? ” How many hours did

START Display message “How many hours did you work? ” How many hours did you work? 40 How much do you get paid per hour? 300 Read Hours Display message “How much do you get paid per hour? ” Read Pay Rate Variable Contents: Hours: 40 Pay Rate: 300 Gross Pay: 12000 Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END 17

START Display message “How many hours did you work? ” How many hours did

START Display message “How many hours did you work? ” How many hours did you work? 40 How much do you get paid per hour? 300 12000 Read Hours Display message “How much do you get paid per hour? ” Read Pay Rate Variable Contents: Hours: 40 Pay Rate: 300 Gross Pay: 12000 Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END 18

START Display message “How many hours did you work? ” How many hours did

START Display message “How many hours did you work? ” How many hours did you work? 40 How much do you get paid per hour? 300 12000 Read Hours Display message “How much do you get paid per hour? ” Read Pay Rate Variable Contents: Hours: 40 Pay Rate: 300 Gross Pay: 12000 Multiply Hours by Pay Rate. Store result in Gross Pay. Display Gross Pay END 19

Sequence Ø Decision Ø Repetition Ø Case Ø 20

Sequence Ø Decision Ø Repetition Ø Case Ø 20

a series of actions are performed in sequence Ø The pay-calculating example was a

a series of actions are performed in sequence Ø The pay-calculating example was a sequence flowchart. Ø 21

Ø Ø One of two possible actions is taken, depending on a condition. In

Ø Ø One of two possible actions is taken, depending on a condition. In the flowchart segment below, the question “is x < y? ” is asked. If the answer is no, then process A is performed. If the answer is yes, then process B is performed. Yes No X<Y? A B 22

Ø Ø A repetition structure represents part of the program that repeats. This structure

Ø Ø A repetition structure represents part of the program that repeats. This structure is commonly known as a loop. In the flowchart segment, the question “is x < y? ” is asked. If the answer is yes, then Process A is performed. The question “is x < y? ” is asked again. Process A is repeated as long as x is less than y. When x is no longer less than y, the repetition stops and the structure is exited. x < y? YES A 23

Ø Ø One of several possible actions is taken, depending on the contents of

Ø Ø One of several possible actions is taken, depending on the contents of a variable. The structure below indicates actions to perform depending on the value in years_employed. CASE years_employed 1 2 3 bonus = 100 bonus = 200 bonus = 400 Other bonus = 800 24

Ø Ø Ø Sometimes a flowchart will not fit on one page length. A

Ø Ø Ø Sometimes a flowchart will not fit on one page length. A connector (represented by a small circle) allows you to connect two flowchart segments on the same page. The “A” connector indicates that the second flowchart segment begins where the first segment ends. START A END A 25

 • • The position of the module symbol indicates the point the module

• • The position of the module symbol indicates the point the module is executed. A separate flowchart can be constructed for the module. START calc_pay Read Input. net = rate * hour Call calc_pay function. pay = net - tax Display results. return END 26

Ø Off-page connector – Used to connect remote flowchart portion on different page Ø

Ø Off-page connector – Used to connect remote flowchart portion on different page Ø Comment – Used to add description Ø Put any Comments Here. Document – Used to represent a print out Ø Database – Used to represent a database 27

Ø Pseudocode is a compact and informal highlevel description of a computer programming algorithm

Ø Pseudocode is a compact and informal highlevel description of a computer programming algorithm that uses the structural conventions of a programming language, but is intended for human reading rather than machine reading. Pseudocode typically omits details that are not essential for human understanding of the algorithm, such as variable declarations, system-specific code and subroutines. (Wikipedia) 28

Pseudocode generally does not actually obey the syntax rules of any particular language. Ø

Pseudocode generally does not actually obey the syntax rules of any particular language. Ø There is no systematic standard form. Ø Pseudocode may therefore vary widely in style, from a near-exact imitation of a real programming language at one extreme, to a description approaching formatted prose at the other. Ø 29

Write only one statement per line Ø Capitalize initial keyword Ø Indent to show

Write only one statement per line Ø Capitalize initial keyword Ø Indent to show hierarchy Ø End multiline structures Ø Keep statements language independent Ø 30

BEGIN Procedure_Net. Calc READ name, gross. Pay, taxes IF taxes > 0 net =

BEGIN Procedure_Net. Calc READ name, gross. Pay, taxes IF taxes > 0 net = gross. Pay – taxes ELSE net = gross. Pay ENDIF WRITE name, net END Procedure_Net. Calc 31

Yes No 3<Y? X=5 Ø X = 10 Pseudocode IF Y > 3 X=5

Yes No 3<Y? X=5 Ø X = 10 Pseudocode IF Y > 3 X=5 ELSE X = 10 ENDIF 32

BEGIN count = 0 WHILE count < 10 DO Process ENDWHILE WRITE “The End”

BEGIN count = 0 WHILE count < 10 DO Process ENDWHILE WRITE “The End” END Start count = 0 Process Count < 10 BEGIN Process ADD 1 to count WRITE count END Process Yes Process Add 1 to count No Write count Write “The End” End Count < 10 Return 33

Advantages • Standardized • Visual • Hard to modify • Structured design elements not

Advantages • Standardized • Visual • Hard to modify • Structured design elements not implemented • Special software required • Easily modified • Implements structured concepts • Done easily on word processor • Not visual • No accepted standard, varies from company to company Flowchart Pseudocode Disadvantages 34