LP1 - Unit 1 - OVERVIEW OF COMPUTES, ALGORITHM, AND PROGRAM DESIGN

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

0

1 | Computer Programming 1

UNIT 1

OVERVIEW OF COMPUTES, ALGORITHM, AND


PROGRAM DESIGN

1.0 Intended Learning Outcomes


On completion of the module, you should be able to:

1. Identify and analyze computer hardware components and


software types.
2. Create a logical algorithm and flowchart for a given problem.

1.1. Introduction

This is a fast-paced introductory course to the C++ programming language. It is


intended for those with little programming background, though prior programming experience
will make it easier, and those with previous experience will still learn C++-specific constructs
and concepts.

Structured programming (sometimes known as modular programming) is a subset of


procedural programming that enforces a logical structure in the programming being written to
make it more efficient and easier to understand and modify. Structured programming
frequently employees a top-down design model, in which developers map out the overall
program structure into separate subsections. A defined function or set of similar functions is
coded in a separate module can be reused in other programs. After a module has been tested
individually, it is then integrated with other modules into the overall program structure.
Program flow follows a simple hierarchical model that employs looping constructs such as
"for", "repeat" and "while". Use of the "Go To" statement is discouraged. Structured
programming was first suggested by Corrado Bohm and Guiseppe Jacopini. The two
mathematicians demonstrated that any computer program can be written with just three
structures: decision, sequences, and loops. Coders should break larger pieces of code into
shorter subroutines (functions, procedures, methods, blocks, or otherwise) that are small
enough to be understood easily. In general, programs should use local variables and take
arguments by either value or reference. These techniques help to make isolated small pieces of
code easier to understand the whole program at once.
1
1 | Computer Programming 1

1.2. What is a Computer?

A set of instructions for a computer to follow is called a program. The collection of


programs used by a computer is referred to as the software for that computer. The actual
physical machines that make up a computer installation are referred to as hardware. As we will
see, the hardware for a computer is conceptually very simple. However, computers now come
with a large array of software to aid in the task of programming. This software includes editors,
translators, and managers of various sorts.
The resulting environment is a complicated and powerful system. In this book we are concerned
almost exclusively with software, but a brief overview of how the hardware is organized will
be useful.

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:

0110 1001 1010 1011

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.

A sequence of precise instructions which leads to a solution is called an


algorithm. Some approximately equivalent words are recipe, method, directions,
procedure, and routine. The instructions may be expressed in a programming language
or a human language. Our algorithms will be expressed in English and in the
programming language C++. A computer program is simply an algorithm expressed in
a language that a computer can understand. Thus, the term algorithm is more general
than the term program. However, when we say that a sequence of instructions is an
algorithm, we usually mean that the instructions are expressed in English since if they
were expressed in a programming language we would use the more specific term
program. An example may help to clarify the concept.

Display 1. contains an algorithm expressed in rather stylized English. The algorithm


determines the number of times a specified name occurs on a list of names. If the list
contains the winners of each of last season's football games and the name is that of your
favorite team, then the algorithm determines how many games your team won. The
algorithm is short and simple but is otherwise very typical of the algorithms with which
we will be dealing. The instructions numbered 1 through 5 in our sample algorithm are
meant to be carried out in the order they are listed. Unless otherwise specified, we will
always assume that the instructions of an algorithm are carried out in the order in which
they are given (written down). Most interesting algorithms do, however, specify some
change of order, usually a repeating of some instruction again and again such as in
instruction 4 of our sample 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

An algorithm is a sequence of precise instructions that leads to a solution.

1.2.2 Program Design

Designing a program is often a difficult task. There is no complete set of rules,


no algorithm to tell you how to write programs. Program design is a creative
process. Still, there is an outline of a plan to follow. The outline is given in the
8
1 | Computer Programming 1

diagrammatic form in Display 1.7. As indicated there, the entire program-


design process can be divided into two phases, the problem-solving phase, and
the implementation phase. The result of the problem-solving phase is an
algorithm, expressed in English, for solving the problem. To produce a
program in a programming language such as C++, the algorithm is translated
into the programming language. Producing the final program from the
algorithm is called the implementation phase.

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.

Many novice programmers do not understand the need to design an algorithm

before writing a program in a programming language, such as C++, and so they


try to short-circuit the process by omitting the problem-solving phase entirely,
or by reducing it to just the problem definition part. This seems reasonable.
Why not “go for the mark” and save time? The answer is that it does not save
time! Experience has shown that the two-phase process will produce a correctly
working program faster.

The two-phase process simplifies the algorithm design phase by isolating it

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

any other programming language, the translation of an algorithm from English


into the programming language becomes a routine task.

As indicated in Display 2, testing takes place in both phases. Before the


program is written, the algorithm is tested, and if the algorithm is found to be
deficient, then the algorithm is redesigned. Desktop testing is performed by
mentally going through the algorithm and executing the steps yourself. On
large algorithms, this will require a pencil and paper.

The C++ program is tested by compiling it and running it on some sample


input data. The compiler will give error messages for certain kinds of errors. To
find other types of errors, you must somehow check to see if the output is
correct.

Display 2. Program Design Process

The process diagrammed in Display 2 is an idealized picture of the program


design process. It is the basic picture you should have in mind, but the reality
is sometimes more complicated. In reality, mistakes and deficiencies are
discovered at unexpected times, and you may have to back up and redo an
earlier step. For example, testing the algorithm may reveal that the definition
of the problem was incomplete. In such a case you must back up and
reformulate the definition. Occasionally, deficiencies in the definition or
algorithm may not be observed until a program is tested. In that case, you must
back up and modify the definition or algorithm and all that follows them in the
design process.
10
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,

For a program to simulate a highway interchange, the objects might represent


the automobiles and the lanes of the highway. Each object has algorithms that
describe how it should behave in different situations. Programming in the OOP
style consists of designing the objects and the algorithms they use. When
programming in the OOP framework the term Algorithm design in Display 2
would be replaced with the phrase Designing the objects and their algorithms.

The main characteristics of OOP are encapsulation, inheritance, and


polymorphism.

1. Encapsulation is usually described as a form of information hiding or


abstraction. That description is correct, but perhaps an easier-to-
understand characterization is to say that encapsulation is a form of
simplification of the descriptions of objects.

2. Inheritance has to do with writing reusable program code.

3. Polymorphism refers to a way that a single name can have multiple


meanings in the context of inheritance. Having made those statements,
we must admit that they hold little meaning for readers who have not
heard of OOP before. However, we will describe all these terms in detail
later in this book. C++ accommodates OOP by providing classes, a kind
of data type combining both data and algorithms.

The Software Life Cycle


Designers of large software systems, such as compilers and operating systems,
divided the software development process into six phases known as the software life
cycle.

The six phases of this life cycle are:

1. Analysis and specification of the task (problem definition)


2. Design of the software (object and algorithm design)
3. Implementation (coding)
11
1 | Computer Programming 1

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.

• Designing programs so that they can be easily modified will be an


important topic that we will discuss in detail when we have developed a
bit more background and a few more programming techniques.

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

A. Write the correct answer to the following questions below.

1. What is the difference between a machine-language program and a high-level


language program?

2. What is the role of a compiler?

3. What is linking?

4. What is the first step you should take when creating a program?

5. Explain why the problem-solving phase should not be slighted.

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.

Which steps in the following recipe would be allowed?

Place 2 teaspoons of sugar in mixing bowl.

Add 1 egg to mixing bowl.

Add 1 cup of milk to mixing bowl.

Add 1 ounce of rum, if you are not driving.

Add vanilla extract to taste.

Beat until smooth.

Pour into a pretty glass.

Sprinkle with nutmeg.


13
1 | Computer Programming 1

1.3. References
Malini Devi J. (2014). C++ Programming Language for Beginner.
Chapter 1 pp. 1-4

Hari Mohan Pandey (2015). Object-Oriented Programming C++


Simplified; Computer Engineering Department NMIMS University
Mumbai; An ISO 9001:2008 Certified Company; ISBN 978-93-81159-50-7

C++ Programming Language


https://www.tutorialspoint.com/cplusplus/index.htm
https://www.w3schools.com/cpp/
https://cplusplus.com/doc/tutorial/

1.4. Acknowledgment
The images, tables, figures, and information contained in this module
were taken from the references cited above.

You might also like