1 / 23

Problem Solving for Programming

Problem Solving for Programming. Session 4 Calculating and Keeping Track of Values. Variables. Imagine the following scenario:

rama-morgan
Télécharger la présentation

Problem Solving for Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. 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.

  4. 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 . . . .

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

  6. 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.

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

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

  9. 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.

  10. 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.

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

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

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

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

  15. Carrying out the plan. • This solution satisfies our requirements to make coffee for up to six people, with the option for milk and sugar 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 ; 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 ; 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 ; ENDWHILE 2.10.4. IF (white coffee required) 2.10.4.1. Add milk/cream ; ENDIF 2.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

  16. 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.

  17. 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 thefirst character of the second word is capitalised: • sugarsAdded • coffeesRequired • numberCupsRequired • Variables should be named as succinctly as possible (e.g. sugarsAdded rather than numberOfSugarsAddedToTheCoffee.

  18. 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.

  19. Variable types • What is different between the value for milkRequired and the other variables?

  20. 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 (sugarsAddednot equal to sugarsRequired) 2.10.3.1. Add spoonful of sugar ; 2.10.3.2. Add 1 to number of sugarsAdded ; ENDWHILE 2.10.4. IF (milkRequired) 2.10.4.1. Add milk/cream ; ENDIF 2.10.5. Pour coffee into cup ; 2.10.6. Stir coffee ; 2.10.7. Add 1 to number of cupsPoured ; ENDWHILE

  21. 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.

  22. Initialising variables • 1. Find out cupsRequired ; • 2. IF (cupsRequired more than zero) • 2.1. IF (cupsRequired more than six) • 2.1.1 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 ; • 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 cupsPoured • ENDWHILE • 2.12. Turn off machine ; • ENDIF

  23. Sessionreading • Vickers • Chapter 5

More Related