There are a number of programming techniques which the experienced coder takes for granted, but which aren't necessarily intuitive for the newcomer. Call them "design techniques" or "design patterns" that need to be learned, if you want some fashionable buzzwords.
• To produce the sum of a stream / flow / collection of values, initialise a variable to zero, loop through each of the values adding it into that variable, and when you reach the end of the loop the variable contains the total.
• To produce the average of a stream / flow / collection of values, write a loop just as you did to calculate the total. In a addition, start a second variable (a counter) at 0 and add 1 to each each time to add a value to your running total variable. When you have completed the loop, divide the total by the number of values that made up that total.
If you are counting integers (whole numbers), remember that your average might not be a whole number and so you'll need to convert to a real / floating point number to get an accurate average.
• To find the smallest of a stream / flow / collection of values, write a loop to go through all of those values. The
first time through the loop, set a variable which is the minimum so far to the first value. On
subsequent times through the loop, compare the minimum value (so far) with the incoming value, and save the incoming value as the minimum value (so far) if it's the smaller. When you reach the end of the loop, you have the minimum value.
• To find the largest of a stream / flow / collection, use the same principle as you used for the minimum, but check to see if the incoming value is great and if it is, save it as the maximum so far.
Newcomers are often tempted to start loops to find the maximum value at zero to avoid the first time check in the loop.
Don't do that!. If you have a file of the temperatures in Spitzbergen, where it never gets above zero in winter, you'll end up with a result that tells you the maximum was zero, rather than the chilly -5.4 which happened to be the least negative of all the numbers in the incoming data.
There's an example of each of these algorithms, implemented within the same program in Ruby
[here] on our web site - from yesterday's
Learning to Program in Ruby class.
(written 2012-02-22, updated 2012-03-03)
Associated topics are indexed under
Q110 - Object Orientation and General technical topics - Programming Algorithms [3662] Finding all the unique lines in a file, using Python or Perl - (2012-03-20)
[3451] Why would you want to use a Perl hash? - (2011-09-20)
[3102] AND and OR operators - what is the difference between logical and bitwise varieties? - (2010-12-24)
[3093] How many toilet rolls - hotel inventory and useage - (2010-12-18)
[3072] Finding elements common to many lists / arrays - (2010-11-26)
[3042] Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in Perl - (2010-11-11)
[2993] Arrays v Lists - what is the difference, why use one or the other - (2010-10-10)
[2951] Lots of way of converting 3 letter month abbreviations to numbers - (2010-09-10)
[2894] Sorting people by their names - (2010-07-29)
[2617] Comparing floating point numbers - a word of caution and a solution - (2010-02-01)
[2586] And and Or illustrated by locks - (2010-01-17)
[2509] A life lesson from the accuracy of numbers in Excel and Lua - (2009-11-21)
[2259] Grouping rows for a summary report - MySQL and PHP - (2009-06-27)
[2189] Matching disparate referencing systems (MediaWiki, PHP, also Tcl) - (2009-05-19)
[1949] Nuclear Physics comes to our web site - (2008-12-17)
[1840] Validating Credit Card Numbers - (2008-10-14)
[1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
[1187] Updating a page strictly every minute (PHP, Perl) - (2007-05-14)
[1157] Speed Networking - a great evening and how we arranged it - (2007-04-21)
[642] How similar are two words - (2006-03-11)
[227] Bellringing and Programming and Objects and Perl - (2005-02-25)
[202] Searching for numbers - (2005-02-04)
R104 - Ruby - Control Structures [3619] Ruby v Perl - a comparison example - (2012-02-21)
[3422] Assigning values to variables within other statements - Ruby - (2011-09-07)
[3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
[3254] Multiple inputs, multiple out, ruby functions - (2011-04-19)
[3253] Is this number between? Does this list include? - Ruby - (2011-04-18)
[3200] How a for loop works Java, Perl and other languages - (2011-03-12)
[3159] Returning multiple values from a function call in various languages - a comparison - (2011-02-06)
[3158] Ruby training - some fresh examples for string handling applications - (2011-02-05)
[3156] Splitting data reading code from data processing code - Ruby - (2011-02-04)
[2975] Why do I need brackets in Ruby ... or Perl, Python, C or Java - (2010-09-29)
[2892] Alternative loops and conditionals in Ruby and Perl - (2010-07-28)
[2711] For loop - checked once, or evety time? Ruby v Perl comparison and contrast - (2010-04-07)
[2619] Passing code to procedures and yield in Ruby - (2010-02-02)
[2471] A short form of if ... then ... else - (2009-10-23)
[2287] Learning to program in Ruby - examples of the programming basics - (2009-07-15)
[1904] Ruby, Perl, Linux, MySQL - some training notes - (2008-11-23)
[1891] Ruby to access web services - (2008-11-16)
[1887] Ruby Programming Course - Saturday and Sunday - (2008-11-16)
[1870] What to do with a huge crop of apples - (2008-11-04)
[1738] Clean code, jump free (Example in Lua) - (2008-08-06)
[1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
[1587] Some Ruby programming examples from our course - (2008-03-21)
[1582] Ruby, C, Java and more - getting out of loops - (2008-03-19)
[1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
[1163] A better alternative to cutting and pasting code - (2007-04-26)
[995] Ruby's case - no break - (2006-12-17)
[985] Equality in Ruby - == eql? and equal? - (2006-12-14)
[962] Breaking a loop - Ruby and other languages - (2006-12-03)
[960] 1st, 2nd, 3rd revisited in Ruby - (2006-12-02)
Some other Articles
Why do we need a Model, View, Controller architecture?Some TestWise examples - helping use Ruby code to check your web site operationLoading Ruby classes - where does Ruby look?Matching regular expressions, and substitutions, in RubyFinding the total, average, minimum and maximum in a programlists and struct::list in Tcl - Introduction to struct::list and examplesThe fileutil package and a list of file system commands in TclBus top - colours of LondonHistoric documents for Wiltshire