programming -1 (algorithmic problem solving).pdf · how to solve a problem? • programming is a...

58
Programming -1 Atheer Aldwighri Algorithmic problem solving

Upload: others

Post on 23-Feb-2020

22 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Programming -1

Atheer Aldwighri

Algorithmic problem solving

Page 2: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Objective

In this chapter you’ll:

• Learn about problem solving skills

• Explore the algorithmic approach for problem solving

• Problem Solving and Programming Strategy

• Flowchart

• General exercisers on algorithms

Page 3: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Problem Solving

• Programming is a process of problem solving

• Problem solving techniques

• Analyze the problem

• Outline the problem requirements

• Design steps (algorithm) to solve the problem

• Algorithm:

• Step-by-step problem-solving process

• Solution achieved in finite amount of time

Page 4: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

How to solve a problem?

• Programming is a problem-solving activity.

• If you are a good problem solver, you could become a good programmer.

• Problem-solving methods are covered in many subject areas:

• Business students learn to solve problems with a systems approach

• Engineering and Science students use the engineering and science methods

• Programmers use the Software Development Method

Page 5: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Important Problem Types

• Sorting

• Searching

• String processing (e.g. string matching)

• Graph problems (e.g. graph coloring problem)

• Combinatorial problems (e.g. maximizes a cost)

• Geometric problems (e.g. convex hull problem)

• Numerical problems (e.g. solving equations )

Page 6: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

What is an algorithm?

• Before a computer can perform a task, it musthave an algorithm that tells it what to do.

• Informally: “An algorithm is a set of steps that define how a task is performed.”

• Formally: “An algorithm is an ordered set of unambiguous executable steps, defining a terminating process.”

Every algorithm should have the following 5 feature:1. Input

2. Output

3. Definiteness

4. Effectiveness

5. Termination

Page 7: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Algorithms vs. programs

• When an algorithm is coded using any programming language (e.g. Java), then it is called a program.

• The program is a set of instructions that can run by the computer.

“computer program”

problem

algorithm

input

(or instance)

output

Page 8: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Characteristics of Algorithms

It should be accurate:

- It shouldn’t be ambiguous (each step in the

algorithm should exactly determine the action to be

done).

It should be effective:

- It shouldn’t include a step which is difficult to follow

by a person (or the computer) in order to perform it.

The algorithm should be finite:

- the algorithm has to end up in a point at which the task is complete

Page 9: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Representation of Algorithms

• A single algorithm can be represented in many ways:

• Formulas:

F = (9/5)C + 32

• Words: Multiply the Celsius by 9/5 and add 32.

• Flow Charts.

• In each case, the algorithm stays the same; the implementation differs!

Page 10: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Representation of Algorithms (Cont’d)

• A program is a representation of an algorithm

designed for computer applications.

• Process: Activity of executing a program, or execute the algorithm

represented by the program

• Process: Activity of executing an algorithm.

Page 11: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Problem Solving and Programming Strategy

• Programming is a process of problem solving.

• The problem is solved according to the problem domain (e.g. students, money).

• To be a good problem solver and hence a good programmer, you must follow good problem-solving technique.

Page 12: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

One problem solving technique to solve the problem includes:

- Analyzing the problem & outlining the problem’s requirements,

- Designing steps (writing an algorithm)

- Implementing the algorithm in a programming language (e.g. Java) and

verify that the algorithm works,

- Maintaining the program by using and modifying it if the problem domain

changes.

Page 13: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

(a) Problem Analysis

1- Thoroughly understand the problem

2- Understand the problem specifications.

3- If the problem is complex, you need to divide it into sub-problems and repeat steps

(1& 2).

That is, you need to analyze each sub-problem and understand its requirements.

Page 14: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Specifications can include the following:

- Does the problem require interaction with the

user?

- Does the problem manipulate data?

-What is the input data & how it is represented?

- Does the problem produce output? How the results should be generated and formatted.

- What are the required formula for solution?

- Is there any constraints on problem solution?

Page 15: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

• An Example for Problem Specifications:

Problem Statement:

Determine the total cost of apples given the number of kilos of applespurchased and the cost per kilo of apples.

We can summarize the information contained in the problem statement as follows:

Page 16: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

• Problem Input:

- Quantity of apples purchased (in kilos)

- Cost per kilo of apples (in dinars per kilo)

• Problem Output:

- Total cost of apples (in dinars)

• Formula:

Total cost = Number of kilos of apples × Cost per kilo

Page 17: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

(b) Algorithm Design and Testing

• The algorithm would consist of at least the

following tasks:

1- Input (Read the data)

2- Processing (Perform the computation)

3- Output (Display the results)

Page 18: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Structured Design:

Dividing the problem into smaller sub-problems is called “structured design”, “top-down design”,

Structured Programming:

In the structured design

- The problem is divided into smaller sub-problems

- Each sub-problem is analyzed

- A solution is obtained to solve the sub-problem

- The solutions of all sub-problems are combined

to solve the overall problem.

This process of implementing a structured design is called “structured programming”

Page 19: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

• Testing the Algorithm:

- You need to check the algorithm for correctness

- Use sample data for algorithm testing

Page 20: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

(c) Coding

• Coding:

- After verifying that the algorithm is correct,

you can code it in any high-level programming

language

- The algorithm is now converted into a program

Page 21: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

(d) Executing the Program

• Compile the program (to check for syntax error)

• Run the program. If the execution doesn’t go well, then reexamine

the code, the algorithm, or even the problem analysis.

Page 22: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Advantages of Structured Programming

• Easy to discover errors in a program that is well

analyzed and well designed.

• Easy to modify a program that is thoroughly

analyzed and carefully deigned.

Page 23: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Programs Flowchart

• It is another way to display the algorithm.

• It is composed of special geometric symbols connected by lines and contain the instructions.

• You can follow the lines from one symbol to another, executing the instructions inside it.

Page 24: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Flowchart

What is a Flowchart?

• The flowchart is a means of visually presenting the flow of control through an information processing systems, the operations performed within the system and the sequence in which they are performed.

• It is a graphic representation of how a process works, showing, at a minimum, the sequence of steps.

• Flowcharts are generally drawn in the early stages of formulating computer solutions.

Page 25: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Flowchart Symbols

• Start / End symbol

• Input/Output symbol

• Processing symbol

• Condition & decision symbol

• Continuation (connection symbol)

• Links

Page 26: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Flowchart Symbols

Guideline for drawing

a flowchart:Flowcharts are usually

drawn using some standard

symbols

Page 27: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Rules for writing flowcharts

• It should be drawn from top to bottom.

• A flowchart always begins with start symbol and ends with stop symbol.

• Flow lines are used to join the symbols

• Decision box should have one entry point and two exit points.

• For lengthy flowcharts, connectors are used to join them.

Page 28: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Advantages of using flowcharts:

• Communication: Flowcharts are better way of communicating the logic of a system

• Effective analysis: Problem can be analyzed in more effective way.

• Proper documentation: Flowcharts serve as a good program documentation

• Efficient Coding: Flowcharts act as a guide or blueprint during the systems analysis and program development phase.

Page 29: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Advantages of using flowcharts(cont…):

• Proper Debugging: Flowchart helps in debugging process.

• Efficient Program Maintenance: The maintenance of operating program becomes easy with the help of flowchart.

Page 30: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Limitations of using flowcharts:

• Complex logic: Sometimes, the program logic is quite complicated. In that case, flowchart becomes complex and clumsy.

• Alterations and Modifications: If alterations are required the flowchart may require re-drawing completely.

• Reproduction: As the flowchart symbols cannot be typed, reproduction of flowchart becomes a problem.

Page 31: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Programs Flowchart types

• Simple sequential (Sequence) Flowchart

• Branched Flowchart

• Loop Flowchart

Page 32: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Programs Flowchart types

• The algorithm and flowchart include following three types of control structures.

1. Sequence: In the sequence structure, statements are placed one after the other and the execution takes place starting from up to down.

2. Branching (Selection): In branch control, there is a condition and according to a condition, a decision of either TRUE or FALSE is achieved. In the case of TRUE, one of the two branches is explored; but in the case of FALSE condition, the other alternative is taken. Generally, the ‘IF-THEN’ is used to represent branch control.

3. Loop (Repetition): The Loop or Repetition allows a statement(s) to be executed repeatedly based on certain loop condition e.g. WHILE, FOR loops.

Page 33: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a
Page 34: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

.

Example – Sequential (Sequence)

Develop an algorithm that will calculate an hourly employee’s

weekly pay

Step 1 : Understand the problem

Input : pay rate and number of hours

Process: pay = rate * hours

Output: pay

Page 35: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

.

Algorithm – Calculate pay

Plain English

Ask for the pay rate and the number of hours worked. Multiply the pay

rate by the number of hours. The result is the pay

- looks like code, but not a real language1. Variables: hours, rate, pay

2. Display “Number of hours worked: ”

3. Get hours

4. Display “Amount paid per hour: ”

5. Get rate

6. pay = hours * rate

7. Display “The pay is $” , pay

Notice the importance of order and lack of ambiguity

Page 36: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Example - Flowchart

Start

End

Display “Number

of hours

worked: ”

Get hours

Display

“Amount paid

per hour: ”

Get rate

pay = hours * rate

Display “The

pay is $”, pay

Page 37: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

.

Example – Decision Making

Develop an algorithm that will figure out if a number is

positive

Step 1 : Understand the problem

Input : The number

Process: Is it greater than zero?

Output: Message that number is positive or non-positive

Page 38: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

.

Algorithm

Plain English

Ask for the number. Check if it is greater than zero. If it is, it is a

positive number. If not (i.e. else), it is not positive1. Variable: num

2. Display “Enter the number: ”

3. Get num

4. if num > 0

5. Display “It is positive”

6. Else

7. Display “It is not positive”

Page 39: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Flowcharts-Decision Making Example

num >0?

Display

“positive”

Display “Not

positive”

True False

If num > 0 display “Positive”

Else (that means 0 or negative) display “Not Positive”

Page 40: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Looping

Develop an algorithm that will add the numbers from 1 to 10. That is

1 + 2 + 3 + …+ 10 = 55

Step 1 : Understand the problem

Input : None needed

Process: Add 0 to 1 to get a new sum. Add 2 to the old sum to get a new sum. Add

3 to the old sum to get a new sum……Add 10 to the old sum to get a new sum

Output: The new sum after 10 iterations

Page 41: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

.

Algorithm

Plain English

Start count at 1. Add to the sum (originally zero) to get a new sum.

Increment the count. Repeat the last two steps 10 times

While_Example1. Numeric Variables: counter = 1, sum = 0

2. While counter <= 10:

3. sum = sum + counter

4. counter = counter + 1

5. EndWhile

6. Display “ The sum is”, sum

Page 42: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

General Exercisers

Page 43: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Example 1:

1- Write the algorithm and then draw flowchart that

purchase a book from the bookstore??

43

Page 44: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Example 1:

44

Start

Order book

Receive bill

Bay bill

Stop

1. Start

2. Order book

3. Receive bill

4. Bay bill

5. Stop

Page 45: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Example 2:

2-Write the algorithm and then draw flowchart to find the area of circle

A, and the circumference of circle C, radius known R ??

Note:

that the area of the circle = PI × R 2

Circumference = R × PI × 2

PI = 3.14

45

Page 46: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Example 2:

1. Start

2. Read R from user

3. Store the value of PI = 3.14

4. Calculate the area of the circle from the equation PI × A = R 2

5. Calculate the circumference of the circle from equation R × PI × 2C

6. print A and C

7. Stop

46

Page 47: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Example 1:

T. Atheer Aldwighri

Start

Let PI=3.14

Let A=PI*R2

Let C=2PI*R

Read R

Print A,C

Stop

Page 48: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Example 3:

3- write the algorithm and then draw flowchart to find the maximum

value as the following formula:

M = max( A , B)

48

Page 49: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Example 3:

49

1. Start

2. Read A and B.

3. If A> B go to step 4, else go to step 5.

4. M=A, then go to step 6

5. M=B

6. Print M

7. Stop

Page 50: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Example 3:

50

Start

Let M =B

Read A,B

A > B

Let M=A

Print M

Stop

YesNo

Page 51: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Example 4:

4-Draw flowchart to find the following value:

51

x + 1 , x > 0

W(x) = sin(x) + 5 , x = 0

2 x +1 , x < 0

Page 52: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

T. Atheer Aldwighri

Example 4:Start

W= 2X-1

Read X

X ?

X+1=W

Print X & W(x)

End

Sin(X)+5=W

X>0X<0

X=0

Page 53: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Example 5:

5- write the algorithm and then draw flow chart For the tank self-fills

with water, when it becomes high 1m?

53

Page 54: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Example 5:

54

• 1: Start

• 2: Is the water level less than a meter?

• If yes go to step 3, else go to 5

• 3: Open the feeding tap

• 4: Fill the tank

• 5: Close the tap

• 6: go to 2 to check the water level

Start

Level<1m?

No

Open feeding Tap

yes

Fill tank

Keep Tap closed

Page 55: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Example 6:

6- Write the algorithm and then draw flowchart that prints numbers

from 1 to 100 and its squares?

55

Page 56: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Example 6:

56

1. Start

2. I=0

3. I=I+1

4. J=I*I

5. Print I , J

6. Is I=100? if the answer is yes then go to step 7

else go to step 3.

7. Stop

Page 57: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Example 6:

57

start

I <=100

No

yes

Let I=0

Let I = I + 1

Let J = I * I

stop

Print I , J

Page 58: Programming -1 (Algorithmic problem solving).pdf · How to solve a problem? • Programming is a problem-solving activity. • If you are a good problem solver, you could become a

Summary

• Learn about problem solving skills

• Become aware of problem-solving process

• Explore the algorithmic approach for problem solving

• Flowchart

• Problem Solving and Programming Strategy

• General exercisers on algorithms