knowt logo

Algorithm design and problem solving

Section 9.1: Computational Thinking Skills

1. Abstraction:

Definition: Focuses on the essential details of a problem, ignoring irrelevant information.

Examples:

• A road map highlights roads and important landmarks, omitting terrain details.

• Timetables show times, ignoring how vehicles operate.

Benefits:

• Faster program development.

• Smaller program size, requiring less memory.

• Meets user requirements efficiently.

2. Decomposition:

Definition: Breaking down a complex problem into smaller, manageable parts.

Example:

• Problem: Design a program to calculate salaries.

• Subtasks: Gather inputs (hours, rate), calculate salaries, and display results.

3. Pattern Recognition:

Definition: Identifying repeating patterns within a problem to simplify solutions.

Example:

• Converting several currency values using the same conversion logic.

Section 9.2: Algorithms

1. Definition:

• An algorithm is a sequence of steps to solve a problem.

Key Forms:

Structured English: Describes logic in simple English.

Flowcharts: Visual representation using symbols.

Pseudocode: Precise but language-independent representation.

2. Writing Algorithms:

Structured English Example:

1. Ask for the number of values.

2. Loop that many times:

     - Input a value.

     - Add to total.

3. Calculate and output average.

Flowchart:

• Use symbols for start/end, input/output, decisions, and processes.

Pseudocode Example:

INPUT NumberOfValues

Total = 0

FOR i = 1 TO NumberOfValues

    INPUT Value

    Total = Total + Value

NEXT i

Average = Total / NumberOfValues

OUTPUT Average

Practice Question Example: Temperature Conversion Algorithm

Problem:

Write an algorithm to convert between Celsius and Fahrenheit.

Pseudocode:

REPEAT

    OUTPUT "1. Celsius to Fahrenheit"

    OUTPUT "2. Fahrenheit to Celsius"

    OUTPUT "3. Exit"

    INPUT Choice

    IF Choice = 1 OR Choice = 2 THEN

        OUTPUT "Enter temperature"

        INPUT Temperature

        IF Choice = 1 THEN

            ConvertedTemperature = 1.8 * Temperature + 32

        ELSE

            ConvertedTemperature = (Temperature - 32) * 5 / 9

        ENDIF

        OUTPUT "Converted Temperature is ", ConvertedTemperature

    ELSE IF Choice <> 3 THEN

        OUTPUT "Error in choice"

    ENDIF

UNTIL Choice = 3

Solution for Input:

• Input: Choice = 1, Temperature = 25°C.

• Output: Converted Temperature is 77°F.

Stepwise Refinement

Definition: Breaking down a task into smaller, manageable parts repeatedly.

Example:

• Task: Calculate the area of a chosen shape.

• Step 1: Choose the shape.

• Step 2: Input dimensions.

• Step 3: Calculate area based on the formula.

• Step 4: Display the result.

Refined Pseudocode for Area Calculation:

OUTPUT "Choose shape (circle, triangle, square)"

INPUT Shape

IF Shape = "circle" THEN

    OUTPUT "Enter radius"

    INPUT Radius

    Area = 3.14 * Radius^2

ELSE IF Shape = "triangle" THEN

    OUTPUT "Enter base and height"

    INPUT Base, Height

    Area = 0.5 Base Height

ELSE IF Shape = "square" THEN

    OUTPUT "Enter side length"

    INPUT Side

    Area = Side^2

ENDIF

OUTPUT "Area is ", Area

Practice Questions (Page 236)

1. Expressions Evaluation:

Given Variables:

h = 13.6, w = 6.4, r = 10

Perimeter = (h + w) * 2

Area = 3.142 * r^2

Z = 11 + r / 5 + 3

A = NOT(r > 10)

Answers:

• Perimeter = (13.6 + 6.4) * 2 = 40.

• Area = 3.142 * 10^2 = 314.2.

• Z = 11 + 10 / 5 + 3 = 16.

• A = NOT(10 > 10) = True.

2. Stepwise Refinement Practice:

• Describe stepwise refinement using the algorithm for calculating salaries:

Solution:

1. Input employee details (hours worked, pay rate).

2. Calculate salary: Salary = HoursWorked * PayRate.

3. Display the result.

Algorithm design and problem solving

Section 9.1: Computational Thinking Skills

1. Abstraction:

Definition: Focuses on the essential details of a problem, ignoring irrelevant information.

Examples:

• A road map highlights roads and important landmarks, omitting terrain details.

• Timetables show times, ignoring how vehicles operate.

Benefits:

• Faster program development.

• Smaller program size, requiring less memory.

• Meets user requirements efficiently.

2. Decomposition:

Definition: Breaking down a complex problem into smaller, manageable parts.

Example:

• Problem: Design a program to calculate salaries.

• Subtasks: Gather inputs (hours, rate), calculate salaries, and display results.

3. Pattern Recognition:

Definition: Identifying repeating patterns within a problem to simplify solutions.

Example:

• Converting several currency values using the same conversion logic.

Section 9.2: Algorithms

1. Definition:

• An algorithm is a sequence of steps to solve a problem.

Key Forms:

Structured English: Describes logic in simple English.

Flowcharts: Visual representation using symbols.

Pseudocode: Precise but language-independent representation.

2. Writing Algorithms:

Structured English Example:

1. Ask for the number of values.

2. Loop that many times:

     - Input a value.

     - Add to total.

3. Calculate and output average.

Flowchart:

• Use symbols for start/end, input/output, decisions, and processes.

Pseudocode Example:

INPUT NumberOfValues

Total = 0

FOR i = 1 TO NumberOfValues

    INPUT Value

    Total = Total + Value

NEXT i

Average = Total / NumberOfValues

OUTPUT Average

Practice Question Example: Temperature Conversion Algorithm

Problem:

Write an algorithm to convert between Celsius and Fahrenheit.

Pseudocode:

REPEAT

    OUTPUT "1. Celsius to Fahrenheit"

    OUTPUT "2. Fahrenheit to Celsius"

    OUTPUT "3. Exit"

    INPUT Choice

    IF Choice = 1 OR Choice = 2 THEN

        OUTPUT "Enter temperature"

        INPUT Temperature

        IF Choice = 1 THEN

            ConvertedTemperature = 1.8 * Temperature + 32

        ELSE

            ConvertedTemperature = (Temperature - 32) * 5 / 9

        ENDIF

        OUTPUT "Converted Temperature is ", ConvertedTemperature

    ELSE IF Choice <> 3 THEN

        OUTPUT "Error in choice"

    ENDIF

UNTIL Choice = 3

Solution for Input:

• Input: Choice = 1, Temperature = 25°C.

• Output: Converted Temperature is 77°F.

Stepwise Refinement

Definition: Breaking down a task into smaller, manageable parts repeatedly.

Example:

• Task: Calculate the area of a chosen shape.

• Step 1: Choose the shape.

• Step 2: Input dimensions.

• Step 3: Calculate area based on the formula.

• Step 4: Display the result.

Refined Pseudocode for Area Calculation:

OUTPUT "Choose shape (circle, triangle, square)"

INPUT Shape

IF Shape = "circle" THEN

    OUTPUT "Enter radius"

    INPUT Radius

    Area = 3.14 * Radius^2

ELSE IF Shape = "triangle" THEN

    OUTPUT "Enter base and height"

    INPUT Base, Height

    Area = 0.5 Base Height

ELSE IF Shape = "square" THEN

    OUTPUT "Enter side length"

    INPUT Side

    Area = Side^2

ENDIF

OUTPUT "Area is ", Area

Practice Questions (Page 236)

1. Expressions Evaluation:

Given Variables:

h = 13.6, w = 6.4, r = 10

Perimeter = (h + w) * 2

Area = 3.142 * r^2

Z = 11 + r / 5 + 3

A = NOT(r > 10)

Answers:

• Perimeter = (13.6 + 6.4) * 2 = 40.

• Area = 3.142 * 10^2 = 314.2.

• Z = 11 + 10 / 5 + 3 = 16.

• A = NOT(10 > 10) = True.

2. Stepwise Refinement Practice:

• Describe stepwise refinement using the algorithm for calculating salaries:

Solution:

1. Input employee details (hours worked, pay rate).

2. Calculate salary: Salary = HoursWorked * PayRate.

3. Display the result.

robot