Download Steps involved in Problem Solving

Document related concepts

Location arithmetic wikipedia , lookup

Arithmetic wikipedia , lookup

Factorization of polynomials over finite fields wikipedia , lookup

Elementary arithmetic wikipedia , lookup

Addition wikipedia , lookup

Elementary mathematics wikipedia , lookup

Halting problem wikipedia , lookup

Transcript
STEPS INVOLVED IN PROBLEM SOLVING


Problem Solving in Everyday in Life
People make decisions everyday – give examples


What happens when bad decisions are made in business?


Should I watch TV or go out to cinema, what career, what course?
Wrong decisions cost money; time and resources are wasted, important to know how to make a
decision.
Steps involved in Problem Solving (general)
1) Identify/understand the problem
 If you don’t know what the problem is you cant solve it (understand what is
involved in a problem)
2) Identify alternative ways to solve the problem
 Make the list complete
 Talk to other people to find solutions you not thought of
 If alternatives are incomplete
 Is the alternative solution feasible.
3) Select the best way to solve the problem from the list of alternative
solutions


Identify and evaluate pros and cons of each possible solution before selecting the best one.
To do this select criteria for the evaluation ,this will serve guidelines for evaluating each1
solution
STEPS INVOLVED IN PROBLEM SOLVING
4) List instructions that enable you to solve the problem using
selected solution

Step by step instructions (make sure the person who use these
can understand it – know the persons knowledge base (what
they know limited)
5) Evaluate the solution


Evaluate or test the solution means to check the result to see
if its correct and to see if it satisfies the needs of the person
with a problem (e.g. if you need a bed buying a cot may not be
satisfactory)
What makes a good decision?
Well identified problem
All alternatives considered
 Information overloaded – appropriate alternatives
 Can the person carry out steps/instructions


2
TYPES OF PROBLEMS





Algorithmic solutions – problems that can be solved with a
series of actions (the steps are called algorithms –
computers work well on these)
Heuristic (‫)استكشافي‬solutions: – solutions that cannot be
solved through a direct set of steps – how to buy the best
stock? Need: knowledge, experience, try and error
Problem solving with computers
Computers use algorithmic solutions
Is to broken down the tasks into a sequence of instructions
that can be expressed in a computer language by a
program.
Determine the instructions required
Use the instructions to produce the desired results
 Write computer instructions to carry out solution instructions
and acquire (get) specified results


3
DEFINITIONS

Program – set of instructions that make up solution to a problem

Common features for all programs:




Take in data
Manipulate them
Give desired information
Computer software is …

The collection of programs used by a computer

Includes:






Editors
Translators
System Managers
Results – outcome of running the program
Testing – Are the outcomes what you expected and correct
Documentation – two types


for the programmer – specification documentation / comments in code
manual documentation – instructions telling users how to use the
program
4
DEFINITIONS
High-level Languages
 Common programming languages include …

C C++ Java Pascal Visual Basic
COBOL Lisp Scheme Ada

FORTRAN
These high – level languages




Resemble (look like) human languages
Are designed to be easy to read and write
Use more complicated instructions than
the CPU can follow
Must be translated to zeros and ones for the CPU
to execute a program
5
DEFINITIONS
 Low-level
Languages
 An assembly language command such as
ADD X Y Z
might mean add the values found at x and
y
in memory, and store the result in
location z.
 Assembly
language must be translated to
machine language (zeros and ones)
0110 1001 1010 1011
 The CPU can follow machine language
6
DEFINITIONS
Compilers
 Translate high-level language to
machine language


Source code


The original program in a high level language
Object code

The translated version in machine language
7
8
PROGRAM DESIGN
 Programming

No complete set of rules for creating a program
 Program

Design Process
Problem Solving Phase


is a creative process
Result is an algorithm that solves the problem
Implementation Phase

Result is the algorithm translated into a
programming
language
9
PROBLEM SOLVING PHASE
 Be



certain the task is completely specified
What is the input?
What information is in the output?
How is the output organized?
 Develop
the algorithm before
implementation


Experience shows this saves time in getting
your program to run.
Test the algorithm for correctness
10
IMPLEMENTATION PHASE




Translate the algorithm into a programming
language
 Easier as you gain experience with the
language
Compile the source code
 Locates errors in using the programming
language
Run the program on sample data
 Verify correctness of results
Results may require modification of
the algorithm and program
11
12
EXAMPLE: PROBLEM ANALYSIS CHART FOR THE
PAYROLL PROBLEM
13
PROGRAMMING



CONCEPTS
WE have three basic concepts in programming
ITERATION, SEQUENCE
- SELECTION ,
In everyday life – we are asked to make decisions, but how do we make
decisions? We present ourselves with a number of alternatives / options we
devise to solve the problem that we have. Then we decide the pros and cons
for each option and we select the solution we feel best meets our objective.
Let take a very simple example, consider the traffic lights, the lights have
three colours, each colour provides us with an instruction whether to go
ahead (green) stop (red) or get ready to go (amber). The traffic light is an
example of ‘the programming concept’ we refer to as SELECTION
14
DECISION LOGIC STRUCTURE
15
PROGRAMMING



CONCEPTS
In your daily work you may make a list of things to do in a day.
These tasks may follow each other in a specific order for example
the routine of going to work may involve you getting up getting
dressed having breakfast catching a bus and then starting work.
So the tasks are done in a SEQUENCE
Getting up  getting dressed  having breakfast  catching a
bus  starting work
This an example of a ‘programming concept’ we refer to as
SEQUENCE
16
SEQUENTIAL LOGIC STRUCTURE
Sequential logic structure – we ask the computer to
process a set of instructions in sequence from the
top to bottom of an algorithm
17
PROGRAMMING








CONCEPTS
Let look at the third programming concept – ITERATION
Have you found yourself doing certain things over and over again?
Maybe you go shopping a few times a week
Monday
Tuesday
Wednesday
Wake up
Wake up
Wake up
Get into car
Get into car
Get into car
Do shopping
Do shopping
Do shopping
Come home
Come home
Come home
18
LOOP LOGIC STRUCTURE
19
WRITING ALGORITHMS







After using the structure chart the next step in organising the
solution is to for the programmer to develop a set of instructions
for the computer – called algorithms or Psuedocode.
An algorithm is set of instructions needed to solve the problem
An algorithm is a finite number of steps ( ordered actions ) to
execute for solving a problem in finite time. (without grammar)
Pseudocode:- informal language that helps programmers
develop algorithms without having to worry about the details of
language syntax.
Some Programming concepts
Relational Operators :- > greater than x > y
<
less than
x<y
=
equals
x==y
<>
not equals
x <> y
>= is greater than or equal to x >= y
<= is less than or equal to x <= y
20
Conditional logic – if then else
WRITING ALGORITHMS
SEQUENCE
1.
Write an algorithm of a program that reads the radius of a
sphere and calculates its surface area and volume
where, surface area= 4* pi * (radius)2
pi= 3.14
volume= radius * surface area/3
Solution
1.
2.
3.
4.
5.
6.
7.
8.
input: pi=3.14, radius
output :surface area, volume
start
set pi=3.14
read radius from the user
calculate surface area= 4* pi * (radius)2
calculate volume= radius * surface area/3
print surface area, volume
end
21
WRITING ALGORITHMS
SELECTION
2.
Write an algorithm for a program to print out the sum
of two numbers only and only if the two numbers are
positive.
Solution
1.
input: x and y
2.
Start
3.
Read x, y
4.
If x>0 and Y>0 then


5.
output: sum of x and y if they are positive
Sum=x + y
Print sum
End
22
WRITING ALGORITHMS
SELECTION
3. Write an algorithm to add two Numbers. Print "fail" if the
total less than 50, otherwise print "Pass“
Solution
1.input:
two numbers A,B
output: the state of student (“fail", "pass”)
2.
START
3.
Read A,B
4.
Calculate C=A+B
5.
Check if C<50 then Print "FAIL“
6.
Else (c>=50) Print "Pass”
7.
END
23
WRITING ALGORITHMS
SELECTION
4.
Write an algorithm of a program that reads an
integer y to solve the following expression:
y+10
if y odd (‫)فردي‬
y=
y-10
if y even (‫)زوجي‬
Solution:
1. input: y
output: new y
2. Start
3. Read y
4. If reminder (y/2)=0 (y mode 2)then y=y-10
5. Else if y mode 2 not equal to 0 then y=y+10
6. Print y
7. End
24
WRITING ALGORITHMS
SELECTION
5. Write a pseudocode / algorithm to work out what grade a student
got based on the following information :- if student grade (above
or equal) to 90 – A , 80 – B, 70 – C, 60 – D, less then or equal 60 – F
Solution:
1. input: grade
output: letter-grade
2. start
3. read grade
4. If students grade is greater than or equal to 90
Display ‘A’
5.
Else If students grade is greater than or equal to 80
Display ‘B’
6.
Else If students grade is greater than or equal to 70
Display ‘C’
7.
Else If students grade is greater than or equal to 60
Display ‘D’
8.
Else
Display ‘f’
9.
End
25
WRITING ALGORITHMS
SELECTION
6.
Write a pseudo code for a program that reads two
numbers and prints the maximum number or
print message that the numbers are equal.
Solution
1.
input: two numbers (x, y)
2.
Start
3.
Read x and y
4.
If x>y then print x
5.
Else if y>x then print y
6.
Else print “x=y”
7.
end
output: max number
26
WRITING ALGORITHMS
ITERATION
7. Write an algorithm/pseudo code to add two marks. Print
"fail" if the total less than 50, otherwise print "Pass". Do it
for N different pairs of marks.
Solution:
1. input: two numbers (A,B), N- number of students
students (“fail"," pass”)
2. START
3. Read N
4. Set counter=0
5. While counter <N repeat the following
- Read A,B
- Calculate C=A+B
- Check if C<50 then Print "FAIL“
- Else (c>=50) Print "Pass”
- add one to the counter
6. END
output: the state of N
27
WRITING ALGORITHMS
ITERATION
8. Write an algorithm/pseudocode for a program that
calculates and prints the value of X. where X=
1/2+2/4+3/8…..+n/2n the value of n is entered by user.
Solution:
1. input: n for the last term output: X-sum of terms
2. START
3. Read n
4. Set c=1
5. Set sum=0
6. While c <=n repeat the following
-calculate term=c/2c
- Calculate sum=sum + term
- add one to the counter c=c+1
7. Print sum
8. END
28
WRITING ALGORITHMS
ITERATION
9.
Write an algorithm to find the Sum of 100+110+120+…
for N terms.
Solution:
1.
2.
3.
4.
5.
6.
7.
input: N-number of terms
Start
Read N
Set sum=0
Set counter =0
Set term=100
Do the following
1.
2.
3.
8.
9.
10.
output: sum of the series
add term to the sum: sum = sum + term
calculate the new term by adding 10 to the previous one: term =
term +10
add one to the counter : counter= counter + 1
Repeat the above while the counter < N
Print sum
end
29
WRITING ALGORITHMS
ITERATION
10.
Write an algorithm to find the Sum of 100+110+120+…
for N terms.
Solution:
1. input: N-number of terms
output: sum of the series
2. Start
3. Read N
4. Set sum=0
5. Set counter =0
6. Set term=100
7. Repeat the following while the counter < N
add term to the sum: sum = sum + term
2. calculate the new term by adding 10 to the previous one: term = term
+10
3. add one to the counter : counter= counter + 1
1.
8.
9.
Print sum
end
30
WRITING ALGORITHMS
ITERATION
11.
Write an algorithm of a program that reads a sequence of 20 integer
values.
The program will count the number of positive numbers and calculates
their sum. Also the program counts the number of negative numbers and
calculates their sum. Then it prints the results.
Solution
1. input: 20 integer values
output: number of +ves and their sum , number of –ves and their
sum
2. Start
3. Set sump=0,sumn=0
4. Set countp=0,countn=0,countt=0
5. While the countt<20 repeat the following
- Read value
- If value >= 0 then
- countp=countp+1
- sump=sump+value
- Else
- countn=countn+1
- sumn=sumn+value
- countt+=countt+1
6. Print sump , countp , sumn , countn
7. End
31
WRITING ALGORITHMS
ITERATION
12.
Write an algorithm of a program that reads a sequence of 20 integer values.
The program will count the number of positive numbers and calculates their
sum. Also the program counts the number of negative numbers and calculates
their sum. Then it prints the results.
Solution
1.
2.
3.
4.
5.
6.
7.
8.
input: 20 integer values output: number of +ves and their sum , number of –ves and their sum
Start
Set sump=0,sumn=0
Set countp=0,countn=0,countt=0
do
- Read value
- If value >= 0 then
- countp=countp+1
-sump=sump+value
- Else
- countn=countn+1
- sumn=sumn+value
- countt+=countt+1
While the countt<20 repeat
Print sump , countp , sumn , countn
En
32
WRITING ALGORITHMS
ITERATION
13.
Write an algorithm of a program that reads a sequence of 20 integer
values. The program will find and print the maximum value
Solution
1.
input: 20 integer values output: max value
2.
Start
3.
Read value
4.
Set max = value
5.
Set c=1
6.
While c <20 then repeat the following
- read value
- if value>max
- max = value
- c =c+1
7.
Print max
8.
End
33
WRITING ALGORITHMS
ITERATION
14. Write an algorithm/pseudocode for a program that calculates
and prints the value of X. where X= 1/2+2/4+3/8…+…. Your
calculations stopped only and only if one of terms become less
than 0.00001.
Solution:
1. input: last term< 0.00001 output: X-sum of terms
2. START
3.Set term=1/2
4.Set c=2
5. Set sum=0
6. While term >= 0.00001 repeat the following
- Calculate sum=sum+term
- calculate term=c/2c
- add one to the counter c=c+1
7. Print sum
8. END
34
WRITING ALGORITHMS
ITERATION (CONDITIONAL LOOP)
15. Write an algorithm of a program that reads an unspecified number of
integers (your program ends with negative input), determines and prints
how many odd and even values have been read, and computes and prints
the total and average of the input values.
 Solution
1. input: value output: number of odds , number off evens, total sum average
2. start
3. set codd=0, ceven=0, ctotal=0, total=0
4. read value
5. While value >= 0 then
- total=total+value
- ctotal=ctotal+1
- if value mod 2 = 0 then
- ceven = ceven + 1
- else
- codd = codd +1
- read value
6. average= total / ctotal
7. print codd, ceven, ctotal, total, average
35
8. end
WRITING ALGORITHMS
ITERATION (CONDITIONAL LOOP)
16. Suppose a teacher has 10 sections and each section has 20 students.
The teacher want to calculate and print the average mark for each
section and the average for all sections. You are to write an
algorithm for a program to solve this problem.
Solution
1.
input: no of sections, no of students output: section average,
average for sections
2.
3.
4.
Start
Set i=0, totalSum = 0
While the i<10 repeat the following
1.set j=0, sum = 0
2. While the j<20 repeat the following
- read mark
- sum = sum + mark
- j=j+1
3. avg = sum / 20
4. print avg
5. totalSum = totalSum + avg
6. i=i+1
5.
6.
7.
totalAvg = totalSum / 10
print totalAvg
End
36
WRITING ALGORITHMS
ITERATION
17. Write an algorithm of a program that prints out the 10th
multiplication table [0x0,0x1,…10X10]
Solution
1.
input: starting value of multiplication operands
multiplication
table
2.
Start
3.
Set i=0
4.
While the i<=10 repeat the following
1.set j=0
2. While the j<=10 repeat the following
output:
- r=I *J
- print r
- j=j+1
3. i=i+1
5.
End
37
WRITING ALGORITHMS
38
ALGORITHMS TO FLOWCHARTS
From algorithms the programmer develops flowcharts
 Flowcharts are the Graphical representations of the
algorithms
 A flowchart shows the flow of processing form
beginning to end
 Each block in flowchart represents one instruction
from an algorithm
 Flow lines indicate direction of data flow
 Why use flowcharts?



Flowcharts show errors in logic
data can be tested easily using flowcharts
39
FLOW CHART SYMBOLS

Algorithms will be prepared with flow charts, which are going to be drawn
with below given symbols:
To start/end the program
To input data/output result
For calculations
For decision
For subroutines
For directions
For connections
For repetitions
40
FLOWCHART SYMBOLS
41
FLOWCHART SYMBOLS
42
FLOWCHART SYMBOLS
43
1. DRAW A FLOWCHART OF A PROGRAM THAT READS THE RADIUS OF A
SPHERE AND CALCULATES ITS SURFACE AREA AND VOLUME WHERE,
SURFACE AREA= 4* PI * (RADIUS)2, PI= 3.14, VOLUME= RADIUS * SURFACE
AREA/3
44
2. DRAW A FLOWCHART FOR A PROGRAM TO PRINT OUT THE SUM OF
TWO NUMBERS ONLY AND ONLY IF THE TWO NUMBERS ARE
POSITIVE.
45
3. DRAW A FLOWCHART TO ADD TWO NUMBERS. PRINT "FAIL" IF
THE TOTAL LESS THAN 50, OTHERWISE PRINT "PASS“
46
4. DRAW A FLOWCHART OF A PROGRAM THAT READS AN INTEGER Y TO
SOLVE THE
FOLLOWING EXPRESSION:
Y+10
IF Y ODD
Y=
Y-10
IF Y EVEN
47
5. DRAW A FLOWCHART TO WORK OUT WHAT GRADE A STUDENT GOT BASED
ON THE FOLLOWING INFORMATION :- IF STUDENT GRADE (ABOVE OR EQUAL)
TO 90 – A , 80 – B, 70 – C, 60 – D, LESS THEN OR EQUAL 60 – F
48
6. DRAW A FLOWCHART FOR A PROGRAM THAT READS TWO
NUMBERS AND PRINTS THE MAXIMUM NUMBER OR PRINT MESSAGE
THAT THE NUMBERS ARE EQUAL.
49
7. DRAW A FLOWCHART TO ADD TWO NUMBERS. PRINT "FAIL" IF
THE TOTAL LESS THAN 50, OTHERWISE PRINT "PASS". DO IT FOR N
DIFFERENT PAIRS OF NUMBERS.
50
8. WRITE AN ALGORITHM/PSEUDOCODE FOR A PROGRAM THAT
CALCULATES AND PRINTS THE VALUE OF X. WHERE X=
1/2+2/4+3/8…..+N/2N THE VALUE OF N IS ENTERED BY USER.
51
9. DRAW A FLOWCHART TO FIND THE SUM OF 100+110+120+… FOR
N TERMS.
52
10. DRAW A FLOWCHART TO FIND THE SUM
100+110+120+… FOR N TERMS.
OF
53
11. DRAW A FLOWCHART OF A PROGRAM THAT READS A SEQUENCE OF 20
INTEGER VALUES. THE PROGRAM WILL COUNT THE NUMBER OF POSITIVE
NUMBERS AND CALCULATES THEIR SUM. ALSO THE PROGRAM COUNTS THE
NUMBER OF NEGATIVE NUMBERS AND CALCULATES THEIR SUM. THEN IT
PRINTS THE RESULTS.
54
12. DRAW A FLOWCHART OF A PROGRAM THAT READS A SEQUENCE OF 20
INTEGER VALUES. THE PROGRAM WILL COUNT THE NUMBER OF POSITIVE
NUMBERS AND CALCULATES THEIR SUM. ALSO THE PROGRAM COUNTS THE
NUMBER OF NEGATIVE NUMBERS AND CALCULATES THEIR SUM. THEN IT
PRINTS THE RESULTS.
55
13.
WRITE AN ALGORITHM OF A PROGRAM THAT READS A SEQUENCE OF 20
INTEGER VALUES. THE PROGRAM WILL FIND AND PRINT THE MAXIMUM VALUE
56
14.
WRITE AN ALGORITHM/PSEUDOCODE FOR A PROGRAM THAT CALCULATES
AND PRINTS THE VALUE OF X. WHERE X= 1/2+2/4+3/8…+…. YOUR CALCULATIONS
STOPPED ONLY AND ONLY IF ONE OF TERMS BECOME LESS THAN 0.00001.
57
15. DRAW A FLOWCHART OF A PROGRAM THAT READS AN UNSPECIFIED NUMBER OF
INTEGERS (YOUR PROGRAM ENDS WITH NEGATIVE INPUT), DETERMINES AND PRINTS
HOW MANY ODD AND EVEN VALUES HAVE BEEN READ, AND COMPUTES AND PRINTS
THE TOTAL AND AVERAGE OF THE INPUT VALUES.
58
16. SUPPOSE A TEACHER HAS 10 SECTIONS AND EACH SECTION HAS
20 STUDENTS. THE TEACHER WANT TO CALCULATE AND PRINT THE
AVERAGE MARK FOR EACH SECTION AND THE AVERAGE FOR ALL
SECTIONS. YOU ARE TO WRITE AN ALGORITHM FOR A PROGRAM TO
SOLVE THIS PROBLEM.
59
17. DRAW A FLOWCHART
10TH
OF A PROGRAM THAT PRINTS OUT THE
MULTIPLICATION TABLE
[0X0,0X1,…10X10]
60