Skip to content
Snippets Groups Projects
Commit e2d450f7 authored by cprutean's avatar cprutean
Browse files

Update file program.ipynb

parent a66952de
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:776882d3 tags: %% Cell type:markdown id:776882d3 tags:
# What is a Computer Program # What is a Computer Program
## What is a Computer ? ## What is a Computer ?
Before we start teaching *computer programming* we need to first ask Before we start teaching *computer programming* we need to first ask
*what is a computer*. A simplfied structure of a computer is shown *what is a computer*. A simplfied structure of a computer is shown
below. below.
<div> <div>
<img src="png.png" width="500"/> <img src="png.png" width="500"/>
</div> </div>
The *Central Processing Unit* (CPU) operates on a series of instructions The *Central Processing Unit* (CPU) operates on a series of instructions
called a *program* which is stored in *memory*. The CPU consists of an called a *program* which is stored in *memory*. The CPU consists of an
*Arithmetic Logic Unit* (ALU) which performs such functions as addition, *Arithmetic Logic Unit* (ALU) which performs such functions as addition,
subtraction, comparison etc. and a *Control Unit* which directs and subtraction, comparison etc. and a *Control Unit* which directs and
monitors the operation of the computer. The *input* and *output* provide monitors the operation of the computer. The *input* and *output* provide
an interface to the outside world, for example the *input* may be the an interface to the outside world, for example the *input* may be the
keyboard and *output* the display screen. The *input* and *output* units keyboard and *output* the display screen. The *input* and *output* units
also provide access to data storage devices such as hard discs, DVDs, also provide access to data storage devices such as hard discs, DVDs,
USB devices etc. All these may be either connected directly to the USB devices etc. All these may be either connected directly to the
computer or over the *network*. computer or over the *network*.
The computer holds all information (numbers, letters, instructions) in The computer holds all information (numbers, letters, instructions) in
memory as patterns of binary bits, (ones and zeros). The instructions memory as patterns of binary bits, (ones and zeros). The instructions
are decoded by the *Control Unit* and determine what operations the ALU are decoded by the *Control Unit* and determine what operations the ALU
performs on the data. The desired sequence of instructions (the performs on the data. The desired sequence of instructions (the
*Program*) have to be placed in memory by the “programmer”, which is *Program*) have to be placed in memory by the “programmer”, which is
what we will be doing during this course. Remember that the computer is what we will be doing during this course. Remember that the computer is
a machine that exactly follows instructions and it is the sole a machine that exactly follows instructions and it is the sole
responsibility of the programmer to get these instructions right! responsibility of the programmer to get these instructions right!
The binary instructions used by the computer are not convenient for The binary instructions used by the computer are not convenient for
humans to use. This is further complicated by the fact that different humans to use. This is further complicated by the fact that different
computers use different sets of instructions. It is therefore much more computers use different sets of instructions. It is therefore much more
convenient to program in a *High Level Language* which is (more) humanly convenient to program in a *High Level Language* which is (more) humanly
readable and is independent of the details of the computer being used. readable and is independent of the details of the computer being used.
The high level language chosen for this course is . The high level language chosen for this course is .
From the model above the *program* is a set of instuctions that the From the model above the *program* is a set of instuctions that the
computer hardware implements in strict sequential order. computer hardware implements in strict sequential order.
## What is a program? ## What is a program?
### Key Point ### Key Point
The actual program is: The actual program is:
1. written in a *high level language* by the “programmer”, typically as 1. written in a *high level language* by the “programmer”, typically as
a text file, a text file,
2. converted into low level *machine instuctions*, 2. converted into low level *machine instuctions*,
3. loaded into memory, 3. loaded into memory,
4. executed by the hardware. 4. executed by the hardware.
so as a *programmer* you see only the *high level language*; all the so as a *programmer* you see only the *high level language*; all the
rest is handled for you by the *operating system*. rest is handled for you by the *operating system*.
> #### More details > #### More details
`Python` is an *interpreted language* which means that the > `Python` is an *interpreted language* which means that the
*conversion to low level machine instuctions*, *load into memory* all > *conversion to low level machine instuctions*, *load into memory* all
occurs automatically *on the fly* as you execute the program, for > occurs automatically *on the fly* as you execute the program, for
example with the command > example with the command
```python >```python
python3 MyProgram.py python3 MyProgram.py
``` >```
from a Terminal Window. You do not have to worry about the underlying > from a Terminal Window. You do not have to worry about the underlying
details, particularly since we are running from Jupyter Notebooks in this course, but an understanding of what is “happening” is useful > details, particularly since we are running from Jupyter Notebooks in this course, but an understanding of what is “happening” is useful
when you are trying to work out why your program is not working…. > when you are trying to work out why your program is not working….
>In particular when things go wrong, has it failed during >In particular when things go wrong, has it failed during
1. conversion to low level instructions (syntax error), >1. conversion to low level instructions (syntax error),
2. during load (not able to find libraries or modules), >2. during load (not able to find libraries or modules),
3. or execution (did something numerically wrong). >3. or execution (did something numerically wrong).
There are more details on this in [Finding and fixing bugs](bugs.ipynb). >There are more details on this in [Finding and fixing bugs](bugs.ipynb).
### Key Point ### Key Point
So a *computer program* actually is So a *computer program* actually is
1. a set of instructions written in a precisely defined *language* with 1. a set of instructions written in a precisely defined *language* with
a very strict *syntax*, a very strict *syntax*,
2. implemented by *hardware* in a strict and precisely defined 2. implemented by *hardware* in a strict and precisely defined
sequential order. sequential order.
and it is the sole responsibility of the *programmer* to get this right. and it is the sole responsibility of the *programmer* to get this right.
This is why computer programing is *not easy*. This is why computer programing is *not easy*.
In this course you have to learn two things at the same time, these In this course you have to learn two things at the same time, these
being being
1. the syntax of the *language*; *this is mainly covered in the on-line 1. the syntax of the *language*; *this is mainly covered in the on-line
notes and examples* notes and examples*
2. how to formulate a problem in the required sequential order (program 2. how to formulate a problem in the required sequential order (program
design); *this is mainly covered in the mini-lectures*. This is design); *this is mainly covered in the mini-lectures*. This is
challenging part and a skill you will develop as progress through challenging part and a skill you will develop as progress through
this and subsequent courses. this and subsequent courses.
All of this will only start to come together as *you* work through this All of this will only start to come together as *you* work through this
course. course.
> #### (Apparent) similarity with mathematics > #### (Apparent) similarity with mathematics
When you look at the source for any *computer program* there is an *apparent* similarity with When you look at the source for any *computer program* there is an *apparent* similarity with
mathematics. There are *variables*, *functions* and signs; this can lead mathematics. There are *variables*, *functions* and signs; this can lead
novice programmers into a false sense of security…you are NOT *doing novice programmers into a false sense of security…you are NOT *doing
mathematics*, you *are* learning a new language that happens to use the mathematics*, you *are* learning a new language that happens to use the
same symbols! same symbols!
For example, the usage of ``=`` for assignment (see later) is very different from *equals* and can lead to quite hard to spot bugs.| For example, the usage of ``=`` for assignment (see later) is very different from *equals* and can lead to quite hard to spot bugs.|
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment