Skip to content

Airspeed Velocity

On your journey, you encountered a strange person who mentioned something about coconuts and queries you vigorously about the airspeed velocity of unladen swallows.

To answer this age-old question, you can mock up the following program to calculate the airspeed velocity of these important birds. With a quick search, you find an equation based on the Strouhal Number and some details on swallows. We can use this information to create a program that can calculate the airspeed velocity of African and European Swallows.

From these articles we can determine that the airspeed velocity of a swallow can be calculated using Speed = Frequency * Amplitude / Strouhal. Code this into a program and then calculate and display the airspeed for different wing values.

Some values that will help with this are:

  • We can use two set Strouhal values: an efficient Strouhal of 0.20 and low efficiency Strouhal of 0.40.
  • African Swallow: frequency 15hz, amplitude 21cm.
  • European Swallow: frequency 14hz, amplitude 22cm.

The Algorithm

My plan for this program involves the following steps:

  1. Show the program details at the start.
  2. Read in the name, frequency, and amplitude.
  3. Calculate min and max speeds.
  4. Output results.

When the program starts, we can output a few messages to let the user know what the program does. This can be a sequence of calls to WriteLine.

Reading in the bird’s name will be relatively straightforward. We can use Write to display a prompt asking the user to enter a bird name, and then use ReadLine to get a value we then store in a variable.

With frequency and amplitude, we will require the extra step to convert the user’s input to a number. These will need to be double values, and any loss of precision will not cause issues. To do the conversion we can call ToDouble from C#‘s System.Convert.

To calculate the min and max speeds we can use the equation that we noted above, using variables to store the results. These can then be used in the “output results” step, where we can call WriteLine to output the calculated details.

The Building Blocks

Reviewing this plan, we can identify which constants and variables we will need to create within our program. We have a few set values, and some values that we want to be able to be different each time the program runs. We can create the following building blocks to help us work with this into our program:

  • Two constants:
    • STROUHAL_LOW_EFFICIENCY to store 0.4.
    • STROUHAL_HIGH_EFFICIENCY to store 0.2.
  • Six variables:
    • Two strings:
      • birdName to capture the name of the bird the user enters.
      • A line to store the numbers they enter until we can convert them to a double.
    • Four double values:
      • freq to store the frequency of the bird’s wing stroke in beats per second.
      • amp to store the amplitude in meters.
      • resultMax and resultMin to store the two speed calculations based on the low and high efficiency Strouhal values.

Pseudocode

Using all of this information, and our selected building blocks, we might write the following pseudocode for our program:

Declare constants:
- STROUHAL_LOW_EFFICIENCY = 0.4
- STROUHAL_HIGH_EFFICIENCY = 0.2
Declare variables:
- birdName, line to store string data
- freq, amp to store double data
Display program details
Read in the name of the bird from the user
Read the frequency and amplitude - converting from string to double
Declare variables:
- resultMax, resultMin to store double data
Calculate the airspeed, given the fixed Strouhal values
resultMax = freq * amp / STROUHAL_HIGH_EFFICIENCY
resultMin = freq * amp / STROUHAL_LOW_EFFICIENCY
Output the bird's name, and its min and max airspeed

Building and Running

Build and run this in small steps:

  • Read in the bird’s name and output it back. Build and run.
  • Add reading in frequency, and add it to the output. Build and run.
  • Duplicate the frequency code and change it to read and output the amplitude. Build and run.
  • Add the calculation for max speed, update output, then build and run.
  • Test the values work as expected.
  • Duplicate for min speed, update output, then build and run.
  • Test that it all works.
  • Celebrate!

Wrap Up

Think about the algorithm, identify any things you need in the code (variables and constants), then code by building out the plan little by little. As you write more programs you will get very used to this process.