Wednesday, January 19, 2011

Basic Programming 101: Session II

All About Looping

Writing a computer program that calculates Factorial and prints the result tells you almost everything you'll ever need to know about programming computers. Factorial requires an if statement (should I stop at 0! or keep going and calculate N*(N-1)!), it requires an input statement (Factorial of what number) and an output statement (print the result).

This example calculates Factorial two separate ways. It's the same calculation and produces the same result, although each method uses a different technique: (1) for loop and (2) recursion.

For Loop

In this first part, we use a "for loop", which iterates over the numbers from 1 to N, multiplying them all together to get the answer N! = N * (N-1) * ... * 3 * 2 * 1.

10 input "Give me a number? ",a
20 gosub 2000
30 print "Looping: ";str$(a);"! = ";f
50 end
2000 f = 1
2010 for i = 1 to a
2020 f = f*i
2030 next i
2040 return

Line 20 (gosub 2000), jumps to 2000, sets 'f' equal to 1, and then line 2010 runs through all the numbers from 1 to 'a', finding their product and storing the value in 'f'. Line 2040 returns from the subroutine (back to line 30). Notice line 30 prints the resulting product, which is stored in 'f'.

The for loop is located on lines 2010, 2020 and 2030. Those lines do all of the work to compute Factorial.

Recursion

The second method to compute Factorial uses recursion. In this case, we take advantage of the fact that N! = N * (N-1)!. Rather than loop explicitly, we create a subroutine (in this case a special kind of subroutine known as a function) which calculates Factorial using itself.

3000 sub factorial(n)
3010 if (n < 1) then
3020 factorial = 1
3030 else
3040 factorial = n*factorial(n-1)
3050 endif
3060 end sub

Line 3000 starts the function by naming it ("factorial") and telling the computer it takes one variable ('n'). Line 3010 is the "if statement", it makes sure the recursion doesn't go on forever. In this case, we stop if 'n' is ever less than 1. If 'n' is not less than 1, line 3040 tells the computer to calculate the factorial of 'n' by multiplying n and the result of factorial(n-1); just like the mathematical definition. Line 3060 ends the subroutine. The value to be returned is stored in a variable 'factorial', the same name as the function.

One last item, the subroutine needs to be called:

40 print "Recursion: ";str$(a);"! = ";factorial(a)

Run the program to get the following:

Give me a number? 6
Looping: 6! = 720
Recursion: 6! = 720

Same result, two different methods, one using a for-loop and the other using a subroutine and recursion.

Wednesday, January 12, 2011

Basic Programming 101: Session I

Let's write two programs: sum two numbers and hello, world.

Summing two numbers in BASIC

10 input a
20 input b
30 print a+b

Run that and the computer asks for two numbers and prints their sum. However, it's not very informative because the "? " doesn't really tell you what the computer wants. Try adding the following line then run it:

5 print "Give me a number";

That's better, but it still isn't quite there. Try one more change:

20 input "Give me another number? ", b
30 print "The sum of your two numbers is " (a+b)

That's it. Two questions with explicit instructions and an informative answer. Here's the whole program (list):

5 print "Give me a number";
10 input a
20 input "Give me another number? ",b
30 print "The sum of your two numbers is "(a+b)

Save that and clear out the old program (del 1-), so we can write hello world.

Hello, world!

Run the following:

10 input "What is your name? ", name$
20 print "Hello, " name$

Notice how this program, like the other one, displays everything on the console.

Now, add these lines and run again:

30 greeting$ = "Hello, " + name$
50 graphics window 100,100,400,200
60 graphics 0
70 graphics moveto 10,100
80 graphics drawtext greeting$

It took a little more work, but this displays your text in a graphics window.

Finally, for the coup de grace, add this line and run it.

90 say greeting$

Remember to save your work!

Basic Programming 101: Prelude

A long time ago, more years than I care to remember, I learned to write my first program in BASIC, not Visual Basic, but real, honest to goodness, HP 3000 series BASIC. A few years later I graduated to a TRS-80 with 4K of memory and a lousy tape recorder for program storage that never worked. I had to write my programs from memory.

What a great environment! Easy to use, all of the principles of good software development were possible (and sometimes necessary).

Recently, I started teaching my son (13) how to program and figured I'd show him a more useful and modern language: Java. Ugh. He got it, but it takes forever. Try doing a proper coding for a program that takes in two numbers and prints their sum or the classic "hello, world!". Forget it. I started thinking about what might be the best programming language to start off kids (or perhaps anybody). C? Perl? Javasript? XHTML? Ruby (on rails)? Lisp?

What would you want your first experience with programming to feel like? The language should be powerful enough to do interesting things, yet simple enough that it doesn't get in the way of a beginner. It should fail informatively and be forgiving. It doesn't need to support a server farm or the support of a server farm. It needn't be able to run 3D vector calculations on a graphics card.

It ought to inspire confidence to go deeper.

Anyone who has coded for fun or for a living, that truly loves programming and can make computers sing or fly or run, knows how amazing the rush of coding a really great project feels. How to inspire that rush in kids? Airline pilots use flight simulators, little kids play with squishy balls and bats, small bikes have training wheels.

And beginning programmers use BASIC.

Check out Ron Nicholson's Chipmunk BASIC for the Mac. Great stuff. I'm using it for a beginning programming class for 7th and 8th graders.

Monday, January 3, 2011

Some Maze Programs

This one suggests how to generate on the screen and walk through it in real time:


This one generates the maze in a really cool fashion with doubly linked lists:


Can they be combined into one program to let the kiddies surf through a generated maze?

C++ for Web Apps

Great blog post by Steve Hanov explaining how to use C++ in your web application. He goes through a number of scenarios, refers to a small web server (Hibachi) that won a coding contest, and finally shows how he wrote his own web server to support the application.

Nice stuff.

Monday, March 29, 2010

Smart Meters Hacked (In Lab)

Great article on utility smart meters being hacked by a local SF security company. This highlights the problem with engineers and security; if an engineer has logged into a computer or opened a door with a key, that engineer feels qualified to design a security system. How hard can it be?

"I never imagined anyone would do that!"

Although I like to believe these failures are due to ignorance and not hubris, I find myself baffled that large companies don't know better.

Sure, some designers try to do a good job, but more often than not all they really do is sprinkle security pixie dust on the system and release it to the public.

The facts are: security professionals make careers out of protecting systems and hackers make careers out of attacking systems.

This stuff isn't that complicated, but it's more complicated than tossing a salad.

Thursday, October 1, 2009

Backing up your data

This is always a touchy subject. Most people, even long time technologists, do not bother backing up their data until far too late. Some years back, we learned the hard way how vital regular backups are when one of the disks failed on a home built PC. $2200 to a cleanroom later, we had all of that data back.

There are plenty of fantastic data backup programs and most large external drives come with backup software installed on the device. Mac OS and Windows deliver backup software with their operating systems bundled for free. Alternatively, you could buy an external backup server specific for your environment (computers) and run it on your internal network. Finally, you could choose an online backup company (fashionably: backing up to the cloud) and enjoy automatic, remote backups.

So, what is a consumer to do? The long and short of it is, I don't think there's one golden solution out there for everyone. Solutions depend upon your needs, how important your data is to you (and therefore how much you want to spend on backups), how technical you are or what you are willing to pay for setup, how paranoid you are and what level of risk you wish to tolerate.

There are no end of configurations and knobs you can twist when setting up these things. Doing backups? Should they be incremental, differential, full copy, replaced? What do those mean and why should you care?

What about a network storage (NAS) device for backups? Will it support Mac and Windows backups (Time Machine and Windows NTFS)? Do I have to purchase extra software to remote mount my NAS so that my other OS can "see" it? Also, how should you format your NAS's disks? RAID 0, 1, 5, 10, 01? Again, what do those mean and why do you care? Can the NAS disks be partitioned so that some backup data is only RAID 0?

Time Machine only works for the same computer; you need to dig deep and extract the files in a non-standard way if your computer is stolen and replaced (v. adding a replacement disk drive to the same computer). Are you backups working?

Will your chosen cloud computing backup provider go out of business and what happens to your data? Is it secure? Can you trust your financial data to be stored there? Is it encrypted? Who holds the encryption key(s)?

What is your offsite data backup plan? Do data copies go to local places or remote? What if an earthquake hits? Can your business recover? Will it matter?

It's enough to drive a sane person mad.

What's my backup plan?

For now, I use Time Machine from both of my computers. I keep a small number of files in Google Docs. All email is either POP (Yahoo) or IMAP (Google). All data in Parallels resides in Mac file hierarchy so that it can be backed up, except for Quicken data because Quicken does not play well in that configuration. However, Quicken backups go to Mac file hierarchy.

This is not enough, but it's a start.

What I would like:
A NAS that can handle Time Machine and Windows backups, RAID 10 (not 5), autobackup from NAS to the cloud, iTunes data not backed up or not backed up duplicate (as it resides on two computers and the NAS), photos backed up and available from NAS as a webserver to the world.

The cloud backup folks would want me to just use their services, but what if they go out of business? The NAS folks don't see the need for a cloud backup, but I don't want to be bothered with roll your own offsite backups/storage.

There's one NAS that almost fits the bill, but it's RAID 5 (no RAID 10): Synology Disk Station DS409+. It has almost all of these things, plus SVN (source control) and allows third-party application development. A most excellent offering. I'm concerned about the lack of RAID 10 as I'm one of those folks who thinks RAID 5 is not worth it. However, if you are going to do RAID 5, be sure to pick up disk drives from different batches, otherwise, they might all fail similarly and due similar problems during manufacturing.