Learning to program – throw yourself in at the deep end

If you have a yearning to learn how to program, then the best way to go about it may be the “deep end” of the pool approach. What I mean by this is to just try and code a program. Pick a problem you know something about (i.e. you know what the result will be), and design a program. Of course you’ll need a language to code in – and I would strongly suggest Julia.

The first thing to understand about programs is that they take data and turn it into information. For example suppose we wanted to write a program to calculate the amount of interest earned on a sum of money. We would need a number of pieces of data:

  • the original sum of money, or principal (P)
  • the interest rate (r)
  • the compounding period of time (t)

Then we need a formula to calculate the interest earned, I.. The simplest formula is:

I = P r t

So, if P=$1000.0, r=3.7%, and t=4 years, then the calculation can be performed in the following manner:

I = 1000.0 × 0.037 × 4
I = 148.0

These pieces of data are all numbers that have a fractional part. For example the interest rate, r, is 3.7%, or 0.037. The choice of data has an impact on the type of data used in a program, effecting the amount of memory used, and how precise calculations will be. A Julia program to perform this calculation might look like this:

P = 1000.0
r = 0.037
t = 4.0
I = P * r * t
println("Interest = ", I)

The program is very simple, and achieves exactly what it needs to. Of course it can only calculate one value for the interest, because P, r, and t are static. Next, maybe add a means of allowing the user to input values, making the program more generic.

println("Enter principal amount: ")
P = parse(chomp(readline()))

println("Enter interest rate (0-100): ")
r = parse(chomp(readline()))
r = r / 100.0

println("Enter period of time: ")
t = parse(chomp(readline()))

I = P * r * t 
println("Interest = ", I)

This program is longer, but allows each of the values to be input by the user. At this stage it doesn’t matter that you don’t completely understand the parse(chomp(readline())) code. Just know that is reads in a number. Now the program works, it can be used as the basis for building other similar programs.

The truth is that for many people there may be no systematic way of learning to program. I learned through trial-and-error, and honestly it’s not a terrible way to learn.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s