Chapter 0 Introduction to Problem-Solving

Preview:

Citation preview

Chapter 0Introduction to Problem-SolvingCPIT 110 (Problem-Solving and Programming)

Version 2.0

Sections

2

• 0.1. Problem-Solving & Computer Science

• 0.2. Program Design & Problem-Solving Techniques

• 0.3. Steps in Program Development

• 0.4. Algorithms, Pseudocode, & Flowcharts

• 0.5. Decision Structures

Examples

Examples

3

• Example 1: Road Example

• Example 2: Area of a Rectangle Calculator

• Example 3: Simple Calculator

• Example 4: Determining a Student’s Final Grade

• Example 5: Converting The Length

• Example 6: Area of a Rectangle Calculator

• Example 7: Determining The Largest Value

Python Online IDE

Objectives

4

• To explain what problem solving is, and why it is important (0.1).

• To understand how to write algorithms (0.1–0.5).

• To describe how a program can be designed (0.2–0.3).

• To describe algorithms in different forms (0.4).

• To understand the difference between algorithms and pseudocode (0.4).

• To draw program flowcharts (0.4-0.5).

• To understand decision Structures (0.5).

0.1. Problem-Solving & Computer Science

5

▪ What is Computer Science?

▪ Example 1: Road Example

▪ Algorithms

▪ Example 2: Area of a Rectangle Calculator

What is Computer Science?

6

• Computer Science can be summarized with two simple words: problem solving.

• Computer Science is the study of problems, problem-solving, and the solutions that come out of this problem-solving process.

• Given a problem, the goal is to develop an algorithm to solve the problem.

• An algorithm is a step-by-step list of instructions to solve the problem.

0.1

Road ExampleExample 1

7

Imagine that you have the followingimage, which is a map of a roadleading to the building shown in thepicture.

• There are a car and trees.

• The car cannot cross the trees.

• The road is divided into squares tocalculate the steps of the car.

• Each square is considered as onestep.

How can the car arrive at the building?

Example 10.1

Road ExampleSolution A

8

• Step 1: Move to the right three steps.

• Step 2: Move to down two steps.

• Step 3: Move to the left one step.

• Step 4: Move to down two steps.

• Step 5: Move to the right one step.

• Step 6: Move to down one step. Step 1

Step 3 Step 4 Step 5

Step 2

Step 6

Example 10.1

Road ExampleSolution B

9

• Step 1: Move to down four steps.

• Step 2: Move to the right three steps.

• Step 3: Move to down one step.

Step 1 Step 2 Step 3

Example 10.1

Road ExampleDifferent Solutions

10

• As we can see that Solution A and Solution B are both correctsolutions to the same problem, but there are differences in thecomplexity and efficiency of the solutions.

• The cost of Solution A is 10 steps while Solution B is 8 steps, so weconsider Solution B as a better solution based on the number ofsteps.

• Reducing the number of steps in the previous example meansreducing the amount of fuel needed by the vehicle and speeding upthe arrival time.

Example 10.1

Algorithms

11

• An algorithm is a set of obvious, logical, and sequentialsteps that solve a specific problem.

• To put it simply, the algorithm is like a recipe for preparing a specific food.

• Following the steps of the algorithm will end up solving the problem.

0.1

Area of a Rectangle CalculatorExample 2

12

Write an algorithm that can calculate the area of arectangle. The width and the height of the rectangleshould be taken from the user.

Note: Area = Width × Height

Example 20.1

Area of a Rectangle CalculatorSolution A

13

Solution A – Good:1. Ask the user to enter Width

2. Ask the user to enter Height

3. Set Area to (Width × Height)

4. Display Area for the user

• As you can see in this solution, we have described the steps that are going to solve the problem.

• You can describe the steps in your own way, but your description of the steps should be obvious, logical, and sequential.

Example 20.1

Area of a Rectangle CalculatorSolution B

14

Solution B - Bad: 1. Ask the user to enter Width

2. Ask the user to enter Height

3. Calculate Area

4. Display Area for the user

The reason for considering Solution B as a bad solution:

• Step 3 is not clear because it does not explain how we can calculate Area.

• So, this algorithm is bad because its steps are not obvious.

Example 20.1

Area of a Rectangle CalculatorSolution C

15

Solution C - Bad: 1. Set Area to (Width × Height)

2. Ask the user to enter Width

3. Ask the user to enter Height

4. Display Area for the user

The reasons for considering Solution C as a bad solution:

• We don't know what Width and Height at the Step 1 are. In other words, Width andHeight have not been defined before Step 1, so we cannot use them because they donot exist yet.

• What about Step 2 and Step 3? Width and Height are defined there!. After Step 2,Width does exist, but Height does not. After Step 3, Height does exist. Both Width andHeight are available to be used at or after step 4.

• So, this algorithm is bad because its steps are not correctly sequential.

Example 20.1

Area of a Rectangle CalculatorSolution D

16

Solution D - Bad: 1. Set Area to (Width × Height)

2. Display Area for the user

The reasons for considering Solution D as a bad solution:

• Step 1 tells us to multiply Width and Height, but we don't knowwhat Width and Height are. Even, they have not been defined in anysteps of the algorithm.

• So, this algorithm is bad because of the illogical step, which is using unknown things (Width and Height).

Example 20.1

Area of a Rectangle CalculatorSolution E

17

Solution E - Bad: 1. Ask the user to enter Width

2. Ask the user to enter Height

3. Set Area to (Width × Height × 2)

4. Display Area for the user

The reasons for considering Solution E as a bad solution:

• This algorithm will give us a wrong value of the Area. For example, suppose that theuser entered 4 for Width and 5 for Height. The correct value of the Area should be 20,but this algorithm will display 40 as the value of the Area.

• The reason for giving the wrong value is how Step 3 calculates the Area. Step 3calculates the Area by the incorrect equation (Width × Height × 2) instead of (Width ×Height).

• So, this algorithm is bad because it has a logical problem, which is producing incorrectoutput (the value of the Area).

Example 20.1

0.2. Program Design &Problem-Solving Techniques

18

▪ How Do We Write a Program?

▪ Problem-Solving Phase

▪ Implementation Phase

How Do We Write a Program?

19

• A Computer is not intelligent. ◦ It cannot analyze a problem and come up with a solution.

◦ A human (the programmer) must analyze the problem, develop theinstructions for solving the problem, and then have the computer carry outthe instructions.

• To write a program for a computer to follow, we must go through atwo-phase process: problem solving and implementation.

0.2

Problem-Solving Phase

20

1. Analysis and Specification - Understand (define) the problem andwhat the solution must do.

2. General Solution (Algorithm) - Specify the required data types andthe logical sequences of steps that solve the problem.

3. Verify - Follow the steps exactly to see if the solution really doessolve the problem.

0.2

Implementation Phase

21

• Concrete Solution (Program) - Translate the algorithm (the general solution) into a programming language.

• Test - Have the computer follow the instructions.◦ Then manually check the results.

◦ If you find errors, analyze the program and the algorithm to determine the source of the errors, and then make corrections.

• Once a program is tested, it enters into next phase (Maintenance).

• Maintenance requires modification of the program to meet changing requirements or to correct any errors that show up while using it.

0.2

0.3. Steps in Program Development

22

▪ Example 3: Simple Calculator

Steps in Program Development

23

1. Define the problem into three separate components:

• Inputs

• Processing steps to produce required outputs.

• Outputs

0.3

Steps in Program Development

24

2. Outline the solution.

• Decompose the problem to smaller steps.

• Establish a solution outline.

3. Develop the outline into an algorithm.

• The solution outline is now expanded into an algorithm.

0.3

Steps in Program Development

25

4. Test the algorithm for correctness.

• Very important in the development of a program, but oftenforgotten.

• Major logic errors can be detected and corrected at an early stage.

5. Code the algorithm into a specific programming language.

0.3

Steps in Program Development

26

6. Run the program on the computer.

• This step uses a program compiler or interpreter, and programmer-designed test data to machine-test the code for

• Syntax errors• Runtime errors

• Logic errors

7. Document and maintain the program.

0.3

Simple CalculatorExample 3

27

Suppose that you are asked to write a calculator programthat can sum and subtract two integer numbers. Write theprogram requirements, specifications and algorithm.

Example 30.3

Simple CalculatorThe Requirements

28

Suppose that you are asked to write a calculator program that can sumand subtract two integer numbers. Write the program requirements,specifications and algorithm.

The requirements:◦ The user can enter an equation which consists of two numbers and a sign (- or

+).

◦ The program should calculate the equation correctly and display the result forthe user.

◦ The program can sum and subtract two integer numbers.

Example 30.3

Simple CalculatorThe Specifications

29

Suppose that you are asked to write a calculator program that can sumand subtract two integer numbers. Write the program requirements,specifications and algorithm.

The specifications:◦ When the program runs, it will display a welcome message that says, 'Welcome

to our Calculator".

◦ The program will then ask the user to enter the first number.

◦ The program will then ask the user to enter the second number.

◦ The program will then ask the user to select the sign (calculation operators)from this set (-,+).

◦ The program will then display the correct result of the calculation on the screenand end.

Example 30.3

Simple CalculatorDesigning a Solution

30

• After the steps of identifying the problem (the requirements and specifications), we should have a clear idea about what is going exactly to be achieved and solved.

• In this step, we are going to describe how the specifications can be achieved.

• This means that we need to design an algorithm that fulfills the specifications.

• We can design the algorithm via written steps or visualized steps using, for example, flowcharts.

• In the written steps, we can use simple sentences in English or some special notations and structures in something called "Pseudocode".

Example 30.3

Simple CalculatorThe Algorithm

31

Suppose that you are asked to write a calculator program that can sumand subtract two integer numbers. Write the program requirements,specifications and algorithm.

The algorithm:1. Display a welcome message that says, “Welcome to our Calculator".

2. Ask the user to enter the first number and save it to X.

3. Ask the user to enter the second number and save it to Y.

4. Ask the user to select the sign (-,+) and save it in Sign.

5. if Sign is equal to “+”, make Sum = X + Y

6. Otherwise, make Sum = X - Y

7. Display Sum

Example 30.3

Simple CalculatorThe Pseudocode and Flowchart

32

Suppose that you are asked to write a calculator program that can sumand subtract two integer numbers. Write the program requirements,specifications and algorithm.

The algorithm (pseudocode and flowchart):1. print “Welcome to our Calculator”

2. X = input “Enter the first number:“

3. Y = input “Enter the second number:”

4. Sign = input "Select – or +"

5. if Sign is equal to "+" then:

6. Sum = X + Y

7. else:

8. Sum = X – Y

9. End if

10. print Sum

Example 30.3

33Example 30.3

Simple CalculatorVerifying The Algorithm

34

1. Display a welcome message that says, “Welcome to our Calculator".

2. Ask the user to enter the first number and save it to X.

3. Ask the user to enter the second number and save it to Y.

4. Ask the user to select the sign (-,+) and save it in Sign.

5. if Sign is equal to “+”, make Sum = X + Y

6. Otherwise, make Sum = X - Y

7. Display Sum

The Algorithm The User

“Welcome to our Calculator”

Test 1

1 of 70.3 Example 3

Simple CalculatorVerifying The Algorithm

35

1. Display a welcome message that says, “Welcome to our Calculator".

2. Ask the user to enter the first number and save it to X.

3. Ask the user to enter the second number and save it to Y.

4. Ask the user to select the sign (-,+) and save it in Sign.

5. if Sign is equal to “+”, make Sum = X + Y

6. Otherwise, make Sum = X - Y

7. Display Sum

The Algorithm The User

Test 1

X = 20

Please enter the first number

2 of 70.3 Example 3

Simple CalculatorVerifying The Algorithm

36

1. Display a welcome message that says, “Welcome to our Calculator".

2. Ask the user to enter the first number and save it to X.

3. Ask the user to enter the second number and save it to Y.

4. Ask the user to select the sign (-,+) and save it in Sign.

5. if Sign is equal to “+”, make Sum = X + Y

6. Otherwise, make Sum = X - Y

7. Display Sum

The Algorithm The User

Test 1

Y = 10

Please enter the second numberx = 20

3 of 70.3 Example 3

Simple CalculatorVerifying The Algorithm

37

1. Display a welcome message that says, “Welcome to our Calculator".

2. Ask the user to enter the first number and save it to X.

3. Ask the user to enter the second number and save it to Y.

4. Ask the user to select the sign (-,+) and save it in Sign.

5. if Sign is equal to “+”, make Sum = X + Y

6. Otherwise, make Sum = X - Y

7. Display Sum

The Algorithm The User

Test 1

Sign = +

Please select – or +x = 20Y = 10

4 of 70.3 Example 3

Simple CalculatorVerifying The Algorithm

38

1. Display a welcome message that says, “Welcome to our Calculator".

2. Ask the user to enter the first number and save it to X.

3. Ask the user to enter the second number and save it to Y.

4. Ask the user to select the sign (-,+) and save it in Sign.

5. if Sign is equal to “+”, make Sum = X + Y

6. Otherwise, make Sum = X - Y

7. Display Sum

The Algorithm The User

Test 1

x = 20Y = 10Sign = +

Is Sign equal to “+”? Yes

5 of 70.3 Example 3

Simple CalculatorVerifying The Algorithm

39

1. Display a welcome message that says, “Welcome to our Calculator".

2. Ask the user to enter the first number and save it to X.

3. Ask the user to enter the second number and save it to Y.

4. Ask the user to select the sign (-,+) and save it in Sign.

5. if Sign is equal to “+”, make Sum = X + Y

6. Otherwise, make Sum = X - Y

7. Display Sum

The Algorithm The User

Test 1

x = 20Y = 10Sign = +

Sum = X + Y = 20 + 10 = 30

6 of 70.3 Example 3

Simple CalculatorVerifying The Algorithm

40

1. Display a welcome message that says, “Welcome to our Calculator".

2. Ask the user to enter the first number and save it to X.

3. Ask the user to enter the second number and save it to Y.

4. Ask the user to select the sign (-,+) and save it in Sign.

5. if Sign is equal to “+”, make Sum = X + Y

6. Otherwise, make Sum = X - Y

7. Display Sum

The Algorithm The User

Test 1

x = 20Y = 10Sign = +Sum = 30

30

7 of 70.3 Example 3

Output:30

Simple CalculatorVerifying The Algorithm

41

1. Display a welcome message that says, “Welcome to our Calculator".

2. Ask the user to enter the first number and save it to X.

3. Ask the user to enter the second number and save it to Y.

4. Ask the user to select the sign (-,+) and save it in Sign.

5. if Sign is equal to “+”, make Sum = X + Y

6. Otherwise, make Sum = X - Y

7. Display Sum

The Algorithm The User

“Welcome to our Calculator”

Test 2

1 of 70.3 Example 3

Simple CalculatorVerifying The Algorithm

42

1. Display a welcome message that says, “Welcome to our Calculator".

2. Ask the user to enter the first number and save it to X.

3. Ask the user to enter the second number and save it to Y.

4. Ask the user to select the sign (-,+) and save it in Sign.

5. if Sign is equal to “+”, make Sum = X + Y

6. Otherwise, make Sum = X - Y

7. Display Sum

The Algorithm The User

X = 50

Please enter the first number

Test 2

2 of 70.3 Example 3

Simple CalculatorVerifying The Algorithm

43

1. Display a welcome message that says, “Welcome to our Calculator".

2. Ask the user to enter the first number and save it to X.

3. Ask the user to enter the second number and save it to Y.

4. Ask the user to select the sign (-,+) and save it in Sign.

5. if Sign is equal to “+”, make Sum = X + Y

6. Otherwise, make Sum = X - Y

7. Display Sum

The Algorithm The User

Y = 15

Please enter the second numberx = 50

Test 2

3 of 70.3 Example 3

Simple CalculatorVerifying The Algorithm

44

1. Display a welcome message that says, “Welcome to our Calculator".

2. Ask the user to enter the first number and save it to X.

3. Ask the user to enter the second number and save it to Y.

4. Ask the user to select the sign (-,+) and save it in Sign.

5. if Sign is equal to “+”, make Sum = X + Y

6. Otherwise, make Sum = X - Y

7. Display Sum

The Algorithm The User

Sign = -

Please select - or +x = 50Y = 15

Test 2

4 of 70.3 Example 3

Simple CalculatorVerifying The Algorithm

45

1. Display a welcome message that says, “Welcome to our Calculator".

2. Ask the user to enter the first number and save it to X.

3. Ask the user to enter the second number and save it to Y.

4. Ask the user to select the sign (-,+) and save it in Sign.

5. if Sign is equal to “+”, make Sum = X + Y

6. Otherwise, make Sum = X - Y

7. Display Sum

The Algorithm The User

x = 50Y = 15Sign = -

Is Sign equal to “+”? No

Test 2

5 of 70.3 Example 3

Simple CalculatorVerifying The Algorithm

46

1. Display a welcome message that says, “Welcome to our Calculator".

2. Ask the user to enter the first number and save it to X.

3. Ask the user to enter the second number and save it to Y.

4. Ask the user to select the sign (-,+) and save it in Sign.

5. if Sign is equal to “+”, make Sum = X + Y

6. Otherwise, make Sum = X - Y

7. Display Sum

The Algorithm The User

x = 50Y = 15Sign = -

Sum = X - Y = 50 - 15 = 35

Test 2

6 of 70.3 Example 3

Simple CalculatorVerifying The Algorithm

47

1. Display a welcome message that says, “Welcome to our Calculator".

2. Ask the user to enter the first number and save it to X.

3. Ask the user to enter the second number and save it to Y.

4. Ask the user to select the sign (-,+) and save it in Sign.

5. if Sign is equal to “+”, make Sum = X + Y

6. Otherwise, make Sum = X - Y

7. Display Sum

The Algorithm The User

x = 50Y = 15Sign = -Sum = 35

35

Test 2

7 of 70.3 Example 3

Output:35

0.4. Algorithms, Pseudocode, & Flowcharts

48

▪ Example 4: Determining a Student’s Final Grade

▪ Flowcharts

▪ Flowchart Symbols

▪ Example 5: Converting The Length

▪ Example 6: Area of a Rectangle Calculator

Algorithm, Pseudocode, & Flowcharts

49

• What is an algorithm?◦ A step-by-step series of instructions in order to perform a specific

task.

◦ An algorithm must:▪ Be lucid (clear), precise and unambiguous.

▪ Give the correct solution in all cases, and eventually end.

• What is pseudocode?◦ It is English that looks similar to code▪ But it is not actual code (only looks a little similar) .

▪ Think of pseudocode as a way of expressing your algorithm.

• What is a flowchart?◦ A graphical representation of the sequence of operations in an

information system or program.

0.4

Algorithm, Pseudocode, & Flowcharts

50

• For Clarity:◦ An algorithm is a series of steps you take to solve a problem, just

like a recipe is a series of steps you take to make a food!

◦ Now, we express our algorithms in many ways:▪ Pseudocode: this is not “real code”, but a slightly more formal way of writing

the algorithmic steps

▪ As an example, maybe the programmer does not know the language he/she will use. Therefore, they just write pseudocode during Problem-Solving Phase.

▪ Flowchart: this is a graphical representation of the algorithm

▪ Actual code: this is during the Implementation Phase

▪ Python, Java, C++, C, etc

0.4

Determining a Student’s Final GradeExample 4

51

Write an algorithm and pseudocode to determine astudent’s final grade and indicate whether it is passing orfailing. The final grade is calculated as the average of fourmarks.

Example 40.4

Determining a Student’s Final GradeAlgorithm

52

Write an algorithm and pseudocode to determine a student’s finalgrade and indicate whether it is passing or failing. The final grade iscalculated as the average of four marks.

Algorithm:1. Ask the user to enter 4 marks (Mark1, Mark2, Mark3, Mark4)

2. Calculate the marks average (Avg) by summing marks and it dividing by 4

3. If average (Avg) is greater than or equal 60

4. Print “Pass”

5. Else

6. Print “Fail”

7. End if

Example 40.4

Determining a Student’s Final GradePseudocode

53

Write an algorithm and pseudocode to determine a student’s finalgrade and indicate whether it is passing or failing. The final grade iscalculated as the average of four marks.

Pseudocode:1. input Mark1, Mark2, Mark3, Mark4

2. Avg = (Mark1 + Mark2 + Mark3 + Mark4) / 4

3. if Avg >= 60:

4. print “Pass”

5. else:

6. print “Fail”

7. End if

Example 40.4

Determining a Student’s Final GradeVerifying The Algorithm

54

1. input Mark1, Mark2, Mark3, Mark4

2. Avg = (Mark1 + Mark2 + Mark3 + Mark4) / 4

3. if Avg >= 60:

4. print “Pass”

5. else:

6. print “Fail”

7. End if

The Algorithm The User

Mark1 = 80, Mark2 = 90, Mark3 = 95, Mark4 = 85

I am waiting you to give me 4 marks

Test 1

1 of 50.4 Example 4

Determining a Student’s Final GradeVerifying The Algorithm

55

1. input Mark1, Mark2, Mark3, Mark4

2. Avg = (Mark1 + Mark2 + Mark3 + Mark4) / 4

3. if Avg >= 60:

4. print “Pass”

5. else:

6. print “Fail”

7. End if

The Algorithm The User

Mark1 = 80, Mark2 = 90, Mark3 = 95, Mark4 = 85

Avg = (80 + 90 + 95 + 85) / 4 = 350 / 4 = 87.5

Test 1

2 of 50.4 Example 4

Determining a Student’s Final GradeVerifying The Algorithm

56

1. input Mark1, Mark2, Mark3, Mark4

2. Avg = (Mark1 + Mark2 + Mark3 + Mark4) / 4

3. if Avg >= 60:

4. print “Pass”

5. else:

6. print “Fail”

7. End if

The Algorithm The User

Mark1 = 80, Mark2 = 90, Mark3 = 95, Mark4 = 85

Avg = 87.5

Avg >= 60 = 87.5 >= 60 = Yes

Test 1

3 of 50.4 Example 4

Determining a Student’s Final GradeVerifying The Algorithm

57

1. input Mark1, Mark2, Mark3, Mark4

2. Avg = (Mark1 + Mark2 + Mark3 + Mark4) / 4

3. if Avg >= 60:

4. print “Pass”

5. else:

6. print “Fail”

7. End if

The Algorithm The User

Mark1 = 80, Mark2 = 90, Mark3 = 95, Mark4 = 85

Avg = 87.5“Pass”

Test 1

4 of 50.4 Example 4

Output:Pass

Determining a Student’s Final GradeVerifying The Algorithm

58

1. input Mark1, Mark2, Mark3, Mark4

2. Avg = (Mark1 + Mark2 + Mark3 + Mark4) / 4

3. if Avg >= 60:

4. print “Pass”

5. else:

6. print “Fail”

7. End if

The Algorithm The User

Mark1 = 80, Mark2 = 90, Mark3 = 95, Mark4 = 85

Avg = 87.5

Test 1

5 of 50.4 Example 4

Output:Pass

Determining a Student’s Final GradeVerifying The Algorithm

59

1. input Mark1, Mark2, Mark3, Mark4

2. Avg = (Mark1 + Mark2 + Mark3 + Mark4) / 4

3. if Avg >= 60:

4. print “Pass”

5. else:

6. print “Fail”

7. End if

The Algorithm The User

Mark1 = 42, Mark2 = 55, Mark3 = 60, Mark4 = 37

I am waiting you to give me 4 marks

Test 2

1 of 60.4 Example 4

Determining a Student’s Final GradeVerifying The Algorithm

60

1. input Mark1, Mark2, Mark3, Mark4

2. Avg = (Mark1 + Mark2 + Mark3 + Mark4) / 4

3. if Avg >= 60:

4. print “Pass”

5. else:

6. print “Fail”

7. End if

The Algorithm The User

Mark1 = 42, Mark2 = 55, Mark3 = 60, Mark4 = 37

Avg = (42 + 55 + 60 + 37) / 4 = 194 / 4 = 48.5

Test 2

2 of 60.4 Example 4

Determining a Student’s Final GradeVerifying The Algorithm

61

1. input Mark1, Mark2, Mark3, Mark4

2. Avg = (Mark1 + Mark2 + Mark3 + Mark4) / 4

3. if Avg >= 60:

4. print “Pass”

5. else:

6. print “Fail”

7. End if

The Algorithm The User

Mark1 = 42, Mark2 = 55, Mark3 = 60, Mark4 = 37

Avg = 48.5

Avg >= 60 = 48.5 >= 60 = No

Test 2

3 of 60.4 Example 4

Determining a Student’s Final GradeVerifying The Algorithm

62

1. input Mark1, Mark2, Mark3, Mark4

2. Avg = (Mark1 + Mark2 + Mark3 + Mark4) / 4

3. if Avg >= 60:

4. print “Pass”

5. else:

6. print “Fail”

7. End if

The Algorithm The User

Mark1 = 42, Mark2 = 55, Mark3 = 60, Mark4 = 37

Avg = 48.5

Test 2

4 of 60.4 Example 4

Determining a Student’s Final GradeVerifying The Algorithm

63

1. input Mark1, Mark2, Mark3, Mark4

2. Avg = (Mark1 + Mark2 + Mark3 + Mark4) / 4

3. if Avg >= 60:

4. print “Pass”

5. else:

6. print “Fail”

7. End if

The Algorithm The User

Mark1 = 42, Mark2 = 55, Mark3 = 60, Mark4 = 37

Avg = 48.5“Fail”

Test 2

5 of 60.4 Example 4

Output:Fail

Determining a Student’s Final GradeVerifying The Algorithm

64

1. input Mark1, Mark2, Mark3, Mark4

2. Avg = (Mark1 + Mark2 + Mark3 + Mark4) / 4

3. if Avg >= 60:

4. print “Pass”

5. else:

6. print “Fail”

7. End if

The Algorithm The User

Mark1 = 42, Mark2 = 55, Mark3 = 60, Mark4 = 37

Avg = 48.5

Test 2

6 of 60.4 Example 4

Output:Fail

Flowchart

65

• A graphical representation of the sequence of operationsin an information system or program.

• Program flowcharts show the sequence of instructions in a single program or subroutine.

◦ show logic of an algorithm

◦ emphasize individual steps and their interconnections

◦ e.g. control flow from one action to the next

• Different symbols are used to draw each type of flowchart.

0.4

Flowchart Symbols

66

Name Symbol Use in Flowchart

Denotes the beginning or end of the program.

Denotes an input / output operations.

Denotes a process to be carried out.

For example: addition, subtraction, and division.

Denotes a decision or branch to be made The program

should continue along one of two routes (Ex. If/Then/Else)

Denotes the direction of logic flow in the program

Oval

Parallelogram

Rectangle

Diamond

Flow line

0.4

Flowcharts

67

• Are flowcharts really necessary or helpful?

◦ In the real world, programs are not only 1000 lines.

◦ Programs are hundreds of thousands of lines of code.▪ Even Millions of lines of code.

◦ Could you use only English to describe your program?▪ Sure you could, but you would end up with a book!

◦ Therefore, think of flowcharts as an easier, clearer way to quickly understand what a program is doing.

0.4

Flowcharts

68

• Are flowcharts really necessary or helpful?

◦ So in summary, yes, they are helpful.

• That said, most of the programs we show you over the next few weeks are smaller programs.

◦ Do you really need a flowchart for a small program?

◦ Probably not.

◦ However, students should get into the habit of making flowcharts with smaller, easier programs.

◦ Then, it will be easy to do for larger programs.

0.4

Converting The LengthExample 5

69

Write an Algorithm, Pseudocode, and draw a flowchart to convert the length in feet to centimeter.

Algorithm:1. Input the length in feet (LFT)

2. Calculate the length in cm (LCM) by multiplying LFT with 30

3. Print length in cm (LCM)

Example 50.4

Converting The LengthThe Algorithm

70

Pseudocode: 1. LFT = input “Length in feet”

2. LCM = LFT x 30

3. print LCM

Flowchart:

Start

input

LFT

LCM = LFT x 30

output

LCM

End

Example 50.4

Converting The LengthVerifying The Algorithm

71

Start

input

LFT

LCM = LFT x 30

output

LCM

End

Test 1

The Algorithm

The User

1 of 50.4 Example 5

Converting The LengthVerifying The Algorithm

72

Start

input

LFT = 15

LCM = LFT x 30

output

LCM

End

Test 1

The Algorithm

The User

2 of 50.4 Example 5

Converting The LengthVerifying The Algorithm

73

Start

input

LFT = 15

LCM = LFT x 30

= 15 x 30 = 450

output

LCM

End

Test 1

The Algorithm

The User

3 of 50.4 Example 5

Converting The LengthVerifying The Algorithm

74

Start

input

LFT = 15

LCM = 450

output

LCM

= 450

End

Test 1

The Algorithm

The User

4 of 50.4 Example 5

Output:450

Converting The LengthVerifying The Algorithm

75

Start

input

LFT = 15

LCM = 450

output

LCM

= 450

End

Test 1

The Algorithm

The User

5 of 50.4 Example 5

Output:450

Area of a Rectangle CalculatorExample 6

76

Write an Algorithm, Pseudocode, and draw a flowchart thatwill read the two sides of a rectangle and calculate its area.

Algorithm:1. Input the Length (L) and width (W) of a rectangle

2. Calculate the area (A) by multiplying L with W

3. Print A

Example 60.4

Area of a Rectangle CalculatorThe Algorithm

77

Pseudocode: 1. input L, W

2. A = L x W

3. print A

Flowchart:

inputL , W

A = L x W

outputA

Start

End

Example 60.4

0.5. Decision Structures

78

▪ If–then–else Structure

▪ Relational Operators

▪ Example 7: Determining The Largest Value

Decision Structures

79

• The expression A > B is a logical expression

• It describes a condition we want to test

• if A > B is true (if A is greater than B) we take the action on left: print the value of A

• if A > B is false (if A is not greater than B) we take the action on right: print the value of B

• Note: Print = Output

0.5

If–then–else Structure

80

• The structure is as follows

if condition then

true alternative

else

false alternative

End if

0.5

If–then–else Structure

81

• The algorithm for the flowchart is as follows:

if A > B then

print A

else

print B

End if

0.5

Relational Operators

82

Relational Operators

Operator Description

> Greater than

< Less than

== Equal to

Or >= Greater than or equal to

Or <= Less than or equal to

Or != Not equal to

0.5

Determining The Largest ValueExample 7

83

Write a Pseudocode that reads two values, determines thelargest value and prints the largest value with an identifyingmessage.

Pseudocode:1. Input VALUE1, VALUE22. if (VALUE1 > VALUE2) then 3. MAX = VALUE14. else 5. MAX = VALUE26. endif7. print “The largest value is”, MAX

print

“The largest value is”, MAX

End

Yes

Start

input

VALUE1, VALUE2

MAX = VALUE2

is

VALUE1 > VALUE2

MAX = VALUE1

No

Example 70.5

Determining The Largest ValueThe Algorithm

84

print

“The largest value is”, MAX

End

Yes

Start

input

VALUE1, VALUE2

MAX = VALUE2

isVALUE1 > VALUE2

MAX = VALUE1

No

Example 70.5

Determining The Largest ValueVerifying The Algorithm

85

Test 1

The Algorithm

print

“The largest value is”, MAX

End

Yes

Start

input

VALUE1, VALUE2

MAX = VALUE2

isVALUE1 > VALUE2

MAX = VALUE1

No

The User

1 of 60.5 Example 7

Determining The Largest ValueVerifying The Algorithm

86

Test 1

The Algorithm

The User

print

“The largest value is”, MAX

End

Yes

Start

input

VALUE1, VALUE2

MAX = VALUE2

isVALUE1 > VALUE2

MAX = VALUE1

No

2 of 60.5 Example 7

Determining The Largest ValueVerifying The Algorithm

87

Test 1

The Algorithm

The User

print

“The largest value is”, MAX

End

Yes

Start

input

VALUE1 = 3, VALUE2 = 5

MAX = VALUE2

isVALUE1 > VALUE2

MAX = VALUE1

No

3 of 60.5 Example 7

Determining The Largest ValueVerifying The Algorithm

88

Test 1

The Algorithm

The User

print

“The largest value is”, MAX

End

Yes

Start

input

VALUE1 = 3, VALUE2 = 5

MAX = VALUE2

= 5

isVALUE1 > VALUE2

MAX = VALUE1

No

4 of 60.5 Example 7

Determining The Largest ValueVerifying The Algorithm

89

Test 1

The Algorithm

The User

print

“The largest value is”, MAX

= “The largest value is 5”

End

Yes

Start

input

VALUE1 = 3, VALUE2 = 5

MAX = VALUE2

= 5

isVALUE1 > VALUE2

MAX = VALUE1

No

5 of 60.5 Example 7

Output:The largest value is 5

Determining The Largest ValueVerifying The Algorithm

90

Test 1

The Algorithm

The User

print

“The largest value is”, MAX

= “The largest value is 5”

End

Yes

Start

input

VALUE1 = 3, VALUE2 = 5

MAX = VALUE2

= 5

isVALUE1 > VALUE2

MAX = VALUE1

No

6 of 60.5 Example 7

Output:The largest value is 5

Determining The Largest ValueVerifying The Algorithm

91

1. Input VALUE1, VALUE2

2. if (VALUE1 > VALUE2) then

3. MAX = VALUE1

4. else

5. MAX = VALUE2

6. endif

7. print “The largest value is”, MAX

Test 2

1 of 70.5 Example 7

Determining The Largest ValueVerifying The Algorithm

92

1. Input VALUE1, VALUE2

2. if (VALUE1 > VALUE2) then

3. MAX = VALUE1

4. else

5. MAX = VALUE2

6. endif

7. print “The largest value is”, MAX

Test 2

2 of 70.5 Example 7

Determining The Largest ValueVerifying The Algorithm

93

1. Input VALUE1, VALUE2

2. if (VALUE1 > VALUE2) then

3. MAX = VALUE1

4. else

5. MAX = VALUE2

6. endif

7. print “The largest value is”, MAX

Test 2

3 of 70.5 Example 7

Determining The Largest ValueVerifying The Algorithm

94

1. Input VALUE1, VALUE2

2. if (VALUE1 > VALUE2) then

3. MAX = VALUE1

4. else

5. MAX = VALUE2

6. endif

7. print “The largest value is”, MAX

Test 2

4 of 70.5 Example 7

Determining The Largest ValueVerifying The Algorithm

95

1. Input VALUE1, VALUE2

2. if (VALUE1 > VALUE2) then

3. MAX = VALUE1

4. else

5. MAX = VALUE2

6. endif

7. print “The largest value is”, MAX

Test 2

5 of 70.5 Example 7

Determining The Largest ValueVerifying The Algorithm

96

1. Input VALUE1, VALUE2

2. if (VALUE1 > VALUE2) then

3. MAX = VALUE1

4. else

5. MAX = VALUE2

6. endif

7. print “The largest value is”, MAX

Test 2

6 of 70.5 Example 7

Determining The Largest ValueVerifying The Algorithm

97

1. Input VALUE1, VALUE2

2. if (VALUE1 > VALUE2) then

3. MAX = VALUE1

4. else

5. MAX = VALUE2

6. endif

7. print “The largest value is”, MAX

Test 2

7 of 70.5 Example 7

Determining The Largest ValuePython Code

98Example 70.5

Example7.py

value1 = eval(input("Enter Value 1: "))

value2 = eval(input("Enter Value 2: "))

if value1 > value2:

largest = value1

else:

largest = value2

print("The largest value is", largest)

1

2

3

4

5

6

7

8

9

Run

End

99

▪ Play & Learn

Play & Learn

100

• Be familiar with basic logic and problem-solving techniques through practicing at Code.org.

• Visit https://studio.code.org/hoc/1 and play.

Play & Learn

101

• Note: You can select “Arabic” from the menu at the bottom.

Recommended