e-genting programming competition 2003 public lecture by jonathan searcy 10 january 2004

47
E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Upload: cassandra-lynch

Post on 31-Dec-2015

224 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

E-Genting Programming Competition 2003

Public Lecture by Jonathan Searcy

10 January 2004

Page 2: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Competition Questions

No. Title and Description Marks

1. MicrobankA commercial reporting program with performance requirements

200

2. M44A problem in data structures and algorithms and spatial geometry

400

3. Windscreen WipersA state machine to drive a stepping motor

400

4. 25 FactorialA problem in extended precision arithmetic

100

Page 3: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

M44

Page 4: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

A Near-Newtonian Experience

Page 5: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Azimuth-Elevation Co-Ordinate System

Page 6: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Sectors

Page 7: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Restricting the Extent of the Search

Page 8: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Multiple Sectors may need to be Searched

Page 9: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Azimuth is a Cyclic Domain

Page 10: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Bucket Data Structure

Page 11: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

A Common Mistake

Page 12: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

No HashTable Zone

Page 13: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Profile Data Type

Page 14: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

How will the Profile Data Type be Used

UsageCentroid, height and

width

Bottom-left, height and

width

Bottom-leftand

top-right

Load from input data

Trivial Easy Easy

Generate sector set

Very messy Messy Clean

Test for intersection

Very messy Messy Clean

Page 15: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Domains of the Profile Components

left ≤ x(pointInProfile) < right

bottom ≤ y(pointInProfile) < top

0° ≤ left < 360°

left ≤ right < left+5.73°

-45° ≤ bottom < +45°

bottom ≤ top < +45°

Page 16: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

The Test for Intersection

In a non-cyclic domain, if a profile is not:

above the other profile; or

below the other profile; or

to the left of the other profile; or

to the right of the other profile

then it intersects the other profile

Page 17: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

But Azimuth is a Cyclic Domain

Page 18: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

The Expression for Intersection in the Cyclic Domain

If:

! NonCyclicIntersect (profile, otherProfile) and

! NonCyclicIntersect (profile, otherProfile + 360°) and

! NonCyclicIntersect (profile + 360°, otherProfile)

then profile does not intersect otherProfile in the cyclic domain.

Page 19: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Sector Set Data Type

Page 20: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

How will the Sector Set be Used?

UsageBottom-left,

height and widthBottom-left and

top-right

Load from profile

Slightly harderAs easy as it

gets

Expansion into sectors

Not too hardSomewhat

tricky

Page 21: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Converting a Profile into aSector Set

sectorSet.left = (int)(profile.left / SECTOR_WIDTH);

right = (int)(profile.right / SECTOR_WIDTH);

if (right * SECTOR_WIDTH != profile.right) right ++ ;

sectorSet.width = right – sectorSet.left;

Page 22: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Integer Divide Truncates towards Zero

Page 23: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Vertical Sector Set Range

 sectorSet.bottom = (int)((profile.bottom + 45º)

/ SECTOR_HEIGHT);

top = (int)((profile.top + 45º)

/ SECTOR_HEIGHT);

if (top * SECTOR_HEIGHT != profile.top) top++;

sectorSet.height = top – sectorSet.bottom;

Page 24: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Expanding a Sector Set

for (i = 0; i < sectorSet.width; i++) {

x = (sectorSet.left + i) % NO_OF_HORIZONTAL_SECTORS;

for (j = 0; j < sectorSet.height; j++) {

y = sectorSet.bottom + j;

// Insert friend into bucket identified by

// (x,y) or search bucket (x,y) for possible

// trajectory intersections.

}

}

Page 25: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Designing the Bucket Data Structure

Page 26: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Results of a Trial Run

Number of sectorsSearch time per

trajectory(μs, 500MHz Pentium)

Horizontal Vertical

1 1 18,000

90 22 25

180 45 10

360 90 6

720 180 5

Page 27: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Summary• Use diagrams to represent spatial problems;• Use a stylised entity relationship diagram to visualise a data

structure;• Use data flow diagrams to identify processes and data movements;• Engage brain before plugging in packages;• Tabulate design alternatives to choose the best;• Choose the data structure that will be easiest to use;• If you can’t conceptualise a logical function, consider it’s

complement;• Use a diagram to visualise intersection in a cyclic domain;• Integer truncation rounds towards zero;• Use simple data structures – arrays and linked lists.

Page 28: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

25-Factorial

Write a program to calculate and display the exact decimal value of 25-factorial (i.e. 25 * 24 * 23 * ... * 3 * 2) without the aid of any language or operating system supplied extended-precision arithmetic functions.

Page 29: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Shift and Add 610 x 1010

= 01102 x 10102

= 00102 x 10102 = 10100 + 01002 x 10102 = 101000

= 1111002

= 6010

  0110 1010 ← shift register ↑.......+ RESULT ← result register

If bit is set, add the shift register to the result register. Shift the pointer and shift register one bit

to the left and repeat.

Page 30: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

The Complication

x = 1;for (i = 2; i <= 25; i++) x *= i; do { push (x % 10); x /= 10;} while (x != 0);while (pop (&c)) putchar(c + '0');

Page 31: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Decimal Register

0...9 0...9 0...9 0...9

Page 32: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Multiplying by a Digit 1281 x 7 = 1 x 7 = 7 + 8 x 7 = 56 + 2 x 7 = 14 + 1 x 7 = 7 = 8967  carry = 0; r[3]=1, r[2]=2, r[1]=8, r[0]=1; for (i = 0; i < 4; i++) { x = r[i] * 7 + carry; r[i] = x % 10; carry = x / 10; }

Page 33: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Multiplying by a Two-Digit Number

bigNumber * (d[1],d[0]) =

bigNumber * d[0]

+ bigNumber * d[1] * 10

Page 34: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

The Simple Answer char r[PRECISION]; short i, j, x, carry;  memset (r, 0, PRECISION); r[0] = 1; for (i = 2; i <= 25; i++) { carry = 0; for (j = 0; j < PRECISION; j++) { x = r[j] * i + carry; r[j] = x % 10; carry = x / 10; }

}

Page 35: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Summary

• Theory of shift and add;

• Store data in the form that is easiest to use;

• Decimal string multiplication;

• Progressive simplification.

Page 36: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Windscreen Wipers

Page 37: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Step Table

For clockwise rotation

SCR VALUEFor anti-

clockwise rotationD C B A

0 0 0 1

0 1 0 0

0 0 1 0

1 0 0 0

Page 38: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

The Required Motion

Page 39: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Acceleration and Deceleration

Inter-Step Time (milliseconds)

42 17 13 11 10 9 8 8 7 7 6 6 6 6 6 5

Page 40: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Operating System Interface

class WiperControl {//...public void setSCR (int scrVal);public boolean getSensorState();//...

}interface WiperInterface {

void initialise (WiperControl control);void startWiping ();void stopWiping ();void tick ();

}

Page 41: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Synchronising the Start/Stop Instructions

Page 42: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Top-to-Bottom Pseudo-Code-- UNINITIATED stateSCR = 0phase = 0if sensor is off:

SCR = STEP_TABLE[ phase ];stepsToEnd = 20while stepsToEnd != 0

wait for 42 ticks -- RESETTING stateif sensor is off

stepsToEnd = 20else

stepsToEnd --phase = (phase + 4 – 1) % 4SCR = STEP_TABLE[ phase ]

end whileend if

enter normal operating mode

Page 43: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Uninitiated State TransitionsEvent Processing

initiate SCR = 0phase = 0if sensor is off:

SCR = STEP_TABLE[ phase ];stepsToEnd = 20waitRemaining = 42state = RESETTING

elseenter normal operating

modeend if.

Page 44: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Resetting State TransitionsEvent Processing

tick decrement waitRemainingif waitRemaining == 0

if sensor is offstepsToEnd = 20

elsedecrement stepsToEnd

phase = (phase + 4 – 1) % 4SCR = STEP_TABLE[ phase ]if stepsToEnd != 0

waitRemaining = 42;else

enter normal operating modeend if

end if.

Page 45: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Re-synchronising the Theoretical Wiper Location

The question paper required the program to:

• decelerate the blades so that they stop 180 steps after the sensor switches off;

• reverse the direction of the motor, accelerate the blades, adjust the theoretical location of the blades when the sensor switches on...

Page 46: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Making Use of Tables

static final int STEP_TABLE[] = {1, 4, 2, 8};// Step sequence table

static final int NEXT_PHASE[][] ={{1, 2, 3, 0}, {3, 0, 1, 2}};

// Next phase map // To move one step to the right:phase = NEXT_PHASE[0][phase];wiperControl.setSCR(STEP_TABLE[phase]); // To move one step to the left:phase = NEXT_PHASE[1][phase];wiperControl.setSCR(STEP_TABLE[phase]);

Page 47: E-Genting Programming Competition 2003 Public Lecture by Jonathan Searcy 10 January 2004

Summary

• Transfer asynchronously changing data from one process to another in global or class instance memory;

• Convert the top-to-bottom sequence into an event-driven finite state machine;

• Correctly program the variable arc length.

• Make use of tables.