Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.

Slides:



Advertisements
Similar presentations
Problem Solving for Programming Session 1 Programming and Problem Solving.
Advertisements

1 CS101 Introduction to Computing Lecture 17 Algorithms II.
A Third Look At Prolog Chapter Twenty-TwoModern Programming Languages, 2nd ed.1.
Programming Types of Testing.
Chapter 1 - An Introduction to Computers and Problem Solving
Chapter 2 - Problem Solving
Chapter 2 - Problem Solving
Introduction Creating equations from context is important since most real-world scenarios do not involve the equations being given. An equation is a mathematical.
Chapter 10 Algorithmic Thinking. Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Learning Objectives List the five essential.
X Australian Curriculum Year 5 Solve problems involving multiplication of large numbers by one or two digit numbers using efficient mental, written strategies.
Polya’s Four Step Problem Solving Process
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
Pseudocode and Algorithms
Modules, Hierarchy Charts, and Documentation
Problem Solving The process of applying previously acquired knowledge to new and unfamiliar situations.
Chapter 2: Algorithm Discovery and Design
Fundamentals of Python: From First Programs Through Data Structures
1 I.Introduction to Algorithm and Programming Algoritma dan Pemrograman – Teknik Informatika UK Petra 2009.
Thanks to: Dr. John S. Mallozzi Department of Computer Science 1. Introduction 2. Overview of programming in Python.
IS5000xx Foundations of Problem Solving Dr Lahen Ouarbya.
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
Dr Scott Turner. Why do you needed to develop problem solving skills?  One definition of programming is it is applied problem solving  You have a problem.
Chapter An Introduction to Problem Solving 1 1 Copyright © 2013, 2010, and 2007, Pearson Education, Inc.
Invitation to Computer Science, Java Version, Second Edition.
Moving Around in Scratch The Basics… -You do want to have Scratch open as you will be creating a program. -Follow the instructions and if you have questions.
Algorithms CS139 – Aug 30, Problem Solving Your roommate, who is taking CS139, is in a panic. He is worried that he might lose his financial aid.
INTRODUCTION TO COMPUTING CHAPTER NO. 04. Programming Languages Program Algorithms and Pseudo Code Properties and Advantages of Algorithms Flowchart (Symbols.
Problem Solving. What is Problem Solving???? Well you could find several definitions of problem solving, but we only have to concentrate in the fact that.
Section 1.3 Problem Solving Math in Our World. Learning Objectives  State the four steps in the basic problem solving procedure.  Solve problems by.
1 The Practice of Computing Using PYTHON William PunchRichard Enbody Chapter 3 Algorithms & Program Development Copyright © 2011 Pearson Education, Inc.
Good Teaching Through Mathematical Problem Solving.
PROGRAM DEVELOPMENT CYCLE. Problem Statement: Problem Statement help diagnose the situation so that your focus is on the problem, helpful tools at this.
Grade 3 Instructional Focus Four critical areas: Developing understanding of: multiplication & division and strategies of multiplication & division within.
 In this packet we will look at:  The meaning of acceleration  How acceleration is related to velocity and time  2 distinct types acceleration  A.
Chapter 10 Algorithmic Thinking. Learning Objectives Explain similarities and differences among algorithms, programs, and heuristic solutions List the.
Data Structures & Algorithms CHAPTER 1 Introduction Ms. Manal Al-Asmari.
Chapter 2 - VB 2005 by Schneider- modified by S. Jane '081 Chapter 2 - Problem Solving 2.1 Program Development Cycle 2.2 Programming Tools.
Programming Introduction. What is a program? Computers cannot think for themselves, they can only follow instructions. A program is a set of instructions.
LESSON 1 Introduction to Programming Language. Computer  Comprised of various devices that are referred to as HARDWARE.  The computer programs that.
The single most important skill for a computer programmer is problem solving Problem solving means the ability to formulate problems, think creatively.
WHAT IS THIS? Clue…it’s a drink SIMPLE SEQUENCE CONTROL STRUCTURE Introduction A computer is an extremely powerful, fast machine. In less than a second,
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
Lecture #1: Introduction to Algorithms and Problem Solving Dr. Hmood Al-Dossari King Saud University Department of Computer Science 6 February 2012.
It is not enough to know the skills. It is important to know how to use these skills to solve real-world problems. Problem solving touches every aspect.
How Computers Solve Problems Computers also use Algorithms to solve problems, and change data into information Computers can only perform one simple step.
Thinking Mathematically Problem Solving. Polya’s Four Steps Understand the problem Devise a plan Carry out the plan and solve the problem Look back and.
Victoria Ibarra Mat:  Generally, Computer hardware is divided into four main functional areas. These are:  Input devices Input devices  Output.
 Problem Analysis  Coding  Debugging  Testing.
Algorithms and Flowcharts
Week 4 Lecture 1 Models as aids to problem solving.
Algorithms and Problem Solving
ALGORITHMS AND FLOWCHARTS
IOT POLY ENGINEERING I1-20 September 30, 2009
Lecture 2 Introduction to Programming
Introduction to Computer Programming
ALGORITHMS AND FLOWCHARTS
KS4 Mathematics A6 Quadratic equations.
ALGORITHMS AND FLOWCHARTS
Fundamentals of Data Representation
Algorithm Discovery and Design
4. Computational Problem Solving
Understanding Problems and how to Solve them by using Computers
Algorithms and Problem Solving
Tonga Institute of Higher Education IT 141: Information Systems
Tonga Institute of Higher Education IT 141: Information Systems
Basic Concepts of Algorithm
Created for CVCA Physics by Dick Heckathorn 24 March 2K + 5
I believe the reason students have difficulty learning algebra and that most people have trouble doing math word problems is that although children are.
Presentation transcript:

Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies

How do we solve problems? There are many different kinds of problems we need to solve as programmers. Some are complex and very difficult to solve. In order to solve complex and difficult problems, we need: – an appropriate way of describing the problem (a language of description) –an appropriate way of solving the problem (a problem solving strategy).

Languages of description – natural language The most accessible tool at our disposal as problem solvers is natural language. Natural language allows us to describe problems in a way that can easily be understood by everybody. However, natural language is often imprecise and ambiguous (e.g. He eats shoots and leaves as opposed to He eats, shoots and leaves), which is of little use when we are writing computer programs, which need to be clear, precise and unambiguous. Because it can be imprecise and ambiguous, natural language should be only one tool amongst many that we use when problem solving. Natural language needs to be augmented with more precise ways of representing problems.

Languages of description – visual representation An alternative to natural language, and an important tool in a problem solver’s armoury, is visual or graphical representation. Visual representation enables us to make complex problems easier to understand and solve. Take the following example:

Languages of description – visual representation One morning at eight o’clock you set off in your car to visit a friend who lives some distance away. You encounter the odd traffic delay and your driving speed varies as the speed limits on the different stretches of road change. It is a long drive, so you stop twice to refresh yourself and to eat and drink something. You arrive at 6.00 p.m. and decide to stay overnight. The next morning you set off again at 8.00 a.m. You take the same route back, but encounter heavy traffic in different places. Because one traffic jam was so large you only stop once for refreshments. Still, you arrive back home at six o’clock. Question: Is there a point on the road that you pass at exactly the same time of day on the outward and return journeys? Trying to make sense of this problem on the basis a natural language description like this is difficult. There is a lot of detail to store in your head.

Languages of description – visual representation A visual representation of the problem, on the other hand, allows us to see the problem and the solution more clearly. (Assuming the same average speed),we can see that the point is reached at 2pm. Six hours into the journey.

Languages of description – visual representation The same problem can often be represented in many different ways. For example, you could represent the journey problem numerically like this. The best representation is the way that suits you (And, of course, the way that produces a correct solution!).

Languages of description – visual representation Problems involving two or three dimensional space are very difficult to solve without some form of visual representation. Take the following: There is a large square room whose walls are twenty-four feet long. The ceiling is eight feet high. On the floor in a corner is a bowl of sugar. In the opposite corner by the ceiling is an ant. Question: What is the shortest path the ant can take to get to the sugar? The problem appears to be maths/geometry based, but even a somebody with a good head for maths might struggle with this one without some form of picture to assist him/her.

Languages of description – visual representation The quickest route would appear to be straight down the wall and diagonally across the room to the sugar (A). However, we need to consider other possibilities. What about (B)? Is this shorter? How can we test if it is or not?

Languages of description – taking a different view The first one (A) is easy to test using Pythagoras. Now, what about the second one (B)? It appears we do not have sufficient information at our disposal to calculate the distance of the route. But what if we take a different view of the problem? What if we represent the problem two-dimensionally instead of three-dimensionally?

Languages of description – taking a different view Three dimensional Two dimensional Can we solve the problem now? How?

Languages of description – taking a different view It is now possible to draw a straight line from ant to sugar to create a triangle whose width and length are known, meaning, with a little help from Pythagoras, we can easily work out the distance we need. As a result, we can see that B is, in fact, the shortest path. This solution was only possible, because we took an alternative view of the problem.

Other problem solving strategies – solving a simpler analogous problem Another approach to solving complex problems may be to solve a simpler analogous problem first. Recall the problem about meeting on the road. In this problem, one person makes two journeys on two different days. What about if two people make the same journey (in reverse) on the same day. Of course they will meet, and of course they will meet at the same time. By rephrasing a problem in simpler terms, we can often see the solution more clearly.

Other problem solving strategies – finding a pattern Consider the following problem: How many angles are formed by ten distinct rays with a common end point?

Other problem solving strategies – finding a pattern Let’s start simply (again solving a simpler analogous problem). How many angles are formed by one ray? Two rays? Three? The answer is clear here because there is little detail to consider (0, 1, 3).

Other problem solving strategies – finding a pattern If we represent this data in a table, we get the following. Can you see a pattern emerging? What about four or five rays? Can you see a pattern emerging now? No. rays123 No. angles013 No. rays12345 No. angles0136?

Other problem solving strategies – working backwards Consider the following problem: Helen has an 11-litre can and a 5-litre can. How can she measure out exactly seven litres of water?

Other problem solving strategies – working backwards We could try to measure out the 7-litres using a combination of cans through trial and error. However, if we consider where we are at the end of the problem, we can see that our can has 7-litres of water and 4-empty litres. Can we measure off four litres then? The answer is, yes. If we empty the 11-litre can into the 5-litre can twice, we know we have 1-litre left. If we empty this into a 5-litre can, we know the empty part is four litres. Now if we refill the 11-litre can, and pour off 4-litres into the 5-litre can, we are left with 7-litres in the 11-litre can.

Other problem solving strategies – intelligent guessing Guessing solutions is not usually a good strategy, but intelligent guessing can be Consider the following problem: The one mile relay team has four runners: Bob, Ian, Len and Sid. They run in alphabetical order. Each runner runs his quarter-mile 2 sec faster than the next runner. They finish in exactly 3mins 40 sec. Question: How fast did each runner run his part?

Other problem solving strategies – intelligent guessing It will help if we start by converting 3 min 40 sec to seconds (e.g. 220 sec). As a starting point for our guessing, we could imagine the runners are evenly matched, and run at the same speed. They would then run their respective parts of the race at 220/4 = 55 sec. We can use this for our initial guess for our slowest runner, Sid. But this is obviously wrong as 208<220. We know Sid has to run slower than 55 sec. So we need to increase his time. Runner:BobIanLenSidTotal Time:

Other problem solving strategies – intelligent guessing If we up our guess by one, we are still wrong. However, with a couple more (intelligent) guesses we eventually arrive at the correct solution. BobIanLenSidTotal

Other problem solving strategies – logical reasoning Consider the following problem: Place the numbers 1 – 9 in the following grid so that the sum of all the rows, columns and diagonals is the same.

Other problem solving strategies – logical reasoning Start by placing all the numbers in the grid sequentially 1 – 9. The sum of all the cells is 45. If each row is to have the same sum, then it must be 15 (45/3). The next step is to determine which number should be entered into the centre cell. Start by trying extreme cases. If we entered 9, then 9 would appear in a row, column or diagonal with 8, 7 and 6, giving a total > 15. If we entered 1, then 1 would appear in a row, column or diagonal with 2, 3 and 4, giving a total < 15. Having accounted for all the possibilities, we can see now that only 5 can occupy the centre cell.

Other problem solving strategies – logical reasoning Now we can use intelligent guessing. We could try  The problem here, however, is that with nine in one column, the other two numbers in that column must total 6 (e.g. 2 & 4). But if 2 and 4 are in the column, then the row with 1 in it can never total 15. If we move our 1, 5 and 9 to the central column, however, we do not have the same problem. The rest is easy

Mathematical reasoning Mathematical reasoning underlies several of the problem solving strategies we have already looked at. For example: You do not need to be brilliant at maths to be a competent problem solver. But being good at maths will certainly help you to solve programming problems faster and more effectively.

Algorithms After we have solved a problem, we can start to develop algorithms to express our solutions in general terms. If you recall, in session 1 we developed a solution for the following problem: In a room with ten people, everyone shakes hands with everyone else exactly once. In total, how many handshakes are there? The solution to the problem was as follows: grid size = 10x10 = 100 handshakes = (100-10)/2 = 45 The solution was correct, but it would be of little use as a computer program, as it is specific to a particular number of participants (10). We saw that it would be much more useful to develop a general formula that is applicable to all instances of the problem: for example h = ((p 2 ) – p)/2

Algorithms Algorithms are like this formula. They are generalisable to other instances of the same problem (e.g. 20, or 100, 10,000, n participants).

Algorithms Imagine we have a Smartphone and we want to develop an app for it that will solve the handshakes problem we encountered in session 2. Would it be enough to tell the device: h = (p 2 – p)/2 Almost certainly not. There is simply not enough information available for a computer to know what to do. For example, we have not said what p is, or for that matter h, or where these values come from. Also, what does p 2 mean to a computer? We need to provide more detail and to express the algorithm in a way a computer can understand in order for our program to work effectively. We need to provide a detailed, step-by-step set of instructions: in other words a concise and complete algorithm.

Algorithms and pseudo code Algorithm development is the intermediate stage between problem solving and coding. It is where we specify the solution to a problem in a generalisable and structured format somewhere between natural language and programming code. The standard way that programmers have of creating algorithms is to use pseudo code (sometimes called structured English). Pseudo code is a high-level description of a computer program that uses the structural conventions of a programming language, but is intended for human reading rather than machine reading. Importantly, it is programming language independent, which means you do not have to know a particular programming language to understand how it works.

Algorithms and pseudo code 1. Request input ; 2. Check input valid ; 3. IF (input valid) 3.1 persons  input ; ENDIF 4. handshakes = ((persons * persons)- persons)/2 ; 5. Display handshakes ; Task number Line terminator Tasks or steps in the algorithm

Sequence As we have seen, an algorithm is a basic representation of the steps involved in a problem. When we are developing algorithms, it is important that the sequence of the steps is represented correctly. Consider the following example of an ATM: 1. Get password ; 2. Get amountNeeded ; 3. Dispense amountNeeded ; 4. IF (enteredPassword is NOT equal to storedPassword) 4.1 Do not dispense amountNeeded ; END IF What is wrong?

Summary We need to represent our problems in the most appropriate language of description. We need to select the best problem-solving strategy. The difficulty, however, is learning which language of description and problem solving strategy to chose for a particular problem scenario. This will come with experience and practice.

Session reading Vickers –Chapter 3