CH-1 Problem Solving Using Computers
CH-1 Problem Solving Using Computers
Pseudo code (derived from pseudo and code) is a compact and informal high-level
description of a computer algorithm that uses the structural conventions of programming
languages, but typically omits details such as subroutines, variables declarations and
system-specific syntax. The programming language is augmented with natural language
descriptions of the details, where convenient, or with compact mathematical notation. The
purpose of using pseudo code is that it may be easier for humans to read than conventional
programming languages, and that it may be a compact and environment-independent
generic description of the key principles of an algorithm. No standard for pseudo code
syntax exists, as a program in pseudo code is not an executable program. As the name
suggests, pseudo code generally does not actually obey the syntax rules of any particular
language; there is no systematic standard form, although any particular writer will
generally borrow the appearance of a particular language.
Example:
2. Write sum
The 'first do this, next do that' is a short phrase which really in a nutshell describes the
spirit of the imperative paradigm. The basic idea is the command, which has a measurable
effect on the program state. The phrase also reflects that the order to the commands is
important. 'First do that, then do this' would be different from 'first do this, then do that'.
Within the logic paradigm, program is thought of as a set of logic formulae: axioms (facts
and rules) describing properties of certain objects, and a theorem to be proved. Program
execution is a process of logic proving (inference) of the theorem through constructing the
objects with the described properties. The logic paradigm fits extremely well when applied
in problem domains that deal with the extraction of knowledge from basic facts and
relations
Let us consider now how we can define the brother relation in terms of simpler relations
and properties father, mother, and male. Using the Prolog logic language one can say:
Languages that follow this paradigm - Smalltalk, Eiffel, C++, and object Pascal
Table 1. Features of programming paradigms
1.7.9. Assertions
The assert macro-defined in the <cassert> header file-tests the value of an expression. If the
value of the expression is 0 (false), then assert prints an error message and calls function
abort (of the general utilities Iibrary-<cstdlib>) to terminate program execution. This is a
useful debugging tool for testing whether a variable has a correct value. For example,
suppose variable x should never be larger than 10 in a program. An assertion may be used
to test the value of x and print a n error message if the value of x is incorrect. The statement
would be
assert ( x <= 10 ) ;
If x is greater than 10 when the preceding statement is encountered in a program, an error
message containing the line number and file name is printed, and the program terminates.
The programmer may then concentrate on this area of the code to find the error. I f the
symbolic constant NDEBUG is defined, subsequent assertions will be ignored. Thus, when
assertions are no longer needed (i.e., when debugging is complete), the line
#define NDEBUG
is inserted in the program file rather than deleting each assertion manually. Most C + +
compilers now include exception handling. C++ programmers prefer using exceptions
rather than assertions. But assertions are still valuable for C++ programmers who work
with C legacy code.