22
Problem Solving for Programming Session 5 Problems involving arithmetic

Problem Solving for Programming Session 5 Problems involving arithmetic

Embed Size (px)

Citation preview

Problem Solving for Programming

Session 5Problems involving arithmetic

Problems involving arithmetic

• Consider the following problem:• Paul’s Premier Parcels (PPP) is a delivery company. Each morning the parcels

to be delivered for that day are lined up on a conveyor belt in the warehouse by the warehouse staff. The belt takes the parcels to the loading bay where a van waits. The loading crew weigh each parcel to see if it will fit on the van. If so, they put the parcel on the van and wait for the next parcel to arrive. As soon as a parcel arrives that would take the van over its maximum payload, the van door is closed, the van sent off on its rounds and another empty van is brought up. The van is not kept waiting to see if any lighter parcels that will fit show up. This activity continues until all the parcels have been loaded onto vans. Happily, there are always enough vans for the waiting parcels.

Problems involving arithmetic

• PPP wants to install a weighing machine on the conveyor belt linked to a computer so that the process of weighing parcels can be speeded up. Each van has the same maximum payload of 750 kg, and PPP only accepts parcels up to 120 kg in weight. After all the parcels have been loaded, the company’s managing director wants to know how many vans were needed that day and what was the heaviest payload that was sent out.

Understanding the problem

• What are you being asked to do?• What exactly is required?• What is unknown? • What are the principal parts of the problem?• Does the main problem contain any sub-problems? • Have you made any assumptions?• What can you do about the assumptions you have

made? • Can the problem be better expressed by using a

diagram or a picture?

Understanding the problem

40kg 30kg110kg 110kg 110kg 110kg 110kg 110kg

50kg

X

40 70 180 290 400 510 620 730 780

Scales

• A graphical representation should help us see the problem at hand more clearly.

Devising a plan

• Have you solved a similar problem before? (Are there any patterns you recognize in this problem that are similar to the coffee making problem?)

• Are some of the parts more easily solved than others?

• Can you solve a simpler version of the problem?

• Have you overlooked anything?

Carrying out the plan• This appears a more difficult problem to solve than

the coffee making problem. To make it simpler we could solve the simpler analogous problem of loading one parcel first.

• We can then solve the problem of loading multiple parcels onto a single van. In doing this, we can see clearly the repetition element in the process. We weigh and load parcels until the van on the bay is full. Then we despatch the van and call up another one for loading.

Weigh parcel If parcel fits on van Add parcel to vanEnd if

Weigh parcel If parcel fits on van Add parcel to vanEnd if

Weigh parcelIf parcel fits on van Add parcel to vanEnd if

Weigh parcelIf parcel fits on van Add parcel to vanEnd if . . . . . . .Despatch vanCall up another van for loading

Devising a plan

• Have you solved a similar problem before? (Are there any patterns you recognize in this problem that are similar to the coffee making problem?)

• Are some of the parts more easily solved than others?

• Can you solve a simpler version of the problem?

• Have you overlooked anything?

Carrying out the plan

• Again, repetitions in the process can be represented with a WHILE loop.

1. Weigh first parcel ;2. WHILE (room on van)

2.1 load parcel on van ;2.2 weigh next parcel ;

ENDWHILE3. Despatch van ;4. Make note of van’s payload ;

• How would we work out when the van is full? Think about the coffee making problem. How did we solve the problem of adding the correct number of sugars that our guests requested?

Carrying out the plan• The van’s capacity is 750kg. If a parcel appears on the

conveyor that takes the van over that limit (e.g. If the van’s current payload is at 730kg and a parcel of 50kg comes along the conveyor), then the van is deemed full and despatched.

• To implement this solution we will need three variables:– capacity – payload– parcelWeight

• If parcelWeight plus payload is greater than capacity, then the van is full and is despatched.

Carrying out the plan 1. Initialize payload to zero ;

2. Weigh first parcel ;

3. WHILE (payload + parcelWeight is less than or equal to capacity)

3.1 load parcel on van ;

3.2 Add parcelWeight to payload;

3.3 weigh next parcel ;

ENDWHILE

4. Despatch van ;

• Do you notice any difference between capacity and the other two variables?

Variable name Description Range of values

? Holds the weight of one parcel

?

? Holds the weight of the total load on the van

?

? Holds the maximum payload of the van

?

Assessing the result 1. Initialize payload to zero ;

2. Get first parcelWeight ;

3. WHILE (payload + parcelWeight is less than or equal to capacity)

3.1 load parcel on van ;

3.2 Add parcelWeight to payload;

3.3 Get next parcelWeight ;

ENDWHILE

4. Despatch van

• Step through this solution with the following parcel weights: 120, 90, 110, 70, 60, 120, 110, 90, 110, 50. How many parcels are on the van when it leaves the depot?

Devising a plan• What about the more complex problem of loading multiple vans? • Once one van is loaded and despatched, another is called up and filled and this

process in repeated until all the parcels that appear on the conveyor have been delivered.

• This would give us an overall structure something like this:

• To this, we can add to the requirement to report on the number of vans used and the heaviest van despatched.

• Extend the current solution to include the above elements.

While the conveyor belt is not empty Load parcels onto vans End While

While the conveyor is not empty Load parcels onto vans End While Report on the number of vans sent outReport on the heaviest payload

Carrying out the plan1. WHILE (conveyor not empty)

1.1 Initialize payload to zero ; 1.2 Get first parcelWeight ; 1.3 WHILE (payload + parcelWeight is less than or equal to capacity)

1.3.1 load parcel on van ;1.3.2 Add parcelWeight to payload;1.3.3 Get next parcelWeight ; ENDWHILE

1.4 Despatch van ;ENDWHILE

2. Report numberOfVansUsed ; 3. Report heaviestVan ;

• Problems1. Where do numberOfVansUsed and heaviestVan get their values?2. What happens if the conveyor belt becomes empty half way through loading a van (e.g.

#1.3.1)?3. The position of task #1.2 and task #1.3.3. Task #1.3.3 gets a new parcel. If this parcel will not

fit, then the van is despatched and the outer WHILE loop is begun again. What is the implication of this?

Number of vans• Problem: We need to count the total number of vans

despatched during the course of the day. This value will be held in numberOfVansUsed.

• Solution: We start the day with zero vans despatched, so we should initialize numberOfVansUsed to zero. We can then increment numberOfVansUsed by 1 every time a van is despatched giving us a final count.

numberOfVansUsed zero ;numberOfVansUsed numberOfVansUsed + 1 ;

• Where should these lines be positioned in the pseudo code?

Heaviest van• Problem: We need to compare the payloads of the vans and make a note

of the weight of the heaviest van. • Solution: At the start of the day the heaviest van sent out is zero, so we

initialize heaviestVan to zero. Every time a van is sent out we compare its payload to the value of heaviestVan. If payload is greater than the current value of heaviestVan, then we replace the value of heaviestVan with the value of payload.

heaviestVan zero ;IF (payload more than heaviestVan)

heaviestVan payloadENDIF

• Where should these lines be positioned in the code?

Number of vans and heaviest van

1. numberOfVansUsed zero ;2. heaviestVan zero ;3. WHILE (conveyor not empty)

3.1 Initialize payload to zero ; 3.2 Get first parcelWeight ; 3.3 WHILE (payload + parcelWeight is less than or equal to capacity)

3.3.1 load parcel on van ;3.3.2 Add parcelWeight to payload;3.3.3 Get next parcelWeight ;ENDWHILE

3.4 Despatch van ; 3.5 numberOfVansUsed numberOfVansUsed + 1 ; 3.6 IF (payload more than heaviestVan)

3.6.1 heaviestVan payload ; ENDIFENDWHILE

4. Report numberOfVansUsed ; 5. Report heaviestVan ;

Empty conveyor• Problem: Imagine we are working on the loading bay. Halfway through

loading a van, the conveyor becomes empty. If there is no parcel, then there is no parcel weight and #3.3.3 cannot get its value (What do you think would happen in an eventuality like this?).

• Solution: Currently, the WHILE loop at #3.3 terminates when payload + parcelWeight is greater than capacity. We need to update it so that it also terminates if the conveyor becomes empty.

• To achieve this, we add a second condition to the WHILE loop using an AND

operator.

WHILE (payload + parcelWeight is less than or equal to capacity) AND (conveyor not empty) . . .

• Now if the conveyor becomes empty halfway through loading, the incomplete van will be despatched.

The lost parcel • Problem: Task #3.3.3 gets a new parcel weight. If

the parcel will not fit on the van, the van is despatched and the WHILE in task #3 is begun again, which causes task #3.2 to be performed again. The result of this is that the parcel that would not fit on the van is effectively lost.

• Solution: Action #1.2 is in the wrong place and needs to be moved. There is only one first parcel in any given day so we need only get this value once, at the beginning of the sequence before the outer WHILE loop commences.

Assess the result1. capacity 750 ;2. numberOfVansUsed zero ;3. heaviestVan zero ;4. Get first parcelWeight; 5. WHILE (conveyor not empty)

5.1 payload zero ; 5.2 WHILE (payload + parcelWeight is less than or equal to capacity)

5.2.1 load parcel on van ;5.2.2 Add parcelWeight to payload;5.2.3 Get next parcelWeight ;ENDWHILE

5.3 Despatch van ; 5.4 numberOfVansUsed numberOfVansUsed + 1 ; 5.5 IF (payload more than heaviestVan)

5.5.1 heaviestVan payload ; ENDIFENDWHILE

6. Report numberOfVansUsed ; 7. Report heaviestVan ;

Assess the result

• How many vans were sent out and what was the heaviest payload given the parcel weights below?

(1) 40, 20, 120, 100, 40, 100, 35, 45

(2) 50, 90, 120, 110, 40, 30, 85, 85, 110, 100, 100, 100, 100, 120, 90, 50, 85, 120, 40

Session reading

• Vickers – Chapter 5