1. Introduction to Computer Programming
3
1.1 The Making of an Art
The impact of the digital computer in the twentieth century is so great that the
computer has become a vital component in our society. "It has practically touched
every aspect of our lives."[LEES90] It helps us in our business transactions, keeps
track of products' inventories, diagnoses heart conditions, recognizes speech patterns,
and provides entertainment through games, among a hundred other applications. Its
impact is so great that "it has revolutionized our society and made us re-examine
certain basic assumptions about our own role in the universe."[HORO86] "Computers
have evolved from a scientific curiosity to an indispensable tool in virtually all areas
of our lives."[SAVI89]
However true it is that computers can solve problems efficiently and
accurately and how sophisticated it can be, it still cannot think for itself - it cannot
solve problems on its own. It's the human being who understands, thinks, and solves
problems on its own and who gives correct instruction that directs the computer to
solve problems. The set or list of instructions that directs the computer on its
operations is called a computer program. The person who develops a program is
called a programmer. The act of developing a program is called programming.
Computer programming is not just plainly
listing down lengthy steps to be communicated to
the computer. It is not a matter of "just write
anything and make it run". The programmer must
learn to put his logic and his creativity to
make his work a masterpiece. Computer
programming needs to be an art!
1.2 Steps in Program Planning and Development
Programming is a problem-solving activity. A person with good problem solving
skills will tend to be good programmers. To develop this skill, programmers practice
the following steps:
1. problem analysis
2. setting up an algorithm
3. coding
4. encoding
5. running, testing, and debugging
6. documentation
2. Chapter 1
4
1.2.1 Problem Analysis
If we are to use the computer as a problem-solving tool then we must have a good
analysis of the problem given. Here are some suggested steps on how to go about
analyzing a certain problem for computer application:
1. Review the problem carefully and understand what you are asked to do.
2. Determine what information is given (input) and what result must be produced
(output).
3. Assign names to each input and output item.
4. Determine the manner of processing that must be done on the input data to
come up with the desired output (i.e., determine what formulas are needed to
manipulate the given data).
Example.
Given the scores for the two departmental quizzes, two machine projects, final
exam, and teacher's evaluation, write a program that will compute for the final
grade based on the following computation:
50% Average of 2 departmental quizzes
+ 15% Average of 2 machine projects
+ 30% Final exam
+ 5% Teacher's evaluation
--------------------------------------------------------
Quiz No. 1 - Qz1 Final Grade - FG
Quiz No. 2 - Qz2
Mach. Proj. 1 - Mp1
Mach. Proj. 2 - Mp1
Final Exam - FE
Teacher's Eval. - TE
Qz1+Qz2 Mp1+Mp2
FG = 50% x ------------ + 15% x ----------- + 30% x FE + 5% x TE
2 2
1.2.2 Setting Up an Algorithm
After the problem has been clearly defined, a list or sequence of steps that will solve
the given problem must be formulated. This sequence of steps is called an algorithm.
An algorithm can be described in many ways. A natural language such as
Filipino, English, or Chinese can be used but we must be very careful that the
3. Introduction to Computer Programming
5
algorithm be organized in a logical and clear manner. Graphical forms or notations
such as flowcharts can be used, an improvement of the former, but is more
sophisticated. It is important to note that in whatsoever manner an algorithm is
written, it remains to be NOT executable simply because it cannot be entirely
understood by the computer.
To cite an example, let us use the final grade problem. A possible algorithm
written in English that will solve the problem would be:
1. Get Qz1 and Qz2 scores.
2. Get Mp1 and Mp2 scores.
3. Get FE score.
4. Get TE score.
5. Calculate
Qz1+Qz2 Mp1+Mp2
FG = 50% x ------------ + 15% x ----------- + 30% x FE + 5% x TE
2 2
6. Display the final grade FG.
An algorithm may also be viewed as a recipe. The manner in which the series
of steps in a recipe is written is similar to creating an algorithm. But, it should be
noted that an algorithm is more than a recipe. An algorithm has to be exact. An
example statement in a recipe would be “add salt to taste”. In an algorithm, this is not
acceptable because the statement is subjective and not quantifiable. Everything
should be definite.
1.2.3 Coding
After having set up the algorithm, the next step is to convert this into a list of
instructions in a code or language that the computer can understand and execute. This
process is called coding.
4. Chapter 1
6
Example.
Algorithm Program
The list of instructions that will implement the algorithm can be understood
by the computer simply because "it is written in the vocabulary of the programming
language and conforms to the grammatical rules of the language."[LEES90] This list
of instructions understood by the computer is now called a program.
In the example above, we converted our algorithm for computing the final
grade to a C program. The scanf statements ask for input from the user while the
printf statement displays the output.
1.2.4 Encoding
The process of entering the program through a computer terminal directly into
computer memory is called encoding.
1.2.5 Running, Testing, and Debugging
The fifth step in program development would be to execute or run the program. The
program is executed or run on different input data. Testing is the art of creating
Get Qz1 and Qz2 scores.
Get Mp1 and Mp2 scores.
Get FE score.
Get TE score.
Calculate FG.
Display the final grade
FG.
main()
{ float fQ1, fQ2;
float fMP1, fMP2;
float fFE, fTE, fFG;
scanf("%f", &fQ1);
scanf("%f", &fQ2);
scanf("%f", &fMP1);
scanf("%f", &fMP2);
scanf("%f", &fFE);
scanf("%f", &fTE);
fFG = 0.50 * ((fQ1 +
fQ2)/2) + 0.15 *
((fMP1 + fMP2)/2) +
0.3 * fFE + 0.05 *
fTE;
printf("FG = %.2fn",
fFG);
}
5. Introduction to Computer Programming
7
Did you know that...
The term bug dates back
to an old story about a group of
programmers who couldn't
figure out what was wrong with
their programs, until they
opened up the computer and
found a moth trapped inside
[REGE87]
different sets of sample data upon which the program will be run. The programmer
must submit the program to as many combinations of test data as possible to
anticipate and correct any error that might come out before releasing the program to
users. The process of executing a program with test input data and checking the
output results against the requirements is referred to as desk checking [PRAT96].
Errors that come out during program execution are termed as bugs. Computer
programmers use terms as bug-ridden and buggy to describe a program which is
poorly written thus containing a lot of errors. The art of correcting these errors is
called debugging.
These bugs or errors can be classified
into two groups: 1. syntactical or logical errors;
and 2. compile or run-time errors.
Syntactical errors result from failure to
follow the syntax of the language. Syntax refers
to the grammatical rules of the language
defining its legal constructs. Examples of which
are unrecognized instructions, missing
punctuation marks and misspelled names.
Logical errors are hard to identify.
What is erroneous here are the outputs seen
onscreen which did not conform to or match the
expected results. Such errors arise during the
formulation of algorithm or in the coding of the program that implements the
algorithm due to the wrong analysis or perhaps wrong approach of the programmer as
he tackles the problem given.
Compile-time errors halt the compilation of the program. Compilation means
translating the program codes into a form that the physical computing machine can
understand. Program codes are translated completely so long as their syntax is correct.
Once a syntax error is encountered during compilation, this is considered to be a
compile-time error.
Errors that appear during program execution are called run-time errors. Once
a program starts running, it means that the program is already free of syntax errors
and compilation has successfully finished. However, it may still have logical errors
that may cause the abnormal termination of the program. An example would be
dividing a certain number by zero. Another would be the program executing in an
endless loop wherein the only way to stop it is to manually stop the execution of the
program.
6. Chapter 1
8
1.2.6 Documentation
There are three basic types of documentation. The most common type of
documentation is the user’s manual. Software usually comes with a user’s manual
and it contains information on the software and hardware requirements, installation
procedures, and step-by-step instructions on how to use the system. This type of
documentation is used by the user of the program.
In the process of making sure that the program is error-free, running correctly
and efficiently, documentation of the program must be done. The purpose of which is
to maintain the relevance and validity of the program. For the next two types of
documentation, the process must be continuous. Programs in real-world applications
will likely be used for a number of years and will probably require some modification
as time passes. Especially in large programs developed for complex projects, there
will usually be obscure bugs that do not become apparent until after the program has
been placed in use.[LEES90] Such modifications made due to further discovering
errors while the program is in use must be documented well. Ten years after the
program has been written, someone may want to update it and the first thing he'll do
is to look at the documentation of the program to become aware of the details of the
program. Good documentation reduces program maintenance efforts and major
problem reconstruction. These are the objectives of creating the technical manual and
internal documentation.
The technical manual is a printed copy of the information regarding how the
program was designed and how it was created. Issues involved in choosing the data
type or data structure, as well as the algorithm for the solution, are also included. On
the other hand, the internal documentation has the same information but these are
stored within the program themselves, through the use of comments.