# Chapter 1. Overview of Computers and Programming

## 1. Computers

Computers was originally invented to carry out numerical calculations in the 1930s-40s. Later they were gradually developed to process all kinds of data, such as numbers, texts, and other types of media.

A computer system consists of hardware and software.

Hardware is the equipments used to perform the necessary computations.

Software consists of programs that control the hardware to process data.

## 2. Hardware

Major hardware components of a computer include memory, processing unit, and input/output devices (monitor, keyboard, mouse, printer, and so on).

Memory is the place where the programs and data are stored. It can be imagined as an ordered sequence of storage locations called memory cells. Each cell has a unique address, which is like a serial number of the cell in the memory.

The data stored in a memory cell are called the contents of the cell. Program is treated as a special type of data.

A memory cell contains a sequence of binary digit, or bit. Each bit is either a 0 or a 1. All kinds of data are eventually represented by sequences of bits.

A sequence of 8 bits is usually called a byte, which can represent a character, such as the ones on a keyboard.

The terms used to quantify storage capacities:

```    1 byte (B) is 8 bits
1 kilobyte (KB) is 1024 bytes, i.e., 210 bytes
1 megabyte (MB) is 1024 kilobytes, i.e., 220 bytes
1 gigabyte (GB) is 1024 megabytes, i.e., 230 bytes
1 terabyte (TB) is 1024 gigabytes, i.e., 240 bytes```

To store a value into a certain address means to give it a new content (so the previous one is gone). To retrieve a value from a memory cell means to copy the content to another place without destroying it. These two operations are also called write and read, respectively.

In a computer, there are several types of memory. There is a distinction between main memory and secondary memory --- the former is faster and smaller, while the latter is cheaper, and often removable. At the current time, the former is usually in silicon chips, while the latter in hard disks, flash drives, CD/DVDs and so on.

There are two types of main memory: RAM (random access memory) and ROM (read-only memory). Their major difference is that the content of RAM can be modified. In the following, "main memory" means RAM.

In a computer, most of the operations are performed by a CPU (central processing unit), and there are computers with multiple CPUs. At the current time, a CPU is in a single integrated circuit (IC), or call it a chip.

The CPU follows the instructions contained in a program (written in a computer-understandable language). In each step, the CPU fetches (i.e., retrieves) an instruction, interprets its content to decide what to do, and then do it, which may mean to move data from one place to another, or change data in a certain way. Other common operations include addition, subtraction, multiplication, division, comparison, and so on.

CPU usually executes instructions one after another, but can also jump to another memory cell according to an instruction.

A computer uses its input/output (I/O) devices to communicate with human users and other computers.

For a human user, the usual input device is a keyboard and a mouse, and the usual output device is a monitor (display screen), and a printer. The human-computer interaction (HCI) can either happen in a command-line user interface, or a graphical user interface (GUI).

A computer network carries out communications among computer systems. There are different types of networks: local area network (LAN) and wide area network (WAN). The Internet connects computers all over the world, which supports the World Wide Web (WWW), among many of its usages.

To connect to another computer, a computer needs a modem (modulator/demodulator), or some other network device, to translate between its internal language and the language used in the network communication. The communications can go through all kinds of cables, or wireless.

## 3. Software

Every software is eventually executed by the CPU, in a machine language, in which each line in a program is an instruction to the hardware. Since programs in this language are not easily understandable by a human user, the same program is usually also described in other, more human-readable languages.

One type of them is assembly language, in which the instructions are represented by symbols and numbers.

Another type of language are more human-oriented, called "high-level languages", which are closer to mathematical languages and natural languages (such as English), as well as machine-independent.

Typical examples of high-level language include FORTRAN, ALGOL, COBOL, BASIC, Pascal, LISP, Prolog, Perl, C, C++, and Java.

The translation from high-level languages and assembly languages into machine languages is accomplished by special programs: compilers, interpreters, and assemblers. A compiler translates a source program in a high-level language into an object program in the machine language. An interpreter interprets and executes a program in a high-level language line by line. An assembler translates a source program in an assembly language into an object program in the machine language.

A high-level language usually comes with many ready-made common programs, so the user can include them in programs, rather than rewrite them. The program responsible for this is called a "linker". It links user object programs and related "library programs", and produces executable programs.

There are software packages called "integrated development environment" (IDE) which organize all the related software (e.g., editor, compiler, linker, loader, debugger) together to support the development of a software.

When a program in machine language runs, it typically gets some input data from the memory, process them according to the predetermined procedure, then store some output data into the memory, and display some information to the user.

In a computer, there is a software product that occupies a special position: the operating system (OS). Most of other software products are application software, which are managed and supported by the OS.

When a computer is turned on, it starts by executing part of the OS that is stored in a ROM, which then loads the rest of the OS from hard disk and starts it. This process is called "booting".

When running, an OS has the following main responsibility:

• communicating with the user,
• allocating resources (CPU time, memory space, printer usage, ...),
• connecting I/O devices with running programs,
• transferring data between main and secondary memory.
In summary, we often say that the OS manages processes and resources.

At the current time, the most often used OS include Unix/Linux, Microsoft Windows, and Macintosh OS. It is possible for a computer to have more than one OS stored in its memory, but usually only one can be used at a time.

An application software uses the computer to accomplish a specific task. They are usually purchased on CD or DVD, or downloaded from the Internet, and installed into the computer (so it is stored in memory and known to the OS), before they can be used in the computer.

## 4. Software development

Software developing, also called programming, is a problem-solving process. It usually consists of the following major steps:
1. Specify the problem: to state the problem clearly and unambiguously.
2. Analyze the problem: to identify the inputs and corresponding outputs.
3. Design an algorithm: to develop a list of steps, called an algorithm, that will start with the input and stop with the output.
4. Implement the algorithm: to write a program in a programming language according to the algorithm.
5. Test the program: to verify that the program indeed produces the desired results in selected testing cases.
6. Maintain the program: to update the program according to new requirements.
Very often, steps in the above procedure need to be repeated to fix the errors found in the process.

## 5. An example

Case study: kilometer-mile translation.
1. Specify the problem: a survey of maps, some with distances in kilometer, while others with miles. All results should be in kilometer.
2. Analyze the problem: distance in miles should be converted into distance in kilometers. The relationship is that one mile equals 1.609 kilometer. Therefore, the input is a miles value, the output is the corresponding kilometer value.
3. Design an algorithm: there are three steps: (1) get the distance in miles, (2) times 1.609 to that number, and (3) display the result.
4. Implement the algorithm: see a sample program.
5. Test the program: run it with several distances in miles as input, then check the output.
6. Maintain the program: unnecessary for this problem.