Taught by: Dr. Ahmed Shawky Moussa, Dr. Hisham Othman ... is True; selects the next step to do...

17
Taught by: Dr. Ahmed Shawky Moussa, Dr. Hisham Othman Slides are based on: Prof. Dr. Slim Abdennadher’s slides German University Cairo, Department of Media Engineering and Technology

Transcript of Taught by: Dr. Ahmed Shawky Moussa, Dr. Hisham Othman ... is True; selects the next step to do...

Page 1: Taught by: Dr. Ahmed Shawky Moussa, Dr. Hisham Othman ... is True; selects the next step to do Notation Use the same primitives as before plus the following: Execution Evaluate

Taught by: Dr. Ahmed Shawky Moussa, Dr. Hisham OthmanSlides are based on: Prof. Dr. Slim Abdennadher’s slides

German University Cairo, Department of Media Engineering and Technology

Page 2: Taught by: Dr. Ahmed Shawky Moussa, Dr. Hisham Othman ... is True; selects the next step to do Notation Use the same primitives as before plus the following: Execution Evaluate

How to construct an algorithm Identify the input of the algorithm (if needed)

Introduce variables for

Input

(intermediate) results

Analyze the task into steps

Provide for detailed output

Page 3: Taught by: Dr. Ahmed Shawky Moussa, Dr. Hisham Othman ... is True; selects the next step to do Notation Use the same primitives as before plus the following: Execution Evaluate

Objectives By the end of this lecture, you should be able to:

Design algorithms using conditional operations

Page 4: Taught by: Dr. Ahmed Shawky Moussa, Dr. Hisham Othman ... is True; selects the next step to do Notation Use the same primitives as before plus the following: Execution Evaluate

Algorithms: operations Algorithms can be constructed by the following

operations:

Sequential Operations

Conditional Operations

Iterative Operations

Page 5: Taught by: Dr. Ahmed Shawky Moussa, Dr. Hisham Othman ... is True; selects the next step to do Notation Use the same primitives as before plus the following: Execution Evaluate

Conditional operation – idea Decision

name_of_speaker = input(“Tell who is the speaker:”)

if name_of_speaker == “Einstein" :

print("I will go home")

else:

print("I will stay at the GUC")

Page 6: Taught by: Dr. Ahmed Shawky Moussa, Dr. Hisham Othman ... is True; selects the next step to do Notation Use the same primitives as before plus the following: Execution Evaluate

Conditional operation – principle Rationale

Determines whether or not a condition is True; and based on whether or not it is True; selects the next step to do

Notation Use the same primitives as before plus the following:

Execution Evaluate <condition> expression to see whether it is True or False. If True, then execute operations in if-part Otherwise, execute operations in else-part

if condition:

# <operations for the if-part>

else:

# <operations for the else-part>

Page 7: Taught by: Dr. Ahmed Shawky Moussa, Dr. Hisham Othman ... is True; selects the next step to do Notation Use the same primitives as before plus the following: Execution Evaluate

Conditional operation – diagram Example:

Write an algorithm that takes a number as an input and prints out a message indicating whether the number is negative or positive.

Page 8: Taught by: Dr. Ahmed Shawky Moussa, Dr. Hisham Othman ... is True; selects the next step to do Notation Use the same primitives as before plus the following: Execution Evaluate

Conditional operation – diagram Example:

Write an algorithm that takes a number as an input and prints out a message indicating whether the number is negative or positive.

Page 9: Taught by: Dr. Ahmed Shawky Moussa, Dr. Hisham Othman ... is True; selects the next step to do Notation Use the same primitives as before plus the following: Execution Evaluate

Conditional operation – diagram

solution:

num = eval(input())

if num < 0:

print("The number is negative")

else:

print("The number is positive")

print("Good-bye for now")

Page 10: Taught by: Dr. Ahmed Shawky Moussa, Dr. Hisham Othman ... is True; selects the next step to do Notation Use the same primitives as before plus the following: Execution Evaluate

Conditional operation – examples Example 1:

Write an algorithm to compute the absolute value of a given number.

solution:

Number = int(input())

if (Number >= 0):

Value = Number

else:

Value = (-1) * Number

print(Value)

Page 11: Taught by: Dr. Ahmed Shawky Moussa, Dr. Hisham Othman ... is True; selects the next step to do Notation Use the same primitives as before plus the following: Execution Evaluate

Conditional operation – examples Example 2:

Give the user a choice of seeing the area or the circumference of a circle given its radius.

solution:

Page 12: Taught by: Dr. Ahmed Shawky Moussa, Dr. Hisham Othman ... is True; selects the next step to do Notation Use the same primitives as before plus the following: Execution Evaluate

Conditional operation – examples Example 2:

Give the user a choice of seeing the area or the circumference of a circle given its radius.

solution:

radius = eval(input(“Enter radius:”))

response = input("Type A for area or C for circumference:")

if (response == "A"):

area = (radius * radius * 3.14)

print(area)

else:

circumference = (2 * radius * 3.14)

print(circumference)

Page 13: Taught by: Dr. Ahmed Shawky Moussa, Dr. Hisham Othman ... is True; selects the next step to do Notation Use the same primitives as before plus the following: Execution Evaluate

Conditional operation – examples Example 3:

Write an algorithm to convert Euro (EUR) to Egyptian Pound (EGP) and Egyptian Pound to Euro. The inputs to your algorithm are the following:

Amount of money to be converted

Conversion Type, i. e.:

1 for EUR to EGP

2 for EGP to EUR

Exchange Rate (i. e., a EUR is equivalent to 20 EGP)

Page 14: Taught by: Dr. Ahmed Shawky Moussa, Dr. Hisham Othman ... is True; selects the next step to do Notation Use the same primitives as before plus the following: Execution Evaluate

Conditional operation – examples Solution 3:

amount, type, rate = eval(input()), eval(input()),

eval(input())

if type == 1:

amount = amount * rate

else:

amount = amount / rate

print(amount)

Page 15: Taught by: Dr. Ahmed Shawky Moussa, Dr. Hisham Othman ... is True; selects the next step to do Notation Use the same primitives as before plus the following: Execution Evaluate

Compounded conditions Conditions may be compounded using:

AND, OR, NOT

E1 or E2: True if at least one of them is True; False otherwise.

E1 and E2: True if both are True; False otherwise.

not E: True if E is False and False if E is True.

Page 16: Taught by: Dr. Ahmed Shawky Moussa, Dr. Hisham Othman ... is True; selects the next step to do Notation Use the same primitives as before plus the following: Execution Evaluate

Compounded conditions –examples Example 4:

Find the sum of three positive numbers

solution:

A, B, C = eval(input()), eval(input()), eval(input())

if (A > 0) and (B > 0) and (C > 0):

Sum = (A+B+C)

print(Sum)

Page 17: Taught by: Dr. Ahmed Shawky Moussa, Dr. Hisham Othman ... is True; selects the next step to do Notation Use the same primitives as before plus the following: Execution Evaluate

Any Question?