IPT - A Virtual Approach IPT A Virtual Approach by Peter Whitehouse
Quick Links:
 
 
Information and Intelligent Systems Social and Ethical Implications Human Computer Interaction Software and Systems Engineering eXercise Files Course Outline and Assessment A-Z of Geeky Acronyms Terrace Work Program 2004 Sillybus FAQ = Frequently Asked Questions Help
 
 

100x100 coins

A Pascal Problem

Tossing a coin

A coin toss in pascal needs some care, to make it a truly 'random' event. One solution is to randomly generate a number (say between 0 and 9) and if the number is less than or equal to 4 it is a head, otherwise it is a tail.

In pascal, the random number generator needs to be 'seeded', otherwise it generates the same sequence of numbers each time.

randomize;

will do this - actually, it instructs the computer to go get the system time, and use that as the basis of random number generation - that way, theoretically, it makes different numbers every time the program is run.

To toss one coin, the idea would be to do something similar to this:

randomize;
coin := random(9);
if coin <= 4
  then  {it is a head}
  else  {it is a tail}

where COIN is a byte or integer variable

to do this many times, you could put that process into a loop similar to this (suppose we want printed on the screen the results of 20 tosses:

randomize;
for counter := 1 to 20 do
  begin

    coin := random(10);
    if coin <= 4
       then write('HEAD ')
       else write('TAIL ')
  end

where COUNTER is an integer or byte variable. For 100 tosses, the code would be similar

A One Dimensional Array

An ARRAY is a highly organised storage container - you know tupperware - think tupperware. Imagine (for a ONE dimensional array, which is what you need for this problem - it has length... more on that soon) you staple together in a long line, one after the other, 10 identical tupperware containers. You get out your nikko pen (you know your mum is gonna be mad, may as well make it real mad) and write "1" on the first, "2" on the second, all the way up to "10" for the last.

fred 1 dimensional array

Because the boxes are particular shapes, they are designed for particular data - arrays are ORGANISED collections of storage containers for the SAME TYPE of information - you can't put watermelon in one, grapes in another and pumpkin soup in the next - you have to put the same sort of thing in each.

If we name the collection of boxes above something (say FRED), then we can place something (the number 96, say) into the third box of fred using:

  fred[3] := 96;

where FRED was defined as an array of integers so:

Var
  fred : array[1..10] of integer;

As you can see, we have defined FRED as a 10 compartment storage area (indexed 1..10).

The "of integer" part says what sort of thing can be stored in each part of FRED - in this case, single integers are the ONLY sort of thing FRED can store, one only for each part of FRED.

We can change what is in each compartment of FRED by simply re-assigning that part:

fred[2] := 5;
fred[3] := fred[2] * 5;

in this instance, we placed an integer value (5) into the 2nd part of FRED, then replaced what was in the 3rd part of FRED by what is in the 2nd part of fred (fred[2]) times 5. This means that the previous value of fred[3] is replaced by the value 10.

Printing an Array

Typically, we use loops to print arrays - since they are ordered, we can move from part to part, printing as we go. If we had continued to play with FRED, and wanted to see all of the values stored there, we could use:

for counter := 1 to 10 do
  showmessage( inttostr(fred[counter]) )

where COUNTER is an integer or byte variable. As counter changes from 1 to 2 to 3 and so on, the output statement looks in the corresponding part of FRED, and outputs what it finds there.

Initializing an Array

When you begin to use arrays, you need to be sure of what is inside them to begin with. To use the tupperware model - you have to WASH THEM OUT before you use them, as you have no idea what was there before ... got it ? - this is called initialization. In a number exercise, you would go through systematically and place a ZERO in each part of FRED so:

for counter := 1 to 10 do
   fred[counter] := 0;

This is important - never assume the tupperware container is clean before you use it, there may be something nasty lurking there - clean it before you use it (in techo terms, the residual value may make your answers incorrect)

Averages from an Array

How do you calculate an average? ... add up all the scores and divide by the number added - right?. In a program, we need to use a variable to 'accumulate' the total in, before we do the division - I am going to call it BARNEY (it can be called anything, sensible like TOTAL or whatever).

Before we start adding, what is the total? - Zero right?

barney := 0;
for counter := 1 to 10 do
  barney := barney + fred[counter];
showmessage('Average ='+ inttostr(barney/10))

We start by setting BARNEY to 0, we then successively add each individual part of FRED to BARNEY (let barney now become what barney used to be plus that particular part of fred).

At the end of the loop, we take BARNEY (it now contains the cumulative total of all values from fred), and divide it by 10 and print it out.

Finding the Biggest in a numerical array

There are lots of ways of doing this. The simplest involves the use of a new variable - lets call it WILMA. WILMA will be used to hold the biggest number found so far, as we sequentially move through FRED, one part at a time.

To start off the process, we assume that the first part of FRED (fred[1]) is the biggest, and store that value in WILMA, so:

wilma := fred[1];

then, sequentially from the second part of FRED to the last, we look for a bigger value, when we find it, we place it's value in wilma, so:

for counter := 2 to 10 do
  if fred[counter] > wilma
     then wilma := fred[counter];

When we encounter a bigger number, WILMA gets it's value, if the number is not bigger, it is ignored and we move on. At the end of the loop, WILMA holds the biggest number.

Finding the smallest number is merely a variation on this... if you see what I mean?

Tossing 30 coins and store the number of heads in an array

hmmm .... not too difficult here:

for toss := 1 to 30 do
  begin

    coin := random(10);
    if coin <= 4
       then fred[1] := fred[1] + 1  {it was a head}
       else {it was a tail}
  end
Where TOSS is an integer or byte variable, as is COIN. I assume you have already defined FRED, and issued the randomize command

Where to from here

  • Establish a 100 part array of integers
  • intialize your array (put 0's in all the parts using a loop)
  • do a loop (the outside loop moves us from part to part) inside a loop (the inside loop tosses 100 times and records how many heads were encountered for this part)
  • search for and print the biggest number of heads (= the smallest number of tails) or visa versa
  • calculate the average number of heads/tails
  • relax - you've done it

some more information about arrays can be found here

mail me if there are things that are not clear - it takes a bit of getting your head around, but it will click, then appear easy

 

wonko@wonko.info
©Copyright t 1992..2018+. Edition 26.150117
wonkosite
Creative Commons License
This work is licensed under a
Creative Commons Attribution-NonCommercial-ShareAlike 2.1 Australia License
.