Problem Solving for Programming Session 4 Calculating and Keeping Track of Values.

Slides:



Advertisements
Similar presentations
Bison Management Suppose you take over the management of a certain Bison population. The population dynamics are similar to those of the population we.
Advertisements

Process Synchronization Continued 7.2 The Critical-Section Problem.
The LC-3 – Chapter 6 COMP 2620 Dr. James Money COMP
Chapter 2: Problem Solving
Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University.
The Little man computer
Tutorial #6 PseudoCode (reprise)
Foundation of programming Week 3. Last week ‘How to think like a programmer’ The HTTLAP 6 step approach: Understand the problem – Devise a plan to solve.
Programming Logic and Design, Third Edition Comprehensive
Problem Solving for Programming Session 5 Problems involving arithmetic.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
place a teabag in a mug; boil some water; pour over teabag; wait until strong enough; remove the teabag; add some milk and stir. what is this?
Problem Solving for Programming Session 6 Data and Control Abstractions.
1 Introduction to Computability Theory Lecture15: Reductions Prof. Amos Israeli.
1 Introduction to Computability Theory Lecture12: Reductions Prof. Amos Israeli.
CSC1016 Coursework Clarification Derek Mortimer March 2010.
Chapter 2: Design of Algorithms
Original Source : and Problem and Problem Solving.ppt.
Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.
Group practice in problem design and problem solving
Real Numbers and the Decimal Number System
Problem Solving for Programming Session 7 Data Types for Computer Programs.
CS001 Introduction to Programming Day 5 Sujana Jyothi
Imperative. We use imperatives for such as telling people what to do giving instructions and advice making suggestions.
An Overview of Programming in Python CSC 161: The Art of Programming Prof. Henry Kautz 9/9/2009 Slides stolen shamelessly from Dr. Mark Goadrich, Centenary.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
A PowerPoint about Algorithm’s. What is an algorithm?  a process or set of rules to be followed in calculations or other problem-solving operations,
School of Computer Science & Information Technology G6DICP - Lecture 9 Software Development Techniques.
By the end of this session you should be able to...
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Chapter 2 Pseudocode. Objectives To introduce common words, keywords and meaningful names when writing pseudocode To define the three basic control structures.
Pseudocode Simple Program Design Third Edition A Step-by-Step Approach 2.
Introduction to Problem Solving. Steps in Programming A Very Simplified Picture –Problem Definition & Analysis – High Level Strategy for a solution –Arriving.
Flowcharts and Algorithms. Review of Terms  A computer is a machine that can represent and manipulate data –Ultimately the data and the instructions.
04/11/ Arrays 1D Arrays Defining, Declaring & Processing.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Learn about the system life cycle Plan the outline of your project
ALGORITHMS.
 In this chapter you will learn about:  Introduction to Problem Solving  Software development method (SDM)  Specification of needs  Problem analysis.
Chapter 7 Problem Solving with Loops
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Algorithms and Pseudocode
Algorithms. Teacher Lead Activity What is an algorithm? Homework Recap!
Sequential Processing to Update a File Please use speaker notes for additional information!
Pseudocode Skill Area Materials Prepared by Dhimas Ruswanto, BMm.
Sequences, Modules and Variables David Millard
Selection Using IF THEN ELSE CASE Introducing Loops.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Control Structures: Conditionals, If/Else and Loops David Millard
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.
Mixtures And Solutions
Introduction To Flowcharting
Yenka Portfolio Level for this topic: Student Name : My Levels
Control Structure Senior Lecturer
Program Design Introduction to Computer Programming By:
Programming Fundamentals (750113) Ch1. Problem Solving
An Introduction to VEX IQ Programming with Modkit
Algorithm Discovery and Design
Algorithm Discovery and Design
Repetition In today’s lesson we will look at:
Flowchart Repetition Problem Solving.
3.1 Iteration Loops For … To … Next 18/01/2019.
Flowcharts and Pseudo Code
Prepared By: Deborah Becker
Software Development Techniques
3.2 Working with Data Scope of variables 29/07/2019.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Problem Solving for Programming Session 4 Calculating and Keeping Track of Values

Variables Imagine the following scenario: On Friday, you hold a teetotal gathering at your house. Guests arrive. You ask them what they want to drink. Five want coffee. Three want tea. Of the coffee drinkers, three want their drink black without sugar, one black with one sugar and one white with two sugars. On Saturday, you have another gathering. This time six people want coffee. Again, three want their coffee black. But, this time three want their coffee with milk. Nobody wants sugar. What is different between the two gatherings? What remains the same?

Variables The number of coffees has changed, as have the milk and sugar requirements. However, the things the numbers represent (e.g. coffees required and milk and sugar requirements) have remained the same. GatheringCoffees requiredMilk & sugar requirements Friday5 * coffee3 black with no sugar 1 black with one sugar 1 white with two sugars Saturday6 * coffee3 black with no sugar 3 white with no sugar

Variables What we have are examples of variables. A variable is used for tracking values in a program or algorithm. Variables have two parts: –A name (e.g. coffees required, client surname, valid username) –A value (e.g. 6, Smith, Yes/No) A variable name will remain the same throughout the lifetime of a program. But the value that the variable will hold can vary. Q: π = Is π a variable? It has a name and a value, but....

Variables What are the variables in the coffee making algorithm we developed in the previous session? // Set up coffee machine 1. Put water in coffee machine ; 2. Open the coffee holder ; 3. Put filter paper in machine ; 4. Measure coffee for one cup ; 5. Put coffee into filter paper ; 6. Shut the coffee holder ; 7. Turn on machine ; 8. Wait for coffee to filter through ; // Deal with milk and sugar requirements 9. Find out how many sugars required ; 10.Find out if white coffee required ; 11.WHILE (sugars added is not equal to sugars required) 11.1 Add spoonful of sugar ; 11.2 Add 1 to number of sugars added ; ENDWHILE 12. IF (white coffee required) 12.1 Add milk/cream ; END IF // Pour and serve coffee 13.Pour coffee into cup ; 14.Stir coffee ; // Shut the machine down 15.Turn off machine ; Number of sugars required Number of sugars added White coffee required?

Variables Consider the following extension to our coffee making problem: Using an electronic percolator, make coffee for up to six people. Add milk and sugar as required. Again, the best way to approach the problem is to use the solving framework outlined previously.

Understanding the problem Q. What are we being asked to do? Make coffee for up to six people. Each cup may or may not require milk and sugar. Q. What is unknown? The capacity of the of the coffee pot and the water reservoir. Does up to six people mean 5 or 6 persons? Q. What are the principal parts of the problem? Make a pot of coffee. Pour several cups. Add milk and sugar as required. Q. Have we made any assumptions? For the time being, we will assume that the pot will hold six cups of coffee, the water reservoir will hold enough water for six cups and the filter will hold enough coffee for six cups. We will also assume that up to six means 6.

Devising a Plan Q. Have we solved this or a similar problem previously? Yes, we solved a similar problem for one cup of coffee. 1. Put water in coffee machine ; 2. Open the coffee holder ; 3. Put filter paper in machine ; 4. Measure coffee for cups required ; 5. Put coffee into filter paper ; 6. Shut the coffee holder ; 7. Turn on machine ; 8. Wait for coffee to filter through ; 9. Find out how many sugars required ; 10.Find out if white coffee required ; 11.WHILE (sugars added is not equal to sugars required) 11.1 Add spoonful of sugar ; 11.2 Add 1 to number of sugars added ; ENDWHILE 12. IF (white coffee required) 12.1 Add milk/cream ; END IF 13.Pour coffee into cup ; 14.Stir coffee ; 15.Turn off machine ; What parts of the algorithm need repeating in order to get up to 6 cups of coffee? How will we implement this repetition?

Carrying out the plan. We can see that steps #9 through to #14 need repeating in order to get up to six cups. The repetition can be done by adding another WHILE loop. WHILE (cups required) Find out how many sugars required ; Find out if white coffee required ; WHILE (sugars added is not equal to sugars required) Add spoonful of sugar ; Add 1 to number of sugars added ; ENDWHILE IF (white coffee required) Add milk/cream ; END IF Pour coffee into cup ; Stir coffee ; ENDWHILE How would we modify this solution to make sure the outer WHILE loop terminates when the cups required condition becomes false? Think about how we dealt with the WHILE loop for adding sugar.

Carrying out the plan. We would use the same technique as we did for the sugars loop. We would create a variable to count the number of cups poured and keep comparing that to the number of cups required: WHILE (cups poured is not equal to cups required) Find out how many sugars required ; Find out if white coffee required ; WHILE (sugars added is not equal to sugars required) Add spoonful of sugar ; Add 1 to number of sugars added ; ENDWHILE IF (white coffee required) Add milk/cream ; END IF Pour coffee into cup ; Stir coffee ; Add 1 to cups poured ; ENDWHILE Trace through this code with a request for 2 cups of black coffee with one sugar.

Carrying out the plan (1) Find out how many cups required ; 1. Put water in coffee machine ; 2. Open the coffee holder ; 3. Put filter paper in machine ; 4. Measure coffee for cups required ; 5. Put coffee into filter paper ; 6. Shut the coffee holder ; 7. Turn on machine; 8. Wait for coffee to filter through ; (2) Find out how many cups required ; 9.WHILE (cups poured is not equal to cups required) Just as we have to find out how many sugars are required, we also have to find out the total number of cups of coffee that are required. There seem to be two possible positions for such an instruction (1) & (2). Which one is correct? Why?

Carrying out the plan 1. Find out how many cups required; 2. Put water in coffee machine ; 3. Open the coffee holder ; 4. Put filter paper in machine ; 5. Measure coffee for cups required ; 6. Put coffee into filter paper ; 7. Shut the coffee holder ; 8. Turn on machine ; 9. Wait for coffee to filter through ; 10.WHILE (cups poured is not equal to cups required) Find out how many sugars required ; 10.2 Find out if white coffee required ; 10.3 WHILE (sugars added is not equal to sugars required) Add spoonful of sugar ; Add 1 to number of sugars added ; ENDWHILE IF (white coffee required) Add milk/cream ; END IF Pour coffee into cup ; 10.6 Stir coffee ; 10.7 Add 1 to cups poured ; ENDWHILE 11.Turn off machine; (1) is correct because if we perform this step any later (e.g. at (2)) how will we know how much water and coffee to put in the machine? There are still two problems with this solution. Can you spot them?

Carrying out the plan. Sub-problem 1. What if nobody wants coffee (e.g. cups required = 0)? Sub-problem 2. What if our guests ask for 7 cups of coffee, or 12? We have already said that our solution is for up to six cups of coffee. Can you think of a satisfactory solution to either of these problems?

Carrying out the plan. Solution 1. If nobody wants coffee, then we do not want to perform any of the steps (e.g. put water or coffee in the machine, or turn the machine on). It would be a waste of our valuable time and money. Solution 2. When we have a request for more than six cups (e.g. 10), we can limit the number of cups to six. Which is what we would probably do in the real world if we had ten people and only enough coffee for six people. The others would have to drink tea or juice. How could these solutions be implemented in the existing code?

Carrying out the plan. 1. Find out cups required ; 2. IF (more than zero cups required) 2.1. IF (more than six cups required) Limit cups required to six ; ENDIF 2.2. Put water for cups required in coffee machine ; 2.3. Open coffee holder ; 2.4. Put filter paper in machine ; 2.5. Measure coffee for cups required ; 2.6. Put coffee into filter paper ; 2.7. Shut the coffee holder ; 2.8. Turn on machine ; 2.9. Wait for coffee to filter through ; WHILE (cups poured not equal to cups required) Find out how many sugars required ; Find out whether milk required ; WHILE( sugars added not equal to sugars required) Add spoonful of sugar ; Add 1 to number of sugars added ; ENDWHILE IF (white coffee required) Add milk/cream ; ENDIF Pour coffee into cup ; Stir coffee ; Add 1 to number of cups poured ; ENDWHILE Turn off machine ; ENDIF This solution satisfies our requirements to make coffee for up to six people, with the option for milk and sugar

Assess the result Run through the algorithm with the following values: –0 coffees –2 coffees (with milk and two sugars) There is one important problem left in the solution. Did you spot it when you were tracing though the code? We will come back to this problem in a few slides.

More on variables So far we have named our variables in natural language (e.g. cups required). A widely used convention for variable naming in programming is to use camel casing. This convention requires a lower case letter for the first word of the first character of a variable name: –total If there are two or more words in the name, then there is no spacing between the words and the first character of the second word is capitalised: –sugarsAdded –coffeesRequired –numberCupsRequired Variables should be named as succinctly as possible (e.g. sugarsAdded rather than numberOfSugarsAddedToTheCoffee.

More on variables Write down the variable names for the coffee making algorithm in camel casing. Write a short description of what the variable represents. Also, give an indicative value range for each variable. Variable nameDescriptionRange of values cupsRequiredHolds the number of cups of coffee to be made. 0-10?

Variable types Variable nameDescriptionRange of values cupsRequiredHolds the number of cups of coffee to be made 0-10 milkRequiredHolds the milk preference for one drinker (Yes, No) sugarsRequiredHolds the sugars required for each drinker 0-5 cupsPouredHolds the number of cups poured so far 0-10 sugarsAddedHolds the number of sugars added so far 0-5 What is different between the value for milkRequired and the other variables?

Initialising variables The problem we referred to a few slides previously occurs when we cycle through the WHILE loop at WHILE (cupsPoured not equal to cupsRequired) Find out how many sugarsRequired ; Find out whether milkRequired ; WHILE (sugarsAdded not equal to sugarsRequired) Add spoonful of sugar ; Add 1 to number of sugarsAdded ; ENDWHILE IF (milkRequired) Add milk/cream ; ENDIF Pour coffee into cup ; Stir coffee ; Add 1 to number of cupsPoured ; ENDWHILE

Initialising variables If the number of cups required = 2 at the start of the loop and the number of sugars required is two for each person, then the second person gets no sugar! Can you see why? The problem is that after the first cycle through the outer loop (e.g. after the first cup has been created), sugarsAdded has a value of 2. When the outer loop begins its second cycle, the value of sugarsAdded is still 2, which means it is already equal to sugarsRequired and the condition is false, so no sugar is added. What we need to do is reset or initialize the variable sugarsAdded back to zero each time we start a new cup of coffee. What is true of sugarsAdded is also true of cupsPoured. This variable should also be initialised to zero.

Initialising variables 1. Find out cupsRequired ; 2. IF (cupsRequired more than zero) 2.1. IF (cupsRequired more than six) Limit cupsRequired to six ; ENDIF 2.2. Put water for cupsRequired in coffee machine ; 2.3. Open coffee holder ; 2.4. Put filter paper in machine ; 2.5. Measure coffee for cupsRequired ; 2.6. Put coffee into filter paper ; 2.7. Shut the coffee holder ; 2.8. Turn on machine ; 2.9. Wait for coffee to filter through ; 2.10.Initialise cupsPoured to zero ; WHILE (cupsPoured not equal to cupsRequired) Initialise sugarsAdded to zero ; Find out sugarsRequired ; Find out milkRequired ; WHILE (sugarsAdded not equal to sugarsRequired) Add spoonful of sugar ; Add 1 to number of sugarsAdded ; ENDWHILE IF (whiteCoffeeRequired) Add milk/cream ; ENDIF Pour coffee into cup ; Stir coffee ; Add 1 to number of cupsPoured ENDWHILE Turn off machine ; ENDIF

Session reading Vickers –Chapter 5