CSE115/ENGR160 Discrete Mathematics 02/10/11 Ming-Hsuan Yang UC Merced 1.
Cse115 lecture03problemsolving
-
Upload
md-ashikur-rahman -
Category
Engineering
-
view
38 -
download
0
Transcript of Cse115 lecture03problemsolving
![Page 1: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/1.jpg)
Lecture 03Problem Solving
CSE115: Computing Concepts
![Page 2: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/2.jpg)
Introduction to Problem Solving
• Problem solving is the process of transforming the description of a problem into a solution by using our knowledge of the problem domain and by relying on our ability to select and use appropriate problem-solving strategies, techniques and tools.
• Computers can be used to help us solving problems
![Page 3: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/3.jpg)
Software Development Method (SDM)
1. Specification of needs
2. Problem analysis
3. Design and algorithmic representation
4. Implementation
5. Testing and verification
6. Documentation
![Page 4: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/4.jpg)
Specification of Needs
• To understand exactly:• what the problem is
• what is needed to solve it
• what the solution should provide
• if there are constraints and special conditions.
I want a train…
![Page 5: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/5.jpg)
Problem Analysis
• In the analysis phase, we should identify the following:• Inputs to the problem, their form and the input media to be
used
• Outputs expected from the problem, their form and the output media to be used
• Special constraints or conditions (if any)
• Formulas or equations to be used
![Page 6: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/6.jpg)
Design and Algorithmic Representation
• An algorithm is a sequence of a finite number of steps arranged in a specific logical order which, when executed, produces the solution for a problem.
• An algorithm must satisfy these requirements:• It may have an input(s)• It must have an output• It should not be ambiguous (there should not be different
interpretations to it)• Every step in algorithm must be clear as what it is supposed to
do
![Page 7: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/7.jpg)
Design and Algorithmic Representation• It must be general (it can be used for different inputs)• It must be correct and it must solve the problem for which it is
designed• It must execute and terminate in a finite amount of time• It must be efficient enough so that it can solve the intended
problem using the resource currently available on the computer
• An algorithm can be represented using pseudocodes or flowcharts.
![Page 8: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/8.jpg)
![Page 9: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/9.jpg)
Pseudocodes• A pseudocode is a semiformal, English-like language with limited
vocabulary that can be used to design and describe algorithms.
• Criteria of a good pseudocode:• Easy to understand, precise and clear
• Gives the correct solution in all cases
• Eventually ends
![Page 10: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/10.jpg)
Pseudocodes: The Sequence control structure
• A series of steps or statements that are executed in the order they are written in an algorithm.
• The beginning and end of a block of statements can be optionally marked with the keywords begin and end.
• Example 1:
Begin
Read the birth date from the user.
Calculate the difference between the
birth date and today’s date.
Print the user age.
End
![Page 11: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/11.jpg)
Pseudocodes: The Selection control structure
• Defines two courses of action depending on the outcome of a condition. A condition is an expression that is, when computed, evaluated to either true or false.
• The keyword used are if and else.
• Format:
if condition
then-part
else
else-part
end_if
Example:
if age is greater than 55
print “Retire”
else
print “Work Work Work”
end_if
![Page 12: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/12.jpg)
Pseudocodes: The Selection control structure
•Sometimes in certain situation, we may omit the else-part.
if number is odd number
print “This is an odd number”
end_if
![Page 13: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/13.jpg)
Pseudocodes: The Selection control structure
• Nested selection structure: basic selection structure that contains other if/else structure in its then-part or else-part.
if number is equal to 1
print “One”
else if number is equal to 2
print “Two”
else if number is equal to 3
print “Three”
else
print “Other”
end_if
![Page 14: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/14.jpg)
Pseudocodes: The Repetition control structure
• Specifies a block of one or more statements that are repeatedly executed until a condition is satisfied.
• The keyword used is while.
• Format:
while condition
loop-body
end_while
![Page 15: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/15.jpg)
Pseudocodes: The Repetition control structure
• Example: Summing up 1 to 10
set cumulative sum to 0
set current number to 1
while current number is less or equal to 10
add the cumulative sum to current number
add 1 to current number
end_while
print the value of cumulative sum
![Page 16: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/16.jpg)
Pseudocodes: The Repetition control structure
• Subsequently, we can write the previous pseudocode with something like this.
• Example: Summing up 10 numbers
cumulative sum = 0
current number = 1
while current number is less or equal to 10
cumulative sum = cumulative sum + current number
current number = current number + 1
end_while
print the value of cumulative sum
• Note that in this algorithm, we are using both the sequence and repetition control structure
![Page 17: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/17.jpg)
Pseudocodes: The Repetition control structure
• Example: Begin
number of users giving his birth date = 0
while number of users giving his birth date < 10
Read the birth date from the user.
Calculate the difference between the birth date and today’s date.
Print the user age.
if the age is greater than 55
print “retire”
else
print “keep working”
end_if
number of user giving his birth date + 1
end_while
End
![Page 18: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/18.jpg)
Flowcharts
• Flowcharts is a graph used to depict or show a step by step solution using symbols which represent a task.
• The symbols used consist of geometrical shapes that are connected by flow lines.
• It is an alternative to pseudocoding; whereas a pseudocode description is verbal, a flowchart is graphical in nature.
![Page 19: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/19.jpg)
Flowchart Symbols
Terminal symbol - indicates the beginning and
end points of an algorithm.
Process symbol - shows an instruction other than
input, output or selection.
Input-output symbol - shows an input or an output
operation.
Disk storage I/O symbol - indicates input from
or output to disk storage.
Printer output symbol - shows hardcopy printer
output.
![Page 20: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/20.jpg)
Flowchart Symbols cont…
Selection symbol - shows a selection process
for two-way selection.
Off-page connector - provides continuation
of a logical path on another page.
On-page connector - provides continuation
of logical path at another point in the same
page.
Flow lines - indicate the logical sequence of
execution steps in the algorithm.
![Page 21: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/21.jpg)
Flowchart – sequence control structure
Statement 2
Statement 1
Statement 3
:
![Page 22: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/22.jpg)
Flowchart – selection control structure
Condition
else-
statement(s)
then-
statement(s)
YesNo
![Page 23: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/23.jpg)
Flowchart – repetition control structure
ConditionLoop
Statement(s)
yes
no
![Page 24: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/24.jpg)
Flowchart – example 1
Begin
Read birth date
Calculate
Age = current year – birth date
Display
age
End
![Page 25: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/25.jpg)
Flowchart – example 2
Begin
Read age
End
Age > 55? NOYES
print “retired” print “keep working”
![Page 26: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/26.jpg)
Flowchart – example 3
Begin
End
current_number <= 10?
NO
YES
sum = 0
current_number = 1
sum = sum + current_number
current_number = current_number + 1
print sum
![Page 27: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/27.jpg)
Implementation• The process of implementing an algorithm by writing a
computer program using a programming language (for example, using C language)
• The output of the program must be the solution of the intended problem
• The program must not do anything that it is not supposed to do
• (Think of those many viruses, buffer overflows, trojan horses, etc. that we experience almost daily. All these result from programs doing more than they were intended to do)
![Page 28: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/28.jpg)
Testing and Verification• Program testing is the process of executing a program to
demonstrate its correctness
• Program verification is the process of ensuring that a program meets user-requirement
• After the program is compiled, we must run the program and test/verify it with different inputs before the program can be released to the public or other users (or to the instructor of this class)
Awesome!!!
![Page 29: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/29.jpg)
Documentation• Contains details produced at all stages of the program
development cycle.
• Can be done in 2 ways:• Writing comments between your line of codes• Creating a separate text file to explain the program
• Important not only for other people to use or modify your program, but also for you to understand your own program after a long time (believe me, you will forget the details of your own program after some time ...)
![Page 30: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/30.jpg)
Documentation• Documentation is so important because:
• You may return to this program in future to use the whole of or a part of it again
• Other programmer or end user will need some information about your program for reference or maintenance
• You may someday have to modify the program, or may discover some errors or weaknesses in your program
• Although documentation is listed as the last stage of software development method, it is actually an ongoing process which should be done from the very beginning of the software development process.
![Page 31: Cse115 lecture03problemsolving](https://reader031.fdocuments.us/reader031/viewer/2022030304/587a803c1a28abf0468b731d/html5/thumbnails/31.jpg)
Homework• Read the value of the height, width and length of a box from
the user and print its volume.
• Convert temperature (provided as input) from degree Celsius to degree Fahrenheit.
• Find the minimum of three given numbers.
• Find the maximum of three given numbers.
• Take a, b and c as input and determine the roots for the equation ax2+bx+c.
• Read a positive integer n from the user and then compute and print the sum of all integers between 1 and n.
• Read 100 numbers from the user and print their sum.
• Determine whether a given integer is a prime number or not.
• Find the smallest integer n such that, 1+2+3+...+n is equal to or greater than 1000.