Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using...

33
Computer Science Departme nt FTSM FTSM Algorithm Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution in flowchart and pseudocode forms

Transcript of Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using...

Page 1: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

Computer Science Department FTSMFTSM

AlgorithmAlgorithm

Knowledge:Understand algorithm representation using flowchart and pseudocode

Skill:Map problem to solution in flowchart and pseudocode forms

Page 2: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 22

Consider the following ….Problem: Baking a CakeHow to solve:

1. Start2. Preheat the oven at 180oC3. Prepare a baking pan4. Beat butter with sugar5. Mix them with flour, eggs and essence vanilla6. Pour the dough into the baking pan7. Put the pan into the oven8. End

Algorithm in Real LifeAlgorithm in Real Life

Page 3: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 33

‘‘Divide and Conquer’ Strategy Divide and Conquer’ Strategy in Algorithmin Algorithm

Problem: Prepare a Breakfast

1. Start

2. Prepare a Breakfast

3. End

Page 4: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 44

1. Start2. Prepare a Breakfast 2.1 Prepare a tuna sandwich 2.2 Prepare some chips 2.3 Make a cup of coffee3. End

‘‘Divide and Conquer’ Strategy Divide and Conquer’ Strategy in Algorithmin Algorithm

Page 5: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 55

1. Start2. Prepare a Breakfast 2.1 Prepare a tuna sandwich 2.1.1 Take 2 slices of bread 2.1.2 Prepare tuna paste 2.2 Prepare some chips 2.3 Make a cup of coffee3. End

‘‘Divide and Conquer’ Strategy Divide and Conquer’ Strategy in Algorithmin Algorithm

Page 6: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 66

1. Start2. Prepare a Breakfast 2.1 Prepare a tuna sandwich 2.1.1 Take 2 slices of bread 2.1.2 Prepare tuna paste 2.2 Prepare some chips 2.2.1 Cut potatoes into slices 2.2.2 Fry the potatoes 2.3 Make a cup of coffee3. End

‘‘Divide and Conquer’ Strategy Divide and Conquer’ Strategy in Algorithmin Algorithm

Page 7: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 77

1. Start2. Prepare a Breakfast 2.1. Prepare a tuna sandwich 2.1.1 Take 2 slices of bread 2.1.2 Prepare tuna paste 2.2. Prepare some chips 2.2.1 Cut potatoes into slices 2.2.2 Fry the potatoes 2.3. Make a cup of coffee 2.3.1 Boil water 2.3.2 Add water with sugar and coffee3. End

‘‘Divide and Conquer’ Strategy Divide and Conquer’ Strategy in Algorithmin Algorithm

Page 8: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 88

What is the connection What is the connection between these real life between these real life processes and processes and algorithm?algorithm?

Something to ponder …Something to ponder …

Page 9: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 99

A specific and step-by-step set of instructions for carrying out a procedure or solving a problem, usually with the requirement that the procedure terminate at some point

2 types of algorithm representation will be explained:

Flowchart Pseudocode

Structured Method (will be explained later) A method of designing problem solution

AlgorithmAlgorithm

Page 10: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 1010

Flowchart represents algorithm graphically. It is intended for communication and documentation

FlowchartFlowchart

Page 11: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 1111

Flowchart – Basic SyntaxFlowchart – Basic Syntax

Start/End

SymbolSymbol SemanticSemantic

Process

Input/Output

Test

Connector

Flow of activities

Page 12: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 1212

Flowchart – Other SyntaxFlowchart – Other Syntax

Function call

SymbolSymbol SemanticSemantic

Magnetic Disc

Stored Data

Document/File

Multiple Document/File

Page 13: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 1313

Are the steps in the Are the steps in the algorithm discussed algorithm discussed earlier specific earlier specific enough to be enough to be executed by executed by computer?computer?

Something to ponder …Something to ponder …

Page 14: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 1414

Problem Solving ProcessProblem Solving Process

InputInput ProcessProcess OutputOutput

Page 15: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 1515

Example 1Example 1Calculate and display the price of a number of apples if the quantity in kg and price per kg are given.

• QuantityQuantity

• Price_per_kgPrice_per_kg

PricePricePrice = Quantity * Price_per_kgPrice = Quantity * Price_per_kg

InputInput ProcessProcess OutputOutput

Page 16: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 1616

Flowchart: Calculate Price of Flowchart: Calculate Price of ApplesApples

InputInputQuantityQuantity

StartStart

Price Price Quantity * Price_per_kg Quantity * Price_per_kg

InputInputPrice_per_kgPrice_per_kg

OutputOutputPricePrice

EndEnd

Page 17: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 1717

void main() {

scanf(“%d”,&quantity);

scanf(“%d”,&price_per_kg);

price = quantity*price_per_kg;

printf(“%d”, price);

}

InputInputQuantityQuantity

StartStart

Price Price Quantity * Price_per_kg Quantity * Price_per_kg

InputInputPrice_per_kgPrice_per_kg

OutputOutputPricePrice

EndEnd

C Program: Calculate Price of ApplesC Program: Calculate Price of Apples

Page 18: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 1818

void main() {

scanf(“%d”,&quantity);

scanf(“%d”,&price_per_kg);

price = quantity*price_per_kg;

printf(“%d”, price);

}

C Program: Calculate Price of ApplesC Program: Calculate Price of Apples

It’s not complete! It’s not complete! Declare the variables…Declare the variables…

Page 19: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 1919

void main() {

int quantity, price_per_kg, price;

scanf(“%d”,&quantity);

scanf(“%d”,&price_per_kg);

price = quantity*price_per_kg;

printf(“%d”, price);

}

Well done ! But…Well done ! But…what are they?what are they?

C Program: Calculate Price of ApplesC Program: Calculate Price of Apples

Page 20: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 2020

void main() {

int quantity, price_per_kg, price;

scanf(“%d”,&quantity);scanf(“%d”,&price_per_kg);

price = quantity*price_per_kg;

printf(“%d”, price);

}

StartStart DeclarationDeclaration

} InputInput

ProcessProcess

OutputOutputEndEnd

C Program: Calculate Price of ApplesC Program: Calculate Price of Apples

Page 21: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 2121

Chapter 4Chapter 4

} Chapter 5Chapter 5

Chapter 6Chapter 6

Chapter 5Chapter 5

C Program: Calculate Price of ApplesC Program: Calculate Price of Applesvoid main() {

int quantity, price_per_kg, price;

scanf(“%d”,&quantity);scanf(“%d”,&price_per_kg);

price = quantity*price_per_kg;

printf(“%d”, price);

}

Page 22: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 2222

Example 2Example 2A car park has the following charges:

The 1st hour costs RM2.00. The subsequent hours cost RM1.00 per hour. Write an algorithm based on a vehicle’s entry and exit time.

• Entry_timeEntry_time

• Exit_timeExit_timeChargeCharge????????

InputInput ProcessProcess OutputOutput

Page 23: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 2323

Flowchart: Calculate Car Park Flowchart: Calculate Car Park ChargeCharge

Input Entry_timeInput Entry_timeInput Exit_timeInput Exit_time

StartStart

OutputOutputChargeCharge

EndEnd

Period Period Exit_time – Entry_time Exit_time – Entry_time

Period > 1?Period > 1?YesYes

Charge Charge 2 + (Period * 1) 2 + (Period * 1)Charge Charge 22 NoNo

Page 24: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 2424

scanf(“%d%d”,&entry_time,&exit_time);

period = exit_time – entry_time;

Input Entry_timeInput Entry_timeInput Exit_timeInput Exit_time

StartStart

OutputOutputChargeCharge

EndEnd

Period Period Exit_time – Entry_time Exit_time – Entry_time

Period > 1?Period > 1?YesYes

Charge Charge 2 + (Period * 1) 2 + (Period * 1)Charge Charge 22 NoNo

if (period > 1)charge = 2 + ( period *1);

elsecharge = 2;

printf(“%d”,charge);

Flowchart: Calculate Car Park Flowchart: Calculate Car Park ChargeCharge

Page 25: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 2525

void main() {

int entry_time, exit_time, period, charge;

scanf(“%d%d”,&entry_time,&exit_time);

period = exit_time – entry_time;

if (period > 1)charge = 2 + (period * 1);

elsecharge = 2;

printf(“%d”,charge);

}

} Chapter 7

C Program: Calculate Car Park C Program: Calculate Car Park ChargeCharge

Page 26: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 2626

Write a program to calculate the average mark of three TK1913’s students.

Example 3Example 3

• Mark AMark A

• Mark BMark B

• Mark CMark C

Average_markAverage_mark????????

InputInput ProcessProcess OutputOutput

THINK!!THINK!!

Page 27: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 2727

Identify the input and output of the problem. If necessary, use ‘Divide & Conquer’ strategy

to decompose the problem into smaller and manageable sub problems. Decompose the problem until all the sub problems can be solved to get the required results

For each sub problem, identify and list out the steps involved in solving it

Algorithm Development Algorithm Development GuidelinesGuidelines

Page 28: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 2828

What might be the What might be the disadvantage of disadvantage of flowchart?flowchart?

Something to ponder …Something to ponder …

Page 29: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 2929

An outline of a program, written in a form that can easily be converted into real programming statements. It resembles the actual program that will be implemented later. However, it cannot be compiled nor executed.

Pseudocode normally codes the following actions:

Initialisation of variables Assignment of values to the variables Arithmetic operations Relational operations

PseudocodePseudocode

Page 30: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 3030

1. Start

2. Read quantityquantity

3. Read price_per_kgprice_per_kg

4.4. priceprice quantityquantity * price_per_kgprice_per_kg

5. Print priceprice

6. End

Example of PseudocodeExample of Pseudocode

Page 31: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 3131

Guidelines on Writing Guidelines on Writing PseudocodePseudocode

Refer to the Refer to the handouts in your handouts in your manual …. OK??manual …. OK??

Page 32: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 3232

CFlow DemonstrationCFlow Demonstration

Page 33: Computer Science Department FTSM Algorithm Knowledge: Understand algorithm representation using flowchart and pseudocode Skill: Map problem to solution.

TK1913-C ProgrammingTK1913-C Programming 3333

End of Lecture 2End of Lecture 2

Yes !! That’s all? Yes !! That’s all? What’s next???What’s next???

INTRODUCTION TO CINTRODUCTION TO C on the way … on the way …