Module 0: Introduction

  • Why study programming?
  • How can I succeed?
  • How do we solve problems with software?

Why study programming?

Computer programming gives us a helpful way of solving problems, not to mention a structured way of thinking about problems and solutions. Every engineer should be able to express solutions to certain classes of problems using algorithms (structured, step-by-step solutions to problems). Many first-year Engineering students think this is the last they’ll hear of programming, but Statistics begs to differ, as does Fluid Dynamics, as does seismic analysis… even effective use of spreadsheets is greatly helped by some fundamental understanding of programming. And, of course, good software designers are always in demand in the job market!

Even if you don’t write much software in your career, we live in a digital world that cannot be mastered without some understanding of the way computers work. Engineers use computers in lots of ways, whether we put computers into the systems we just use computer software to help us design things. Engineers are responsible for their use of computers: you can’t blame your tools for faulty design! “My software gave me the wrong answer” is no better an excuse than “my calculator is broken”, so we need to understand how to gain confidence that software is working correctly.

Finally, whether or not you currently believe it, I hope to show you this term that programming can be fun!

Dinosaur Comic about Sherlock Holmes and computers

How to succeed

Active engagement

A good way to underperform in this course is to come to lectures, passively consume them, only do the things you are explicitly told to do and let you lab partner do things without your understanding. The value you get out of this course will be proportional to the effort you put in to explore and understand the material. Specifically, it is helpful to:

Ask questions
In class or labs, in Office Hour or the Help Centre, with friends or with me, via Top Hat or in D2L, if you don’t understand something, ask.
Try things out
When I show you some example code and explain how it works, don’t take my word for it: try it yourself! Try running the same code with different inputs to see if you can predict how it will react. Try finding different solutions to the same problem or to slightly different problems to gauge your own understanding of the material.

Practice, practice, practice!

Software design and development, like other kinds of Engineering design, blends both science and art. I will teach you things about designing software-based solutions to problems, but you can’t really say you’ve learned design unless you practice designing. This is much like learning to play a musical instrument, you need to learn some theory, but you also need to practice. The Tools page provides details on how to get set up with software tools for writing, compiling and testing C++ code. Please take a look at this page and make sure that you are comfortable with at least one of these tools during the first week of lectures.

Solving problems with software

Like every other engineering discipline, software engineering is about designing solutions to complex problems. When problems get complex, one valuable strategy employed by engineers is divide and conquer. Designing a modern aircraft is a complex problem, too complex for one person to do by themselves, but if one team can design the engines, another team can design the wings, another team the avionics, etc., then we might just be able to get somewhere! Of course, each of these individual components is themselves a complex system that needs to be broken into smaller design tasks, which themselves might be broken down again and so on until we arrive at manageable-sized problems, things that an individual engineer or team of engineers can design appropriate solutions to.

So how about software?

We use software to address incredibly complex problems, from building self-driving cars (or self-driving submarines, as some folks do here at Memorial) to training ship’s crews to securing networks against hackers (as in my own research). In fact, computer programs are among the most complex artifacts ever built. Consider that:

  • a 700 page novel occupies 2.6 MB of disk space,
  • Windows 7 takes about 2.5 GB and
  • Eclipse is about 18 million lines of code.

How do we design solutions to these problems? In the same way as the other disciplines we described: by breaking problems down into parts until the parts are small enough to solve. Once we have small modules that address particular aspects of the problem, we can put them together into a hierarchy of ever-more-complex modules to build up the whole system. This requires an understanding of the problems themselves, but it also requires an understanding of our basic building blocks, i.e., how computers work at the lowest levels.

How do computers work?

Computer structure

We can think of a computer as containing three major functional parts:

  1. a central processing unit (CPU) that executes instructions, one at a time,
  2. some memory (often called Random Access Memory or RAM) that stores information and
  3. some input/output (I/O) devices that allow the CPU to communicate with a user, disks and other computers.

These three functional components allow the computer to compute, to remember and to communicate. There are lots of other things inside of a modern computer, but they all exist to serve and facilitate these main tasks.

CPUs work one step at a time

The CPU is the brain of the computer, and we will spend most of our time thinking about how it does its work (later courses in programming, microprocessors and data structures spend more time on these). The key thing to keep in mind is that a CPU follows instructions, and it does this one step at a time. Such a set of step-by-step instructions for accomplishing a task is called an algorithm. Here’s an example of an algorithm that tests whether a number $x$ is even:

  1. Let $y = \frac{x}{2}$.
  2. If $2y = x$, $x$ is even.

Here is another, slightly more sophisticated algorithm (from the Great British Bake-off’s Mary Barry):

The Great British Bake-off
  1. Preheat the oven to 180C/350F/Gas 4.
  2. Grease and line 2 x 20cm/8in sandwich tins: use a piece of baking or silicone paper to rub a little baking spread or butter around the inside of the tins until the sides and base are lightly coated. Line the bottom of the tins with a circle of baking or silicone paper (to do this, draw around the base of the tin onto the paper and cut out).
  3. Break the eggs into a large mixing bowl, then add the sugar, flour, baking powder and baking spread.
  4. Mix everything together until well combined. The easiest way to do this is with an electric hand mixer, but you can use a wooden spoon. Put a damp cloth under your bowl when you’re mixing to stop it moving around. Be careful not to over-mix – as soon as everything is blended you should stop. The finished mixture should be of a soft ‘dropping’ consistency – it should fall off a spoon easily.
  5. Divide the mixture evenly between the tins: this doesn’t need to be exact, but you can weigh the filled tins if you want to check. Use a spatula to remove all of the mixture from the bowl and gently smooth the surface of the cakes.
  6. Place the tins on the middle shelf of the oven and bake for 25 minutes. Don’t be tempted to open the door while they’re cooking, but after 20 minutes do look through the door to check them.
  7. The cakes are done when they’re golden-brown and coming away from the edge of the tins. Press them gently to check – they should be springy to the touch. Remove them from the oven and set aside to cool in their tins for five minutes. Then run a palette or rounded butter knife around the inside edge of the tin and carefully turn the cakes out onto a cooling rack.
  8. To take your cakes out of the tins without leaving a wire rack mark on the top, put the clean tea towel over the tin, put your hand onto the tea towel and turn the tin upside-down. The cake should come out onto your hand and the tea towel – then you can turn it from your hand onto the wire rack.
  9. Set aside to cool completely.
  10. To assemble the cake, place one cake upside down onto a plate and spread it with plenty of jam. If you want to, you can spread over whipped cream too.
  11. Top with the second cake, top-side up. Sprinkle over the caster sugar.

What is software?

Software, whether written in C++, Java, Python or another programming language, is a way of describing algorithms to a computer. Consider the following implementations of the algorithm from above that checks whether or not a number is even. Here’s one implementation written in the Python programming language:

y = x / 2
if (2 * y == x):
        print(x, 'is even!')

Here is the same algorithm implemented in C++:

int y = x;
if (2 * y == x)
        cout << x << " is even!" << endl;

We will spend most of our time in this course using C++. You should note that these two code snippets look a bit different, as the details of the two programming languages are different, but the fundamentals of algorithms are the same.

Computers everywhere!

Computer structure

To the right is a block diagram of the 3pi robot that you will use in the lab: This robot, although very different from a “normal” desktop or laptop computer, has the same key components: a CPU, some memory and some I/O devices. Instead of a mouse, keyboard or hard drive, however, the 3pi robot has sensors, a little LCD display, some buttons and two motor controllers to make the 3pi drive around.

In fact, the computer in the lab, the computer on your 3pi robot, the computer that monitors sensors in a car and the computer that makes a smart watch work all have this same structure. What’s different about them is the power of their CPUs, the quantity of their memories and the kind of I/O devices they have.

License: CC BY-NC-SA

(c) 2009–2016 Michael Bruce-Lockhart, Theo Norvell, Dennis Peters and Jonathan Anderson. Licensed under a Creative Commons Attribution–Noncommercial–Share-Alike 2.5 Canada License. Permissions beyond the scope of this license may be available at theteachingmachine.org.