Sunday 17 June 2012

My first computer program

I've just read Bill Gates's Wikipedia entry and was interested by the fact that he wrote his first computer program at the age of 13 or 14, to play tic-tac-toe (noughts and crosses to us Brits).  This reminded me that I have been meaning for years to write up my first computer programming experience, in 1975 at the age of 15 or 16, when I was a sixth-former at RGS Guildford (then a state grammar school).  The computing facility available to us was an ICL 1900 series machine, which seemed to take up most of the fourth floor of a modern building at the local technical college.  To begin with, our method of entering code was by filling in forms, which staff at the Tech would transfer onto punched cards.  We would submit our lines of FORTRAN IV code, and a few days later receive a printout, usually with compilation (syntax) errors, and eventually, after some corrections, with the output from our program.  Later, we would punch our own cards using entirely mechanical punches, for which we needed to learn the two or three-hole codes for each of the ASCII characters.

As a reasonably bright youngster in the early days of school computing, my idea for a first program was quite ambitious.  I had started to learn the guitar, so I thought it would be cool to do some computer-generated music.  I made a list of guitar chords and drew up a two-dimensional table of conditional probabilities for one chord followed by another.  So for example, a D7 might have a 60% chance of being followed by a G, a 20% chance by a C and a 20% chance by an A7.  For a G, it might have been 30% C, 20% E minor, 20% D7, 20% A minor and 10% B minor.  The program would run in a loop, starting with a particular chord in the set, and generating subsequent chords randomly according to the probability table.  I was fascinated by the idea of changing this table and seeing whether the music that came out would have a different "feel".  Reasonably advanced stuff in those days (or so I like to think)!

This project immediately set me apart as an awkward customer, because I needed a random number generator.  I can't remember how I did that in the end, but I don't think it was a built-in function.  What I do remember, with pride and embarrassment in equal measure, is the results of my efforts.  Pride because, of the ten or so of us, my printout was the only one that came back first time without any compilation errors.  But embarrassment because there didn't seem to be any output at all.  My teacher and I thought this must be a one-off problem, so we re-submitted  the code with no changes (we didn't think of inserting "hello world" print statements or anything like that).  Back it came the following week, same problem.  Nothing!  When the same thing happened the third time I noticed that the printouts did have something on them: a black rectangular blob in the top left corner.  Here is the explanation:  I had chosen to output my chords as single-digit numbers (dealing with characters and strings seemed too hard for a first program).  A chord would be printed each time a print statement was encountered in my loop.  Normally, that would produce a new chord on each line.  But I wanted to save paper and print the chords across the page.  I had been told that the "+" control character would make the next piece of output appear on the same line.  But what I had not appreciated was that there was an implicit carriage return as well.  So the 50 chords I asked for were indeed printed - all on top of each other!  The following week I had my first readable output and I remember playing my first piece of computer-composed music - which has been lost to posterity.

My second project was much more ambitious, and never properly completed.  It just had to be computer chess.  That was a tall order in those days.  I just about managed to write a program to play legal chess, but did not make much progress on getting it to play well!