100 likes | 208 Views
CS149D Elements of Computer Science. Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 13: 10/10/2002. Outline. Problem Solving techniques An example to apply problem solving techniques First look at a C++ program. Problem Solving Techniques.
E N D
CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 13: 10/10/2002 CS149D Fall 2002
Outline • Problem Solving techniques • An example to apply problem solving techniques • First look at a C++ program CS149D Fall 2002
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 CS149D Fall 2002
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? CS149D Fall 2002
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 CS149D Fall 2002
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? CS149D Fall 2002
Strategies: Divide and Conquer Break up large problems into smaller problems that are easier to handle (Top-Down approach) Hard problem Easy subproblem Hard subproblem Easy subproblem Easy subproblem Easy subproblem CS149D Fall 2002
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) • 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) CS149D Fall 2002
Circle area Read radius Compute area Print circle area An 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 CS149D Fall 2002
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 CS149D Fall 2002