problem solving for programming session 4 calculating and keeping track of values

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

Upload: brook-pope

Post on 11-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

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

Problem Solving for Programming

Session 4Calculating and Keeping Track of Values

Page 2: 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?

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

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.

Gathering Coffees required Milk & sugar requirements

Friday 5 * coffee 3 black with no sugar1 black with one sugar1 white with two sugars

Saturday 6 * coffee 3 black with no sugar3 white with no sugar

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

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: π = 3.14. Is π a variable? It has a name and a value, but . . . .

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

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 requirements9. 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 ;

ENDWHILE12. IF (white coffee required)

12.1 Add milk/cream ; END IF// Pour and serve coffee13.Pour coffee into cup ;14.Stir coffee ;// Shut the machine down15.Turn off machine ;

• Number of sugars required

• Number of sugars added

• White coffee required?

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

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.

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

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.

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

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 ;

ENDWHILE12. IF (white coffee required)

12.1 Add milk/cream ; END IF13.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?

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

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.

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

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.

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

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?

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

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)

10.1. 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)

10.3.1 Add spoonful of sugar ;

10.3.2 Add 1 to number of sugars added ;

ENDWHILE

10.4. IF (white coffee required)

10.4.1 Add milk/cream ;

END IF

10.5. 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?

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

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?

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

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?

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

Carrying out the plan.

1. Find out cups required ;2. IF (more than zero cups required)

2.1. IF (more than six cups required)2.1.1 Limit cups required to six ;

ENDIF2.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 ;2.10. WHILE (cups poured not equal to cups required)

2.10.1. Find out how many sugars required ;2.10.2. Find out whether milk required ;2.10.3. WHILE( sugars added not equal to sugars required)

2.10.3.1. Add spoonful of sugar ;2.10.3.2. Add 1 to number of sugars added ;

ENDWHILE2.10.4. IF (white coffee required)

2.10.4.1. Add milk/cream ; ENDIF2.10.5. Pour coffee into cup ;2.10.6. Stir coffee ;2.10.7. Add 1 to number of cups poured ; ENDWHILE

2.11. Turn off machine ; ENDIF

• This solution satisfies our requirements to make coffee for up to six people, with the option for milk and sugar

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

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.

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

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.

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

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 name Description Range of values

cupsRequired Holds the number of cups of coffee to be made.

0-10?

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

Variable types

Variable name Description Range of values

cupsRequired Holds the number of cups of coffee to be made

0-10

milkRequired Holds the milk preference for one drinker

(Yes, No)

sugarsRequired Holds the sugars required for each drinker

0-5

cupsPoured Holds the number of cups poured so far

0-10

sugarsAdded Holds the number of sugars added so far

0-5

• What is different between the value for milkRequired and the other variables?

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

Initialising variables

• The problem we referred to a few slides previously occurs when we cycle through the WHILE loop at 2.10.

2.10. WHILE (cupsPoured not equal to cupsRequired)2.10.1. Find out how many sugarsRequired ;2.10.2. Find out whether milkRequired ;2.10.3. WHILE (sugarsAdded not equal to sugarsRequired)

2.10.3.1. Add spoonful of sugar ;2.10.3.2. Add 1 to number of sugarsAdded ;

ENDWHILE2.10.4. IF (milkRequired)2.10.4.1. Add milk/cream ; ENDIF2.10.5. Pour coffee into cup ;2.10.6. Stir coffee ;2.10.7. Add 1 to number of cupsPoured ;ENDWHILE

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

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.

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

Initialising variables1. Find out cupsRequired ;2. IF (cupsRequired more than zero)

2.1. IF (cupsRequired more than six)2.1.1 Limit cupsRequired to six ;

ENDIF2.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 ;2.11. WHILE (cupsPoured not equal to cupsRequired)

2.11.1. Initialise sugarsAdded to zero ; 2.11.2. Find out sugarsRequired ;

2.11.3. Find out milkRequired ; 2.11.4. WHILE (sugarsAdded not equal to sugarsRequired)

2.11.4.1. Add spoonful of sugar ;2.11.4.2. Add 1 to number of sugarsAdded ;

ENDWHILE 2.11.5. IF (whiteCoffeeRequired) 2.11.5.1. Add milk/cream ; ENDIF 2.11.6. Pour coffee into cup ; 2.11.7. Stir coffee ; 2.11.8. Add 1 to number of cupsPouredENDWHILE

2.12. Turn off machine ; ENDIF

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

Session reading

• Vickers – Chapter 5