Chapter - 1
Chapter - 1
INTRODUCTION
TO
PROGRAMMING
PROBLEM SOLVING
A problem as a question or situation that presents
uncertainty or difficulty.
For example: Selling a product in the shop is the task
here.
The problem is to sell Or how a sales person sells a product in
the shop.
For a sale to happen in a shop you should go through
the following:
Determine what the unit price of the product is.
Count the quantity, (how many of the product) and
Calculate the total amount to be paid by the customer.
Calculate the change that you have to give back to the
customer.
Problem Solving Using Computers
Computers can be programmed to do many
complicated tasks at very high speeds.
We can use computers as a tool in problem
solving if a problem has:
It has extensive input and output.
Its method of solution is too complicated to implement
manually.
If done manually, it takes an excessively long time to
solve.
To use the same method of solution to solve the
same problem with different inputs.
Programming refers to the process of making
(enabling) a machine to perform a particular task
by giving it instructions (programs) and inputs.
It is the process of solving a problem using
computers.
A program is a detailed set of instructions
written in a programming language that directs
the computer to solve a problem.
For the instructions to be carried out, a computer
must execute a program
that is, the computer reads the program, and then
follows the steps encoded in the program in a precise
order until completion.
SOFTWARE DEVELOPMENT PHASES
Software development method is a method of
developing computer programs to solve
problems using a computer.
The software development method consists of
the following steps:
Requirements Specification
Consists of understanding exactly
what the problem is,
what is needed to solve it,
what the solution should provide, and
if there are constraints and conditions.
Analysis
In the analysis phase we should identify the
following:
Inputs to the problem, their form, and the input
media to be used
Outputs expected from the solution, their form,
and the output media to be used
Any special constraints or conditions
Formulas or equations to be used
Design
The next step is to design a method of solution
for the problem.
A method of solution is also called algorithm
Algorithm is a series of steps to be performed
in a specific logical order.
Algorithm can be designed by using either:
Pseudo codes or
Flow charts
Implementation
Translate each step of the algorithm into a language
statement.
Ends up with a computer program.
A computer program is a sequence of a finite number
of statements expressed in a programming language in
a specific logical order that, when executed, produce
the solution for a problem.
This phase consists of
choosing the appropriate programming language and
writing a program following the syntax of the language exactly.
Testing and Verification
Check the correctness of the program and verify that
the program will do what is expected to do.
Program Verification
is the process of ensuring that a program meets user
requirements. One of the techniques that can be used for
program verification is Program Testing.
Program Testing
is the process of executing a program to demonstrate its
correctness.
A program must be tested using a sufficiently large
sample of test data sets such that every logical path in
the program is traversed at least once.
Testing the program consists of :
Desk-checking- is simply reading through, or
checking the program to make sure that it is
free of errors and that the logic works
Debugging the program- means to detect,
locate and remove all errors in a computer
program
Run real-world data
Testing the program with real data and real users
It is also advisable to test it with faulty or
incomplete or overwhelming data
Program Documentation
Prepare documents that explain
how the program works and
how to use it.
Program documentation consists of these elements:
A concise requirements specification.
Descriptions of problem inputs, expected outputs, constraints,
and applicable formula
A pseudocode or flowchart for its algorithm.
A source program listing.
A hard copy of a sample test runs of the program.
A user’s guide explaining to non programmer users how the
program should be used.
PROGRAM ALGORITHMS
Algorithm:
is a sequence of a finite number of steps
arranged in a specific logical order which is
used to develop the solution for a problem
Pseudocodes and flowcharts can be used
to develop algorithms.
Pseudocoding and Flowcharting
Pseudocoding:
Pseudocode is a semiformal, English-like
language with a limited vocabulary that can be
used to design and describe algorithms.
A pseudocode language is a more appropriate
algorithm description language than any
programming language.
A pseudocode can be used for:
Designing algorithms
Communicating algorithms to users
Debugging logic errors in program
Documenting programs for future maintenance and
expansion purposes
Flowcharting:
is a graph consisting of geometrical shapes that are connected
by flow lines.
The geometrical shapes in a flowchart represent the
types of statements in an algorithm.
The details of statements are written inside the shapes.
Examples
Example:1 The following set of instructions
forms a detailed algorithm in pseudo code for
calculating the payment of person.
Input the three values into the variables Name,
Hours, Rate.
Calculate Pay = Hours Rate.
Display Name and Pay.
PROGRAMMING
LANGUAGES
PROGRAMMING LANGUAGES
Early programming languages were
designed for specific kinds of tasks.
Modern languages are more general-
purpose.
In any case, each language has its own
characteristics, vocabulary, and syntax.
Many programming languages have
some form of written specification of their
syntax (form) and semantics (meaning).
Types [Levels] of
Programming Languages
There is only one programming language
that any computer can actually understand
and execute:
its own native binary machine language.
All other languages are said to be high
level or low level according to how
closely they can be said to resemble
machine code.
1. Low-level languages
They allow instructions to be written at
the hardware level.
Thus, a program written in a low-level
language can be
extremely efficient,
making optimum use of both computer
memory and processing time.
However, to write a low-level program
needs
a substantial amount of time,
as well as a clear understanding of the inner
workings of the processor itself.
Characteristics of LOW Level Languages:
Advantages of a Compiler
Fast in execution
The object program can be used without the need to of re-
compilation.
Disadvantages of a Compiler
Debugging a program is much harder.
When an error is found, the whole program has to be re-
compiled
3. Interpreters
An interpreter takes source code and converts each line
in succession.
Advantages of an Interpreter
Good at locating errors in programs
Debugging is easier
If an error is removed, there is no need to retranslate
Disadvantages of an Interpreter
Execution of an interpreted program is much slower
No object code is produced.
For the program to run, the Interpreter must be present.
C++
PROGRAM
COMPILATION
C++ PROGRAM COMPILATION
When a C++ program is written, it must be typed
into the computer and saved to a file.
A text editor, which is similar to a word processing
program, is used for this task.
The statements written by the programmer are
called source code, and the file they are saved
in is called the source file.
After the source code is saved to a file, the
process of translating it to machine language
can begin.
… C++ PROGRAM COMPILATION
The following
illustrates the
process of
translating a
source file
into an
executable
file.
The C++
Preprocessor
The C++ Preprocessor
A C++ compiler begins by invoking the preprocessor,
Preprocessor a program that uses special statements,
known as directives or control statements, that cause
special compiler actions such as:
File inclusion, in which the file being preprocessed incorporates
the contents of another file;
Macro substitution, in which one sequence of text is replaced
by another;
Conditional compilation, in which parts of the source file's
code can be eliminated at compile time under certain
circumstances.
All preprocessor directives begin with the # symbol
(known as pound or hash), which must occur in the
leftmost column of the line.