ICS102: Introduction To Computing King Fahd University of

King Fahd University of Petroleum & Minerals
College of Computer Science & Engineering
Information & Computer Science Department
ICS102: Introduction To Computing
Problem Solving 1
1
Today’s Class
• What’s Computer Science?
• What’s an Algorithm?
• Problem solving techniques
• Overview of computer programming process
• Problem solving techniques
2
What is Computer Science?
•
It is the discipline that seeks to build a scientific foundation for such
topics as computer design, computer programming, information
processing, and algorithmic solutions of problems.
•
A computer is a machine that stores data, interact with devices,
execute programs, and provides computing capabilities to its users).
•
Computing is the execution of an “algorithm”.
What is an Algorithm?
A set of steps that define how a task is performed. In other
words, it is an ordered set of unambiguous, executable steps
that define a terminating activity.
Examples:
•
Make a list of all positive integers
•
Extract the first integer from the list
3
Algorithm
•
An algorithm can be represented using some sort
of language, such as the flowcharts pseudocode
(precisely defined textual structures)
•
An algorithm is abstract and it can be represented
in many ways.
EXAMPLE:
Algorithm for converting from Celsius to Fahrenheit can be represented as
1. F = (9/5) C + 32 (algebraic equation)
2. “ Multiply the temperature reading in Celsius by 9/5 and then add 32
to the product”
4
Example: An algorithm for starting the car
To run the car, we follow the following steps:
1. Insert the key in ignition
2. Make sure transmission is in Park (or Neutral)
3. Depress the gas pedal
4. Turn key to start position
5. If engine starts within six seconds, release key to
ignition position
6. If engine does not start in six seconds, release key and
gas pedal, wait ten seconds, and repeat steps 3 through
6, but not more than five times
7. If the car does not start, call the garage
5
Programming Process
Computer program is a sequence of instructions to be
performed by a computer.
Computer programming is the process of planning a sequence
of steps for a computer to follow
Programming Process has the following three phases:
• Problem-solving phase
• Implementation phase
• Maintenance phase
6
Programming Process
Problem-solving phase
•Analysis and specification ( understand and define problem, and
what is expected of solution)
•General solution (algorithm: a logical sequence of steps that
solves the problem)
•Verification (Follow steps to make sure solution solves the
problem)
Implementation phase
•Concrete solution (Program in a Programming language)
•Testing (make sure the program produces the desired results)
Maintenance phase
•Use Program
•Maintain Program (meet changing requirements)
7
Programming Process
Analysis and
Specification
Concrete solution
(Program)
General solution
(algorithm)
Testing
Verification
Maintenance Phase
Documentation: writing program documentation, and user manuals
8
Problem Solving
• The purpose of writing a program is to solve a problem
• The general steps in problem solving are:
–
–
–
–
–
–
–
Understand the problem
Dissect the problem into manageable pieces
Design a solution
Analyze the complexity of the algorithm
Consider alternatives to the solution and refine it
Implement the solution
Test the solution and fix any problems that exist
9
Problem Solving
• Many software projects fail because the developer didn't
really understand the problem to be solved
• We must avoid assumptions and clarify ambiguities
• As problems and their solutions become larger, we must
organize our development into manageable pieces
• This technique is fundamental to software development
• In java, we will dissect our solutions into pieces called
classes and objects, taking an object-oriented approach
10
Problem Solving Techniques
•
You follow algorithms every day in your life. So, we need to learn
how to design algorithms not simply follow them.
•
So, what is Problem Solving Process?
1. Analysis
2. Design
3. Implementation
4. Testing
Some Strategies to solve
problems
• Ask questions
•
•
•
Look for things that
are familiar
Determine
problem features
Describe objects
and methods
Produce the
classes and code
Analysis
Rethink as
appropriate
Design
Implementation
Means-Ends Analysis
Divide and Conquer
Examine for
correctness
Testing
11
Strategies: Ask Questions
When you are given a problem, you ask questions (What, Why,
When, and Where?)
In the context of programming
•What do I have to work with (What is my data)?
•What do the data items look like?
•How much data is there?
•How will I know when I have processed all the data?
•What should my output look like?
•How many times is the process going to be repeated?
•What special error conditions might come up?
12
Strategies: Look for Familiar Things
• Never reinvent the wheel
If a solution exists  USE IT
Finding the daily high and low temperatures
is really the same problem as
Finding the highest and lowest grades on a test
Both problems can be abstracted as being
Find largest and smallest values in a set of numbers
13
Strategies: Means-Ends Analysis
• Beginning state and End state are often given
• You need to define a set of actions that can be used to
get from one to the other
• Once you have a set of actions, you need to work out the
details
Translated to computer programming
 Begin by writing down what the input is? (Beginning
state)
 What the output should be? (End state)
 What actions can be performed to obtain results from
input data?
14
Strategies: Divide and Conquer
Break up large problems into smaller problems that are easier to handle
(Top-Down approach)
Hard problem
Easy
subproblem
Easy
subproblem
Hard
subproblem
Easy
subproblem
Easy
subproblem
15
An Example
Compute the area of a circle
Problem statement
We need an interactive program (user will input data) that computes the area of a
circle. Given the circle radius, the circle area should be displayed on the screen
Input/Output description
Input
 Circle radius
Output  Circle area
Algorithm development (set of steps, decomposition outline)
1.
Read value of circle radius (r)
2.
Compute circle area as pi* r2
3.
Print the value of circle area
How do we represent more complex algorithms
Pseudocode, flowcharts
16
An Example (continued)
A divide and conquer block diagram of our problem
Circle area
Read radius
Compute area
Print circle area
Pseudocode
Prompt the user for the circle radius (put a message on the screen)
Read radius
Assign Circle area the value pi * radius2
Write Circle area on the screen
Stop
17