Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 13: 10/10/2002CS149D Fall 20021 CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.

Similar presentations


Presentation on theme: "Lecture 13: 10/10/2002CS149D Fall 20021 CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture."— Presentation transcript:

1 Lecture 13: 10/10/2002CS149D Fall 20021 CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 13: 10/10/2002

2 CS149D Fall 20022 Outline Problem Solving techniques An example to apply problem solving techniques First look at a C++ program

3 Lecture 13: 10/10/2002CS149D Fall 20023 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

4 Lecture 13: 10/10/2002CS149D Fall 20024 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?

5 Lecture 13: 10/10/2002CS149D Fall 20025 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

6 Lecture 13: 10/10/2002CS149D Fall 20026 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?

7 Lecture 13: 10/10/2002CS149D Fall 20027 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

8 Lecture 13: 10/10/2002CS149D Fall 20028 An Example 1/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* r 2 3.Print the value of circle area How do we represent more complex algorithms Pseudocode, flowcharts (will introduce flowcharts later)

9 Lecture 13: 10/10/2002CS149D Fall 20029 An Example 2/3 A divide and conquer block diagram of our problem Circle area Read radiusPrint 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 * radius 2 Write Circle area on the screen Stop

10 Lecture 13: 10/10/2002CS149D Fall 200210 An Example 3/3 Convert algorithm into a C++ program #include 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


Download ppt "Lecture 13: 10/10/2002CS149D Fall 20021 CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture."

Similar presentations


Ads by Google