Skip to the content of the web site.

Lesson 1.1: Hello world!

Previous lesson Next lesson


What is a program

A program is a sequence of instructions that manipulates data to produce an appropriate output. For example, the first program we will look at takes the string of characters Hello world! and displays this on the console. Another program may perform some form of mathematical calculation.

In all these cases, there must be information, or data, to manipulate. Some of this data can be hard-coded into the program instructions, and the result is displayed to a computer monitor.

Alternatively, data can also be entered via a keyboard.

Alternatively, data can be stored in text files on some form of external storage, be that a hard-disk drive (HDD) or some form of solid-state drive (SSD). The program could either read data from or write data to files on those media.

In general, there are many different means by which a program can either acquire to process or send processed data. We can generalize such devices through one of four descriptions:

Input devices
This includes keyboards, mice, microphones, image scanners, cameras, and sensors.
External storage
This includes hard-disk, solid-state, floppy, optical and tape drives, as well as the Cloud.
Communication with other devices
Via an expansion bus, the Internet, Bluetooth or a CAN bus.
Output
Monitors (screens), printers, speakers, and actuators.

In today's world, however, programs can also communicate with, for example, cameras and they keyboard, and data can be received from monitors and printers. Consequently, it is perhaps easiest to think of a program as a sequence of instructions that processed data which may be read from or written to various devices:

Now that we understand what a program is, but some of you, especially those of you who are in the electrical engineering program, may be wondering why you are even taking this course. On your own time, you could speak to any 4th-year student in electrical engineering; however, for now you can also read the following rationale at your convenience.

Program execution

When you execute (or open or run) an application or program either on your desktop computer, laptop or smart phone, the computer begins to perform operations behind the scene. The operations that a computer can perform include storing, accessing and manipulating (or processing) information, or data (specifically, numbers). At some point, however, for this to happen, a developer (or programmer) had to specify what operations the computer should perform.

The problem with a computer is that it "speaks" in a binary numbering system (that is, processing using only zeros and ones) and while some people have learned such languages (indeed, someone had to design such languages), the dedication required exceeds the efforts most humans are willing to put towards such an endeavour. We call this language of the computer machine language and the machine language of one processor like the Intel Core i7 is different from that of another processor like the ARM Cortex M3.

Definition: binary numbering system
Counting and performing other instructions using only zeros and ones—more on this later. This is often simply abbreviated to just binary; however, the word "binary" is often used as an adjective to describe two parts or components.

Definition: machine language
The binary instructions that a computer understands and allows the computer to carry out tasks specified by the programmer.

You will learn more about machine language in your course on digital computers.

There is a human-readable version of machine instructions that is called assembly language; however, you will also see this in your course on digital computers. Throughout this course, on occasion, we will present a simplified assembly language where we demonstrate what happens when code is executed on a computer.

For most humans who program computers, we use high-level programming languages to author programs to be executed by a computer. A high-level language is closer to the language you use in your mathematics class, and you can already read such expressions, but unlike mathematics, almost all programming languages are simply a sequence of letters, numbers or symbols, all of which are found on a standard keyboard. Numbers can be either integers (3, 7, 5, -182 and 0) or reals (3.252, -2.5932 and 3.141592654) and operations like addition, subtraction and division use +, - and /, respectively. Multiplication is usually represented by an asterisks, or *. There is no division symbol ($\div$) on a standard keyboard, so we cannot use such symbols in programming. Similarly, we cannot write $n \choose k$, or $|x|$, so we will have to deviate from standard mathematical notation. Some examples of expressions in mathematics and their equivalent in C++ are shown in Table 1.

Table 1. Mathematical expressions in English, mathematical notation and the C++ programming language.

Expressed formWritten formProgramming form
"Add $m$ and $n$ and multiply the sum by two."$2(x + y)$
or
$2 \times (x + y)$
2*(x + y)
"$n$ cubed over three"$n^3/3$ or $\frac{n^3}{3}$(n*n*n)/3
"After $s$ seconds, the ball has dropped one-half 9.8 $s$ squared metres plus $s$ times the initial velocity downward."$\frac{1}{2}9.8s^2 + s v_0$
or
$0.5\cdot9.8s^2 + s v_0$
0.5*9.8*s*s + s*v0
"Sine of $x$"$\sin(x)$ or
$\sin x$
sin(x)
"The absolute value of $x$"$|x|$abs(x)
"The square root value of $x$"$\sqrt{x}$sqrt(x)

Initially, you will see one weakness: if you want to calculate $x^5$, you must write x*x*x*x*x. We will deal with this weakness later; however, for right now, we will point out that calculating higher exponents isn't performed nearly as often as the other arithmetic operations of +, -, * and /.

We will learn such a high-level programming language. Each file will contain one program, and we will refer to that program as your source code. Another program, called a compiler, will translate your source code into machine language.

Definition: high-level programming language
A human-readable (and therefore human-writable) language that allows a human to more easily specify the operations that a computer is to perform.

Definition: source code
Human-readable program instructions written in a high-level programming language stored in a text file or files. These instructions cannot be executed by a computer and need to be compiled into machine instructions to create an executable program.

Definition: compiler
A program that takes source code written in a high-level programming language and translates it into machine language.

Before we go any further, let's actually look at a program:

#include <iostream>

int main();

int main() {
	std::cout << "Hello world!";
	std::cout << std::endl;

	return 0;
}

Before we get you to download an integrated development environment and require you to create projects, etc., it is better to start with an on-line C++ editor and compiler:

https://repl.it/

This is an on-line C++ compiler, and there are two major sections:

  • A left panel that allows for file control.
  • A central panel where you can edit your source code.
  • A right panel which displays the output.

These are shown in Figure 1.


Figure 1. The interface of https://repl.it/.

Definition: input and output
An executing program can request data from a user, and the program refers to such data as input, for the data is being entered into the program. When the program wishes to display data to a user, we refer to that as output.

For historical reasons, we will refer to the window for input and output as the console.

Now, you can either cut-and-paste this program into the editing panel of https://repl.it/ or load this program. Next, click Run▶ and the output will appear in the right panel where you will see the output Hello world!.

If you cut-and-pasted the above code and compilation failed, make sure that

  • the first line starts with a pound symbol (#), and
  • the last line contains just a single closing brace (}).

as you may have accidentally not selected the entire program. If it still doesn't work, try cutting and pasting the code again.

Installing an integrated development environment (IDE)

You may be comfortable using https://repl.it/, but as a computer or electrical engineer, you will be working in integrated environments for preforming all forms of compilation. Consequently, we strongly recommend you start by installing Eclipse or getting to know an IDE you prefer. This may include, for example, XCode if you are a Mac users. Not one percentage of your grade will be forfeited if you choose not to use Eclipse; however, you should choose some IDE.

Review

In your own words, describe each of these concepts:

Questions and practice:

1. Try to modify the code above to print Hello your name! where your name is your name. The text that is to be printed must appear between double quote symbols (").

2. Write a program that prints the following poetry to the screen:

A Elbereth Gilthoniel
silivren penna míriel
o menel aglar elenath!
Na-chaered palan-díriel
o galadhremmin ennorath,
Fanuilos, le linnathon
nef aear, sí nef aearon!

Previous lesson Next lesson