Upload
tiffany-barrett
View
212
Download
0
Embed Size (px)
Citation preview
Lecture 13: 10/10/2002 CS149D Fall 2002 1
CS149D Elements of Computer Science
Ayman Abdel-Hamid
Department of Computer Science
Old Dominion University
Lecture 13: 10/10/2002
Lecture 13: 10/10/2002 CS149D Fall 2002 2
Outline•Problem Solving techniques
•An example to apply problem solving techniques
•First look at a C++ program
Lecture 13: 10/10/2002 CS149D Fall 2002 3
Problem 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
Lecture 13: 10/10/2002 CS149D Fall 2002 4
Strategies: Ask QuestionsWhen 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?
Lecture 13: 10/10/2002 CS149D Fall 2002 5
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
Lecture 13: 10/10/2002 CS149D Fall 2002 6
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?
Lecture 13: 10/10/2002 CS149D Fall 2002 7
Strategies: Divide and ConquerBreak 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
Lecture 13: 10/10/2002 CS149D Fall 2002 8
An 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)
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 (will introduce flowcharts later)
Lecture 13: 10/10/2002 CS149D Fall 2002 9
An Example2/3
A divide and conquer block diagram of our problem
Circle area
Read radius Print circle areaCompute 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
Lecture 13: 10/10/2002 CS149D Fall 2002 10
An Example3/3
Convert algorithm into a C++ program
#include <iostream.h>
void main ()
{
float pi = 3.14159f;
float radius, area;
cout << "Enter the radius of the circle: ";
cin >> radius;
area = pi* radius * radius;
cout << "The area of the circle is: " << area << endl;
}
Let’s look at that program in Microsoft Visual C++ environment