September 26, 2022, 12:04:36 AM


Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........

Programming 3 - A Simple Simulation

Started by GWS, April 27, 2010, 02:18:28 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.


April 27, 2010, 02:18:28 PM Last Edit: April 29, 2010, 10:59:47 AM by GWS
 OK, now for a little scare .. you didn't think this was going to be an easy ride did you ?  

So now, you are employed at a busy bakery as packer/dispatcher ..    

The manager is a control freak, and wants to keep a close eye on output - so she'd like a report from you every 20 minutes.
What a pain .. but she's a pretty young thing, and you'd like to show her what you can do ..

You come up with this idea to use your laptop to keep track of what's happening.  It's mayhem !

Every 2 minutes a door bursts open, and people from the bakery rush in with trays of cakes - some with pink icing, others with yellow icing.

Each delivery has at least 10 cakes of one sort or the other, but it can be as many as 20 of each kind.

What we need to demonstrate how the process is working, is a simulation.  This is a program to mimic typical events and keep track of the timing and statistics.

So here's what you might come up with once you get familiar with the 'instruction set' - the 'toolset' which the language provides.

Don't worry that you're not familiar with all this yet.  The point is just to give you a feel of what is possible.  We'll examine the instructions later.

Programming 3.

A Simulation .....


def elapsedtime,start,TotalCakes:int
def pink,pinktotal,yellow,yellowtotal:int
def PercentPink,Percentyellow:int

elapsedtime = 0
start = timer

print "Pink Cakes      Yellow Cakes":print

' a delivery every two seconds ..
  if (timer - start) >= 2
' there will be at least 10 cakes of each colour
' but sometimes up to 20 per delivery ..
     pink = int(rnd(10) + 10)
     yellow = int(rnd(10) + 10)
     pinktotal = pinktotal + pink
     yellowtotal = yellowtotal + yellow
     print string$(3," "),pink,string$(14," "),yellow
' get ready for the next arrival ..
     start = timer
     elapsedtime = elapsedtime + 2

until elapsedtime >= 20

' analyse what happened in the last 20 minutes ..
TotalCakes = pinktotal + yellowtotal
PercentPink = pinktotal / TotalCakes * 100
Percentyellow = yellowtotal / TotalCakes * 100

print "Survey Time           ",elapsedtime, " Minutes":print
print "Number of cakes       ", TotalCakes:print
print "Total Pink Cakes      ",pinktotal
print "Total Yellow Cakes    ",yellowtotal:print
print "Percent Pink:         ",PercentPink," %"
print "Percent Yellow:       ",PercentYellow, " %"

do:until inkey$<>""


We've speeded things up so that 2 minutes becomes every two seconds - you can do things like that in a simulation.

If you copy and paste the above into a new editing window, using 'File - New Source File', you can run the simulation by clicking the green Run button.

Each run presents the results of 20 minutes of operation, scaled down to 20 seconds so we don't have to hang around too long ..  

Notice we are using the Console Window again - we've just inserted our new application instructions.  You can save the program in a new folder so you can play with it again later.  Just do a 'File - Save As' and give it a name like 'BakerySim' or something.

End of Programming 3


How impressed do you think the manager lady will be with that printed out to show production details.  Of course in this example, we've simulated the number of cakes coming in just to show how it works.   In a real situation, you would keep count of each delivery and type the numbers in as input, printing the results every 20 minutes.

Welcome to the real world of applications programming.

All you need is to think what you want to achieve - and assemble a few instructions to do the deed .. easy ..  

Well, maybe we need to learn a few basics first .. we'll look at that next.
Tomorrow may be too late ..