Problem Solving for Programming Session 5 Problems involving arithmetic.

Slides:



Advertisements
Similar presentations
Working With Algorithm and Flowcharts
Advertisements

Heuristic Search techniques
The Little man computer
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 5: Steps in Problem Solving Stewart Blakeway FML 213
Chapter 11 Data Link Control
1 Pseudocode For Program Design. 22 Rules for Pseudocode Write only one statement per line Capitalise initial keyword Indent to show hierarchy and structures.
Problem Solving for Programming Session 6 Data and Control Abstractions.
CSC1016 Coursework Clarification Derek Mortimer March 2010.
Concurrent & Distributed Systems Lecture 5: Semaphores and Synchronisation Concurrent processes which don’t interact in some way don’t form an interesting.
Pseudocode and Algorithms
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
Programming Fundamentals (750113) Ch1. Problem Solving
H2-1 University of Washington Computer Programming I Lecture 10: Loop Development and Program Schemas © 2000 UW CSE.
Problem Solving for Programming Session 3 Dealing with Sequence, Choice and Repetition.
CSC103: Introduction to Computer and Programming
1 I.Introduction to Algorithm and Programming Algoritma dan Pemrograman – Teknik Informatika UK Petra 2009.
Problem Solving for Programming Session 4 Calculating and Keeping Track of Values.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
PROGRAMMING, ALGORITHMS AND FLOWCHARTS
Problem Solving for Programming Session 7 Data Types for Computer Programs.
CS001 Introduction to Programming Day 5 Sujana Jyothi
Problem Solving for Programming Session 2 Languages of Description and Problem Solving Strategies.
11.1 Chapter 11 Data Link Control Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Pseudo Code Possibly one of the hardest skills to get to grips with in learning a programming language is developing the ability to take a problem and.
Problem Solving for Programming Session 8 Static Data Structures.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
กระบวนการแก้ปัญหาด้วย คอมพิวเตอร์ 3 พฤษภาคม :00-17:00.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.
1 Chapter Six - Errors, Error Detection, and Error Control Chapter Six.
Algorithm Design.
CS221 Algorithm Basics. What is an algorithm? An algorithm is a list of instructions that transform input information into a desired output. Each instruction.
Copyright © 2009 Pearson Education, Inc. Chapter 5 Section 1 - Slide 1 Chapter 1 Number Theory and the Real Number System.
Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4.
1. Introduction Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Algorithm:a finite set of instructions.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
Operational strategies Lean production. The adoption of techniques that help to reduce waste.
Chapter Two Fundamentals of Data and Signals Data Communications and Computer Networks: A Business User's Approach Eighth Edition.
1 Algorithms CSCI 235, Fall 2015 Lecture 30 More Greedy Algorithms.
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
The Accumulator Pattern Summing: Add up (“accumulate”), e.g. 1 2 plus 2 2 plus 3 2 plus … plus Variation: Form a product (instead of sum), e.g.
Algorithms and Pseudocode
Efficiently Solving Computer Programming Problems Doncho Minkov Telerik Corporation Technical Trainer.
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
Sorting Algorithms. Algorithms, revisited What is an algorithm? Wikipedia Definition: an algorithm is a definite list of well-defined instructions for.
Problem of the Day  On the next slide I wrote today’s problem of the day. It has 3 possible answers. Can you guess which 1 of the following is the solution?
Arithmetic Sequences.
Control Structures II Chapter 3
Topics Introduction to Repetition Structures
ALGORITHMS AND FLOWCHARTS
Chapter 5: Control Structure
Teaching design techniques to design efficient solutions to problems
Yenka Portfolio Level for this topic: Student Name : My Levels
Lecture on Data Link Control
A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 13 (Reed) - Conditional.
Iteration with While You can say that again.
Control Structure Senior Lecturer
Objectives Recognize and extend an arithmetic sequence.
Programming Fundamentals (750113) Ch1. Problem Solving
Algorithm Discovery and Design
Algebra
Chapter 11 Data Link Control
Topics Introduction to Repetition Structures
Last Class We Covered Recursion Stacks Parts of a recursive function:
Basic Concepts of Algorithm
A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 13 (Reed) - Conditional.
Presentation transcript:

Problem Solving for Programming Session 5 Problems 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 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.

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 ; ENDWHILE 3. 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 nameDescriptionRange 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.

Carrying out the plan 1. 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) load parcel on van ; Add parcelWeight to payload; Get next parcelWeight ; ENDWHILE 1.4 Despatch van ; ENDWHILE 2. Report numberOfVansUsed ; 3. Report heaviestVan ; Problems 1.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 # 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  payload ENDIF 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) load parcel on van ; Add parcelWeight to payload; Get next parcelWeight ; ENDWHILE 3.4 Despatch van ; 3.5 numberOfVansUsed  numberOfVansUsed + 1 ; 3.6 IF (payload more than heaviestVan) heaviestVan  payload ; ENDIF ENDWHILE 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 result 1. 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) load parcel on van ; Add parcelWeight to payload; Get next parcelWeight ; ENDWHILE 5.3 Despatch van ; 5.4 numberOfVansUsed  numberOfVansUsed + 1 ; 5.5 IF (payload more than heaviestVan) heaviestVan  payload ; ENDIF ENDWHILE 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