User Tools

Site Tools


start

This is an old revision of the document!


Multiple Choices Algorithm - by Mark Kelly

Another in the Fun with Algorithms series

Stuff Pty Ltd sells stuff. The unit cost of the stuff varies according to the quantity ordered. Customers can order any number of items. Stuff Pty Ltd use this scale: 1-9 items = $100 each 10-19 items = $97 each 20-29 items = $92 each 30-39 items = $88 each 40-49 items = $85 each 50 or more = $82 each

Task: using pseudocode, create an algorithm to calculate the unit cost of the items at different quantities. Then calculate a total cost with 10% GST added. Convert the pseudocode into code. Create test data to fully test all aspects of the solution’s functionality. Note: Input data need not be validated, and the interface can be very basic.   Sample VB2010 solution Public Class Form1

  ' Multiple options algorithm
  ' by M.Kelly 10 May 2011
  ' Version 1.0
  ' To Do Next: nil
  Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
      Dim Qty As Integer = CInt(txtQty.Text)
      Dim inctax As Single = 0  'declare and initialise in one hit
      ' deliberately lacks validation of qty
      Select Case Qty
          Case 1 To 9
              lblCostper.Text = 100
          Case 10 To 19
              lblCostper.Text = 97
          Case 20 To 29
              lblCostper.Text = 92
          Case 30 To 39
              lblCostper.Text = 88
          Case 40 To 49
              lblCostper.Text = 85
          Case Else
              lblCostper.Text = 82
      End Select
      lblSubtotal.Text = Qty * CInt(lblCostper.Text)  'using labels for output
      'inctax is here as a sample use of variables for calculations and 
      'using labels just for display purposes.
      'Using variables mean less converting back & forth between text and number.
      inctax = CInt(lblSubtotal.Text) * 1.1           'add GST  
      'The final total is nicely formatted currency just to show how it's done.
      'Nice formatting is not needed in U3O2.
      lblIncTax.Text = Format(inctax, "$#,####.#0")
  End Sub
  
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      End
  End Sub

End Class


Sample output Perhaps more decorative that it needs to be for U3O2, but I can’t abide messy interfaces.

<can't insert image!>

:-(


Fun with Algorithms - Mark Kelly

Use flow charts or N-S charts to elegantly solve the following algorithms. Some are harder than others… Try actually coding it after doing the brainwork. – Round any decimal number UP to the next highest multiple of 0.25. – Convert any Roman number to decimal (e.g. MCMLXIX to 1969) - and/or vice versa – Convert any binary number to decimal. – Find the lowest (or greatest) common denominator of 2 integers (or report that there is no LCD or GCD). – List prime numbers up to 100 – Simulate the display of a single-digit liquid crystal display Assume there are 7 LCD bars like this _

_
_

Feed it any numeral or alphabetic character in hexadecimal range (0 to F) and it should turn on the right bars. (Efficiency is the key requirement here!) – Given a price including 10% GST, give the ex-tax price and the tax component, e.g. $11 inc = $10 ex + $1 GST. – Create a virtual deck of cards encoded as numbers 1 to 52. Given any number from 1 to 52, the algorithm should return the suit and rank represented by the card. – Create an algorithm to shuffle the virtual deck of cards – Create an algorithm to play naughts and crosses - it should be unbeatable if the algorithm plays first. – Given an number of dollars, calculate what notes or coins need to be given as change from $100. e.g. $67 = 1 x $20, 1 x $10, 1 x $2, 1 x $1. – An electrical company is developing a new main-powered sensorlight with the following requirements: • It turns on its LED light for a predetermined time (e.g. 60 seconds) after sensing movement with its passive infrared (PIR) detector. • It does not turn the light on if its light detector senses that the room is already well illuminated (e.g. during the day) • While its light is on, any further movement detection resets the timer back to its starting value and the countdown starts again. Convert your algorithm into a working simulation of the light. You will need to simulate: • The day/night sensor (e.g. with radio buttons) • The movement sensor (e.g. with a mouse click or keypress) • The LED light (e.g. with a shape changing its colour) Show the value of the countdown time onscreen to verify its behaviour.


start.1305026873.txt.gz · Last modified: 2011/05/10 11:27 by kel