Getting Ready To Program
Getting Ready To Program
by Ira Pohl
Programs are written to instruct machines to carry out specific tasks or to solve specific
problems. A step-by-step procedure that accomplishes a desired task is called an algorithm.
Thus, programming is the activity of communicating algorithms to computers. We have all
given instructions to someone in English and then had that person carry out the instructions.
The programming process is analogous, except that machines have no tolerance for
ambiguity and must have all steps specified in a precise language and in tedious detail.
The Programming Process
1. Specify the task.
2. Discover an algorithm for its solution.
3. Code the algorithm in C++.
4. Test the code.
A computer is a digital electronic machine composed of three main components:
processor, memory, and input/output devices. The processor is also called the central
processing unit, or CPU. The processor carries out instructions that are stored in the
memory. Along with the instructions, data also is stored in memory. The processor typically
is instructed to manipulate the data in some desired fashion. Input/output devices take
information from agents external to the machine and provide information to those agents.
Input devices are typically keyboards, mice, scanners, digital cameras, and joysticks. Output
devices are typically screens, printers, speakers, and headphones. The physical makeup of a
machine can be quite complicated, but the user need not be concerned with the details.
The operating system consists of a collection of special programs and has two main
purposes. First, the operating system oversees and coordinates the resources of the machine
as a whole. For example, when a file is created on a disk, the operating system takes care of
the details of locating it in an appropriate place and keeping track of its name, size, and date
of creation. Second, the operating system provides tools to users, many of which are useful
to the C++ programmer. Two of these tools are of paramount importance: the text editor and
the C++ compiler.
We assume the reader can use a text editor to create and modify files containing C++
code. C++ code is also called source code, and a file containing source code is called a
source file. After a file containing source code (a program) has been created, the C++
compiler is invoked. This process is system-dependent. For example, on many UNIX
systems, we can invoke the C++ compiler with the command
CC pgm.cpp
where pgm.cpp is the name of a file that contains a program. If there are no errors in
pgm.cpp, this command produces an executable file – one that can be run, or executed.
Although we think of this as compiling the program, what actually happens is more
complicated.
When we compile a simple program, three separate actions occur: first the preprocessor
is invoked, then the compiler, and finally the linker. The preprocessor modifies a copy of
the source code by including other files and by making other changes. The compiler
translates this into object code, which the linker then uses to produce the final executable
file. A file that contains object code is called an object file. Object files, unlike source files,
usually are not read by humans. When we speak of compiling a program, we really mean
invoking the preprocessor, the compiler, and the linker. For a simple program, this is all
done with a single command. After the programmer writes a program, it has to be compiled
and tested. If modifications are needed, the source code has to be edited again. Thus, part of
the programming process consists of this cycle:
When the programmer is satisfied with the program performance, the cycle ends.