LP1 - Unit 1 - OVERVIEW OF COMPUTES, ALGORITHM, AND PROGRAM DESIGN
LP1 - Unit 1 - OVERVIEW OF COMPUTES, ALGORITHM, AND PROGRAM DESIGN
LP1 - Unit 1 - OVERVIEW OF COMPUTES, ALGORITHM, AND PROGRAM DESIGN
1 | Computer Programming 1
UNIT 1
1.1. Introduction
Hardware
There are three main classes of computers: PCs, workstations, and mainframes. A PC
(personal computer) is a relatively small computer designed to be used by one person
at a time. Most home computers are PCs, but PCs are also widely used in business,
industry, and science. A workstation is essentially a larger and more powerful PC. You
can think of it as an “industrial-strength” PC. A mainframe is an even larger computer
that typically requires some support staff and generally is shared by more than one user.
The distinctions between PCs, workstations, and mainframes are not precise, but the
terms are commonly used and do convey some very general information about a
computer.
2
1 | Computer Programming 1
Software
You do not normally talk directly to the computer, but communicate with it through an
operating system. The operating system allocates the computer’s resources to the
different tasks that the computer must accomplish. The operating system is actually a
program, but it is perhaps better to think of it as your chief servant. It is in charge of all
your other servant programs, and it delivers your requests to them. If you want to run a
program, you tell the operating system the name of the file that contains it, and the
operating system runs the program. If you want to edit a file, you tell the operating
system the name of the file and it starts up the editor to work on that file. To most users
the operating system is the computer. Most users never see the computer without its
operating system. The names of some common operating systems are UNIX, DOS,
Linux, Windows, Macintosh, and VMS. A program is a set of instructions for a
computer to follow. As shown in Display 1.3, the input to a computer can be thought
of as consisting of two parts, a program and some data. The computer follows the
instructions in the program, and in that way, performs some process. The data is what
we conceptualize as the input to the program. For example, if the program adds two
numbers, then the two numbers
are the data. In other words, the data is the input to the program, and both the program
and the data are input to the computer (usually via the operating system). Whenever we
give a computer both a program to follow and some data for the program, we are said to
be running the program on the data, and the computer is said to execute the program on
the data. The word data also has a much more general meaning than the one we have just
given it. In its most general sense it means any information available to the computer.
The word is commonly used in both the narrow sense and the more general sense.
High-Level Languages
There are many languages for writing programs. In this text we will discuss the C++
programming language and use it to write our programs. C++ is a high-level language,
as are most of the other programming languages you are likely to have heard of, such as
3
1 | Computer Programming 1
C, Java, Pascal, Visual Basic, FORTRAN, COBOL, Lisp, Scheme, and Ada. High-level
languages resemble human languages in many ways. They are designed to be easy for
human beings to write programs in and to be easy for human beings to read. A high-level
language, such as C++, contains instructions that are much more complicated than the
simple instructions a computer's processor (CPU) is capable of following. The kind of
language a computer can understand is called a low-level language.
The exact details of low-level languages differ from one kind of computer to another. A
typical low-level instruction might be the following:
ADD X Y Z
This instruction might mean “Add the number in the memory location called X to the
number in the memory location called Y, and place the result in the memory location
called Z.” The above sample instruction is written in what is called assembly language.
Although assembly language is almost the same as the language understood by the
computer, it must undergo one simple translation before the computer can understand it.
In order to get a computer to follow an assembly language instruction, the words need to
be translated into strings of zeros and ones. For example, the word ADD might translate
to 0110, the X might translate to 1001, the Y to 1010, and the Z to 1011.
The version of the above instruction that the computer ultimately follows would then be:
Assembly language instructions and their translation into zeros and ones differ from
machine to machine. Programs written in the form of zeros and ones are said to be written
in machine language, because that is the version of the program that the computer (the
machine) actually reads and follows.
Assembly language and machine language are almost the same thing, and the distinction
between them will not be important to us. The important distinction is that between
machine language and high-level languages like C++: Any high-level language program
must be translated into machine language before the computer can understand and follow
the program.
Compilers
A program that translates a high-level language like C++ to a machine language is called
a compiler. A compiler is thus a somewhat peculiar sort of program, in that its input or
data is some other program, and its output is yet another program. To avoid confusion,
the input program is usually called the source program or source code, and the translated
version produced by the compiler is called the object program or object code. The word
code is frequently used to mean a program or a part of a program, and this usage is
particularly common when referring to object programs. Now, suppose you want to run
a C++ program that you have written. In order to get the computer to follow your C++
instructions, proceed as follows. First, run the compiler using your C++ program as data.
4
1 | Computer Programming 1
Notice that in this case, your C++ program is not being treated as a set of instructions.
To the compiler, your C++ program is just a long string of characters. The output will be
another long string of characters, which is the machine-language equivalent of your C++
program. Next, run this machine-language program on what we normally think of as the
data for the C++ program. The output will be what we normally conceptualize as the
output of the C++ program. The basic process is easier to visualize if you have two
computers available, as diagrammed in
Figure 3. In reality, the entire process is accomplished by using one computer two
times.
The complete process of translating and running a C++ program is a bit more
complicated than what we showed in Display 1.4. Any C++ program you write will
Compiler
A compiler is a program that translates a high-level language program, such as a C++ program,
into a machine-language program that the computer can directly understand and execute.
use some operations (such as input and output routines) that have already been
programmed for you. These items that are already programmed for you (like input and
output routines) are already compiled and have their object code waiting to be combined
with your program’s object code to produce a complete machine-language program that
5
1 | Computer Programming 1
can be run on the computer. Another program, called a linker, combines the object code
for these program pieces with the object code that the compiler produced from your C++
program. The interaction of the compiler and the linker are diagrammed in Display 1.5.
In routine cases, many systems will do this linking for you automatically. Thus, you may
not need to worry about linking in very simple cases.
Linking
The object code for your C++ program must be combined with the object code for
routines (such as input and output routines) that your program uses. This process of
combining object code is called linking and is done by a program called a linker.
For simple programs, linking may be done for you automatically.
6
1 | Computer Programming 1
1.2.1 Algorithm
When learning your first programming language it is easy to get the impression that
the hard part of solving a problem on a computer is translating your ideas into the
specific language that will be fed into the computer. This definitely is not the case. The
most difficult part of solving a problem on a computer is discovering the method of
solution. After you come up with a method of solution, it is routine to translate your
method into the required language, be it C++ or some other programming language. It
is therefore helpful to temporarily ignore the programming language and to concentrate
instead on formulating the steps of the solution and writing them down in plain English
as if the instructions were to be given to a human being rather than a computer. A
sequence of instructions expressed in this way is frequently referred to as an algorithm.
The word algorithm has a long history. It derives from the name of a ninth-century
Persian mathematician and astronomer al-Khowarizmi. He wrote a famous
7
1 | Computer Programming 1
Display 1. An Algorithm
textbook on the manipulation of numbers and equations. The book was entitled
Kitab al-jabr w’almuqabala, which can be translated as Rules for reuniting and
reducing. The similar sounding word algebra was derived from the Arabic
word aljabr, which appears in the title of the book and is often translated as
reuniting or restoring. The meanings of the words algebra and algorithm used
to be much more intimately related than they are today. Indeed, until modern
times, the word algorithm usually referred only to algebraic rules for solving
numerical equations. Today the word algorithm can be applied to a wide
variety of kinds of instructions for manipulating symbolic as well as numeric
data. The properties that qualify a set of instructions as an algorithm now are
determined by the nature of the instructions rather than by the things
manipulated by the instructions. To qualify as an algorithm, a set of
instructions must completely and unambiguously specify the steps to be taken
and the order in which they are taken. The person or machine carrying out the
algorithm does exactly what the algorithm says, neither more nor less.
Algorithm
The first step is to be certain that the task—that which you want your program
to do—is completely and precisely specified. Do not take this step lightly. If
you do not know exactly what you want as the output of your program, you
may be surprised at what your program produces. Be certain that you know
what the input to the program will be and exactly what information is
supposed to be in the output, as well as what form that information should be
in.
For example,
If the program is a bank accounting program, you must know not only the
interest rate, but also whether interest is to be compounded annually, monthly,
daily, or whatever. If the program is supposed to write poetry, you need to
determine whether the poems can be in free verse or must be in iambic
pentameter or some other meter.
from the detailed rules of a programming language such as C++. The result is
that the algorithm design process becomes much less intricate and much less
prone to error. For even a modest size program, it can represent the difference
between a half day of careful work and several frustrating days of looking for
mistakes in a poorly understood program.
The implementation phase is not a trivial step. There are details to be concerned
about, and occasionally some of these details can be subtle, but it is much
simpler than you might at first think. Once you become familiar with C++ or
9
1 | Computer Programming 1
The program design process that we outlined in the previous section represents
a program as an algorithm (set of instructions) for manipulating some data. That
is a correct view, but not always the most productive view. Modern programs
are usually designed using a method known as Object Oriented Programming
or OOP. In OOP a program is viewed as a collection of interacting objects. The
methodology is easiest to understand when the program is a simulation
program.
For example,
4. Testing
5. Maintenance and evolution of the system
6. Obsolescence
We did not mention the last two phases in our discussion of program design
because they take place after the program is finished and put into service.
However, they should always be kept in mind. You will not be able to add
improvements or corrections to your program unless you design it to be easy to
read and easy to change.
The meaning of obsolescence is obvious, but it is not always easy to accept. When a
program is not working as it should and cannot be fixed with a reasonable amount
of effort, it should be discarded and replaced with a completely new program.
12
1 | Computer Programming 1
Activity #1
3. What is linking?
4. What is the first step you should take when creating a program?
B. An algorithm is approximately the same thing as a recipe, but some kinds of steps that
would be allowed in a recipe are not allowed in an algorithm.
1.3. References
Malini Devi J. (2014). C++ Programming Language for Beginner.
Chapter 1 pp. 1-4
1.4. Acknowledgment
The images, tables, figures, and information contained in this module
were taken from the references cited above.