Topic 1: Problem Solving

Slides:



Advertisements
Similar presentations
Dry Runs and Trace Tables
Advertisements

How SAS implements structured programming constructs
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Loop Exercise 1 Suppose that you want to take out a loan for $10,000, with 18% APR, and you're willing to make payments of $1,200/month. How long will.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
General Condition Loop A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
Scratch Programming Cards
Lesson #5 Repetition and Loops.
Learning outcomes 5 Developing Code – Using Flowcharts
Repetitive Structures
Topic 6 Recursion.
Repetition Structures
Loops BIS1523 – Lecture 10.
Repetition (While-Loop) version]
Topics Introduction to Repetition Structures
7 - Programming 7P, Q, R - Testing.
Lesson #5 Repetition and Loops.
CS1371 Introduction to Computing for Engineers
Teaching design techniques to design efficient solutions to problems
Introduction to Programmng in Python
Topics Introduction to Repetition Structures
Using a Stack Chapter 6 introduces the stack data type.
Problem Solving (design of programs) CS140: Introduction to Computing 1 8/26/13.
Algorithm design and Analysis
Conditions and Ifs BIS1523 – Lecture 8.
Lesson #5 Repetition and Loops.
Using a Stack Chapter 6 introduces the stack data type.
Fundamentals of Programming
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
<INSERT_WITTY_QUOTE_HERE>
This Lecture Substitution model
Topic 1: Problem Solving
Fundamentals of Data Structures
We’re moving on to more recap from other programming languages
Michael Ernst UW CSE 140 Winter 2013
Coding Concepts (Basics)
The while Looping Structure
Java Programming Control Structures Part 1
More Looping Structures
Lec 4: while loop and do-while loop
Chapter 6: Repetition Statements
Algorithm and Ambiguity
Problem Solving Designing Algorithms.
3.1 Iteration Loops For … To … Next 18/01/2019.
Python programming exercise
Using a Stack Chapter 6 introduces the stack data type.
Using a Stack Chapter 6 introduces the stack data type.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Flowcharts and Pseudo Code
Python Basics with Jupyter Notebook
Topics Introduction to Repetition Structures
Lesson #5 Repetition and Loops.
CPSC 121: Models of Computation
Loops.
Basic Concepts of Algorithm
This Lecture Substitution model
CPSC 121: Models of Computation
More Looping Structures
The while Looping Structure
Learning Intention I will learn about the standard algorithm for input validation.
Incremental Programming
Michael Ernst UW CSE 190p Summer 2012
While Loops in Python.
The while Looping Structure
Software Development Techniques
Starter Look at the hand-out.
Presentation transcript:

Topic 1: Problem Solving Correcting Algorithms

Understanding Algorithms There may come a point where we don’t create the algorithm ourselves We may be given an algorithm to look at We’ll need to learn how to understand what this algorithm does As well as Work out what the algorithm outputs Test to see if the algorithm is correct Problem Solving: Correcting Algorithms

Understanding Algorithms When it comes to understanding algorithms, knowing the constructs of programming always helps Understanding these constructs, and how they work, are how we can work out What an algorithm is generally doing What is happening to the data in the algorithm The constructs are Sequencing Branching Iteration Problem Solving: Correcting Algorithms

Understanding Algorithms Sequencing The key here is that instructions happen from top to bottom If a variable has a different value placed in it in an instruction That value will be in that variable further down Until a different value is placed in it Example Say we have the following algorithm BEGIN F() x  INPUT(‘Enter a number’) x  x * 2 RETURN x END F First we get a number from the user, and stores it in a variable. The next instruction then doubles the number in the variable. When that variable is returned, we can tell that it will be twice as big as whatever the user types in. Problem Solving: Correcting Algorithms

Understanding Algorithms Branching For this, we need to understand the condition being used As more instructions are only run depending on said condition There is also an important note to make There may be multiple routes to go down We need to be able to spot when we can only go down one If there are lots of ifs on there own We can go down as many as possible If there are lots of else ifs We can only go down one Example BEGIN F() x  INPUT(‘Enter a number’) IF MOD(x, 2) = 0 RETURN TRUE ELSE RETURN FALSE END IF END F We can only go down the IF or the ELSE, not both. This is dependent on the condition of the IF. Can you tell what this algorithm achieves? Problem Solving: Correcting Algorithms

Understanding Algorithms Iteration Here we need to work out how many times the loop happens This is based on The condition of the WHILE loop Or the given range of the FOR loop Both repeat instructions inside them WHILE loops act a bit like Ifs FOR loops require a bit more thought Example BEGIN F() x  INPUT(‘Enter a number’) FOR i FROM 0 TO x OUTPUT(i) END FOR END F Here we need to understand the range of the FOR loop. First the user enters a number (in x). The FOR loop then creates its own variable (i) and makes it go from 0 to the number in x. If the user enters 5, then the range is: (5 – 0) + 1 Why “+ 1”? Because the 5 is inclusive: 0, 1, 2, 3, 4, 5 Problem Solving: Correcting Algorithms

Problem Solving: Correcting Algorithms Can you work out what this algorithm is doing? BEGIN F(x) n  0 WHILE x > 0 IF MOD(x, 2) = 0 OUTPUT(n) x  x - 1 END IF n  n + 1 END WHILE END F Problem Solving: Correcting Algorithms

Problem Solving: Correcting Algorithms Determining Output There may be cases where we’re given an algorithm And we need to work out what the output will be These cases will often give us the values to use as input All we have to do is Work our way through the algorithm (using the input) Make note of any OUTPUT or RETURN statements If OUTPUT is used, then text will be output to the console We will need to state the output in the correct order If RETURN is used, then a value is going to be returned from the function We need to state what that returned value will be Problem Solving: Correcting Algorithms

Problem Solving: Correcting Algorithms Determining Output For example, let’s take the algorithm from earlier Let’s say the input is 10 here Can you work out what the output will be? BEGIN F(x) n  0 WHILE x > 0 IF MOD(x, 2) = 0 OUTPUT(n) x  x - 1 END IF n  n + 1 END WHILE END F 0 2 4 6 8 10 12 14 16 18 Problem Solving: Correcting Algorithms

Problem Solving: Correcting Algorithms Work out what this algorithm is doing Then work out its output with the following inputs TEST 1: x = 10, y = 8 TEST 2: x = 4, y = 12 BEGIN F(x, y) z  MIN(x, y) FOR i FROM z TO 0 IF MOD(x, i) = 0 AND MOD(y, i) = 0 RETURN i END IF END FOR RETURN 1 END F Problem Solving: Correcting Algorithms

Problem Solving: Correcting Algorithms Testing Algorithms We may get lost working through an algorithm Looking for its output/return values There is a tool we can employ to help us Called trace tables They are tables that list All the variables used in the algorithm (as columns) Values of the variables every time they change (as rows) The idea is we can read, from top-to-bottom, how the variables change To help us work out what the algorithm does Problem Solving: Correcting Algorithms

Problem Solving: Correcting Algorithms Testing Algorithms Let’s take the algorithm from the exercise as an example We had to see how the values of the variables changed To work out what the output was We can use a trace table to make this job easier BEGIN F(x, y) z  MIN(x, y) FOR i FROM z TO 0 IF MOD(x, i) = 0 AND MOD(y, i) = 0 RETURN i END IF END FOR RETURN 1 END F Problem Solving: Correcting Algorithms

Problem Solving: Correcting Algorithms Testing Algorithms x y z i RETURN 10 8 7 6 5 4 3 2 BEGIN F(x, y) z  MIN(x, y) FOR i FROM z TO 0 IF MOD(x, i) = 0 AND MOD(y, i) = 0 RETURN i END IF END FOR RETURN 1 END F Above is what the trace table for this algorithm could look like Using TEST 1 from the previous exercise Note that you can also find trace tables that split rows up by line number (and what happens on them). In that case, the above table would look like this: line x y z i RETURN --------------------------------------------------------------------------------------------- 1 10 8 2 8 3 8 3 7 3 6 3 5 3 4 3 3 3 2 5 2 Problem Solving: Correcting Algorithms

Problem Solving: Correcting Algorithms Testing Algorithms x y z i RETURN 10 8 7 6 5 4 3 2 BEGIN F(x, y) z  MIN(x, y) FOR i FROM z TO 0 IF MOD(x, i) = 0 AND MOD(y, i) = 0 RETURN i END IF END FOR RETURN 1 END F We write the variables in order of them being created State values for them whenever they change Problem Solving: Correcting Algorithms

Problem Solving: Correcting Algorithms Testing Algorithms x y z i RETURN 10 8 7 6 5 4 3 2 BEGIN F(x, y) z  MIN(x, y) FOR i FROM z TO 0 IF MOD(x, i) = 0 AND MOD(y, i) = 0 RETURN i END IF END FOR RETURN 1 END F If an earlier variable changes before one on the right We right it on a new row Problem Solving: Correcting Algorithms

Problem Solving: Correcting Algorithms Create two the trace tables for this algorithm Using the inputs: x = 4, y = 12 and then x = 15, y = 10 BEGIN F(x, y) z  MIN(x, y) FOR i FROM z TO 0 IF MOD(x, i) = 0 AND MOD(y, i) = 0 RETURN i END IF END FOR RETURN 1 END F Problem Solving: Correcting Algorithms

Problem Solving: Correcting Algorithms Create a trace table for the following algorithm Using an input of x = 8 BEGIN F(x) n  0 WHILE x > 0 IF MOD(x, 2) = 0 OUTPUT(n) x  x - 1 END IF n  n + 1 END WHILE END F Problem Solving: Correcting Algorithms

Correcting Algorithms We can also use trace tables to help us correct algorithms There may be times when the algorithm we have is incorrect It OUTPUT/RETURNs the wrong value We can use a trace table (with our own input) to check the algorithm Create inputs to use Think about what value we’re expecting See what the OUTPUT/RETURN value is (based off the input) If they are different, then the algorithm is incorrect Problem Solving: Correcting Algorithms

Correcting Algorithms Take this algorithm, for example It sees if a number is a prime number There is a problem with this algorithm Can you spot it? Don’t worry if you can’t We’ll use a trace table to help us work out where the problem is BEGIN IsPrime(x) FOR n FROM 3 TO x IF MOD(x, n) = 0 RETURN FALSE END IF END FOR RETURN TRUE END ISPrime Problem Solving: Correcting Algorithms

Correcting Algorithms Let’s use a trace table with an input of 5 The algorithm gets through the FOR loop and returns true The number is prime Which makes sense (5 is a prime number) x n RETURN 5 3 4 TRUE BEGIN IsPrime(x) FOR n FROM 3 TO x IF MOD(x, n) = 0 RETURN FALSE END IF END FOR RETURN TRUE END ISPrime Problem Solving: Correcting Algorithms

Correcting Algorithms Now let’s try an input of 4 The algorithm returns true Despite 4 not being a prime number Can you see where the problem is? x n RETURN 4 3 TRUE BEGIN IsPrime(x) FOR n FROM 3 TO x IF MOD(x, n) = 0 RETURN FALSE END IF END FOR RETURN TRUE END ISPrime We’re not checking to see if values are divisible by 2! Problem Solving: Correcting Algorithms

Problem Solving: Correcting Algorithms Use a trace table to help you fix the following algorithm The name of the function should give you a hint as to what it does Come up with your own inputs for the trace table tests BEGIN OutputRectangle(width, height) FOR i FROM 0 TO width FOR j FROM 0 TO width OUTPUT(‘0’) END FOR OUTPUT(‘\n’) END OutputRectangle Problem Solving: Correcting Algorithms