Uploaded by sittisarimah01

week1&2 Introduction & problem solving

advertisement
Never tire of learning new things. Stretch your
mind. Broaden your experience.
Introduction
A computer is an electronic device capable of
performing commands given by human.
Program – Are sequences of instructions and decisions
that the computer carries out to achieve a task
Programmer – person responsible in writing a program
Problem solving
The process of working through
details of a problem to reach a
solution.
Steps in Problem solving
1.Problem identification
2.Problem Analysis
3.Designing solution to the problem- algorithm
4.Coding (e.g. Java, Python, C++)- implementation
5.Testing and debugging
6.Documentation
1. Problem
• Write a program to obtain average of 3
numbers given by a user. Display the 3
numbers and the average of the numbers.
Review the problem carefully and understand what you are asked to do.
2.Problem Analysis
• Identify problems:
– Input
– Output
– Process
INPUT
PROCESS
OUTPUT
Problem Analysis
Determine what information
should be given(input) and
what result must be
produced(output).
Assign names to each input
and output items.
Determine the manner of processing
must be done on the input data to come
up with the desired output (i.e.,
determine what formulas are needed to
manipulate the given data).
Problem Analysis
• INPUT
3 numbers
• OUTPUT
Print 3 numbers & average
• PROCESS
Calculate average
Add 3 numbers
Divide sum of the numbers by 3
3.Algorithm
– Any computing problem can be done by executing
a series of actions in a specific order.
– A procedure for solving a problem in terms of the
actions to be executed and the order in which
these actions are to be executed is called an
algorithm.
– Is a logical solution that is inline with our daily
language or mother tongue language.
– Can be done using pseudo code or flow chart
Pseudo code
•
•
•
•
Is an artificial and informal language
Usually it is use English language
Quite similar to programming language
The purpose of pseudo code is to make
humans who do not understand computer
programming can easily understand the
flow of the problem solving.
Example of Algorithm
Start
1. Read 3 numbers: nom1, nom2, nom3
2. Add 3 numbers and assign the result to sum
3. Calculate the average,
average = sum/3
4. Print 3 numbers(nom1, nom2, nom3) and
the average
End
Example of Pseudo code
START
INPUT nom1, nom2, nom3
sum = nom1 + nom2 + nom3
average = sum / 3
PRINT nom1, nom2, nom3, average
END
Flow Chart
• It is represented by using geometry shapes
with connected line
• Use a standard symbol
Flow Chart
TERMINAL
Indicates the beginning or end of an algorithm
PROCESS
Indicates an input computational or data
manipulation.
INPUT / OUTPUT
Indicates an input or output operation
Flow Chart
DECISION
Indicates a decision point in the algorithm
CONNECTOR
Indicates an entry to or exit from another
part of the flowchart
FLOW LINES
Used to connect the flowchart symbols and
indicate the logic flow
Example of a flow chart
START
Input nom1,nom2, nom3
sum = nom1 + nom2 + nom3
average = sum / 3
print nom1,nom2, nom3
print average
END
EXERCISE
• Write an algorithm and a flow chart to
calculate and display a volume of a sphere.
Volume = 4/3 x pi x radius x radius x radius , where
pi = 3.14
Solution
• Analyze the problem
– Input :
Radius
– Process :
Calculate volume of a sphere
Volume = 4/3 x pi x radius x radius x radius
– Output :
Print volume of a sphere
Solution (algorithm)
Start
1. Set pi = 3.14
2. Input radius
3. Calculate volume of a sphere
Volume = 4/3 x pi x radius x radius x radius
4. print volume of a sphere
End
Solution (Pseudo code)
START
SET pi = 3.14
INPUT radius
Volume = 4/3 x pi x radius x radius x radius
PRINT Volume
END
Solution (Flow Chart)
START
pi = 3.14
Input radius
Volume = 4/3 x pi x radius x radius x radius
Print Volume
END
EXERCISE
1.
2.
3.
Write a pseudocode to calculate and display an
average of four numbers.
Write a pseudocode to calculate and display
a multiplication of three numbers.
Write a pseudocode to calculate and display
an area of a rectangle.
1.Problem Analysis
• INPUT
4 numbers
• OUTPUT
average of 4 numbers
• PROCESS
calculate average of 4 numbers
sum of 4 numbers
average = sum/4
1. Example of Pseudo code
START
INPUT num1, num2, num3, num4
average = (num1 + num2 + num3 + num4)/4
PRINT average
END
2.Problem Analysis
• INPUT
3 numbers
• OUTPUT
Multiplication of 3 numbers
• PROCESS
calculate multiplication of 3 numbers
2.Example of Pseudo code
START
INPUT n1,n2,n3
multiplication = n1 x n2 x n3
PRINT multiplication
END
3.Problem Analysis
• INPUT
width, length
• OUTPUT
area
• PROCESS
calculate area
area = width x length
3.Example of Pseudo code
START
INPUT width, length
area = width x length
PRINT area
END
Download