Title: CS149D Elements of Computer Science
1CS149D Elements of Computer Science
Ayman Abdel-Hamid Department of Computer
Science Old Dominion University Lecture 13
10/10/2002
2Outline
- Problem Solving techniques
- An example to apply problem solving techniques
- First look at a C program
3Problem Solving Techniques
- You follow algorithms every day in your life
- We need to learn how to design algorithms not
simply follow them - Some Strategies to solve problems
- Ask questions
- Look for things that are familiar
- Means-Ends Analysis
- Divide and Conquer
4Strategies 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?
5Strategies 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
6Strategies 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?
7Strategies 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
8An Example1/3
- 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) - Read value of circle radius (r)
- Compute circle area as pi r2
- Print the value of circle area
- How do we represent more complex algorithms
- Pseudocode, flowcharts (will introduce flowcharts
later)
9An Example2/3
A divide and conquer block diagram of our problem
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
10An Example3/3
Convert algorithm into a C program include
ltiostream.hgt void main () float pi
3.14159f float radius, area cout ltlt "Enter
the radius of the circle " cin gtgt
radius area pi radius radius cout ltlt
"The area of the circle is " ltlt area ltlt endl
Lets look at that program in Microsoft Visual
C environment