Chapter 2
Chapter 2
What is a problem?
Problem is defined as the difference between an existing situation and a desired
Situation, that is, in accordance with calculation; a problem is numerical situation and has
complex form. Solution is desired situation and has simplest form. If a problem is solved by
computing using machine called computer, then such process is called Problem Solving using
Computer. The activities we have to plan in a sequence so that we can solve the problems and
get fruitful outcomes are:
1. Problem Analysis.
2. Algorithm development.
3. Flowchart development
4. Coding.
5. Compilation and Execution.
6. Debugging and Testing.
7. Documentation.
Properties
Finiteness: Program should have finite number of steps to solve a problem.
Definiteness: The action of each step should be defined clearly without any ambiguity.
Inputs: Inputs of the algorithms should be defined precisely, which can be given initially or
while the algorithm runs.
Outputs: Each algorithm must result in one or more outputs.
Effectiveness: It should be more effective among the different ways of solving the problems.
Flowchart
A flowchart is a pictorial representation of an algorithm that uses boxes of different shapes to
denote different types of instructions. The actual instructions are written within these boxes using
clear and concise statements. These boxes are connected by solid lines having arrow marks to
indicate the flow of operation, that is, the exact sequence in which the instructions are to be
executed.
Advantages of Using Flowcharts
Communication: Flowcharts are better way of communicating the logic of a system to
all concerned or involved.
Effective analysis: With the help of flowchart, problem can be analyzed in more
effective way therefore reducing cost and wastage of time.
Proper documentation: Program flowcharts serve as a good program documentation,
which is needed for various purposes, making things more efficient.
Efficient Coding: The flowcharts act as a guide or blueprint during the systems analysis
and program development phase.
Proper Debugging: The flowchart helps in debugging process.
Efficient Program Maintenance: The maintenance of operating program becomes easy
with the help of flowchart. It helps the programmer to put efforts more efficiently on that
part
Flowchart Symbols
In drawing a proper flow chart, all necessary requirements should be listed out in
logical order.
The flowchart should be clear, neat and easy to follow. There should not be any
room for ambiguity in understanding the flow chart.
The usual direction of the flow of a procedure or system is from left to right
or top to bottom.
(Before coding analysis and design is important. Here explain importance of problem analysis,
algorithm and flowcharting.)
Coding
In order to make a program in any programming language, what we have written is known as
code. The act of writing code in a computer language is known as coding. In other words, code is
a set of instruction that a computer can understand.
The process by which source codes of a computer (programming) language are translated into
machine codes is known as compilation. After compilation if everything is ok, the code is going
under other process that is known as execution. We can get the required output after execution
process.
Compiler:
A compiler is a program that translates a program written in High level language to executable
machine language. The process of transferring High level source program in to object code is a
lengthy and complex process as compared to assembling. Compliers have diagnostic capabilities
and prompt the programmer with appropriate error message while compiling a High level
language program. The corrections are to be incorporated in the program, whenever needed, and
the program has to be recompiled. The process is repeated until the program is mistake free and
translated to an object code. Thus the job of a complier includes the following:
To translate High level language source program to machine codes.
To trace variables in the program.
To include linkage for subroutines.
To allocate memory for storage of program and variables.
To generate error messages, if there are errors in the program.
Compiler
Executable
Runtime Linker program
Library
The process of translation from high level language (source code) to low level language (object
code) is called compilation.
The first step is to pass the source code through a compiler, which translates the high
level language instructions into object code.
The final step is producing an executable program is to pass the object code through a
linker. The linker combines modules and gives real values to all symbolic addresses,
there by producing machine code.
Compilation process ends producing an executable program.
The compiler stores the object and executable files in secondary storage.
If there is any illegal instruction in the source code, compiler lists all the errors during
compilation.
Interpreter
The basic purpose of interpreter is same as that of complier. In compiler, the program is
translated completely and directly executable version is generated. Whereas interpreter translates
each instruction, executes it and then the next instruction is translated and this goes on until end
of the program. In this case, object code is not stored and reused. Every time the program is
executed, the interpreter translates each instruction freshly. It also has program diagnostic
capabilities. However, it has some disadvantages as below:
Instructions repeated in program must be translated each time they are executed.
Because the source program is translated fresh every time it is used, it is slow process or
execution takes more time. Approx. 20 times slower than complier.
Assembler:
Assembler is a computer program which is used to translate program written in Assembly Language
in to machine language. The translated program is called as object program. Assembler checks each
instruction for its correctness and generates diagnostic messages, if there are mistakes in the
program. Various steps of assembling are:
Input source program in Assembly Language through an input device.
Use Assembler to produce object program in machine language.
Execute the program.
Difference between compiler and Interpreter:
COMPILER ASSEMBLER
Generates the assembly language code or Generates the relocatable machine code.
directly the executable code.
Preprocessed source code. Assembly language code.
The compilation phases are lexical analyzer, Assembler makes two passes over the given
syntax analyzer, semantic analyzer, input.
intermediate code generation, code
optimization, code generation.
The assembly code generated by the compiler The relocatable machine code generated by an
is a mnemonic version of machine code. assembler is represented by binary code.
Errors
While writing C programs, errors also known as bugs in the world of programming may occur
unwillingly which may prevent the program to compile and run correctly as per the expectations
of the programmer.
Basically 4 different types:
1. Runtime Errors
Runtime errors are those errors that occur during the execution of a c program and generally
occur due to some illegal operations that performed in the program.
Examples of some illegal operations that may produce runtime errors are:
2. Compile Errors
Compile errors are those errors that occur at the time of compilation of the program. C compile
errors may be further classified as:
Syntax Error
When the rules of the c programming languages are not followed, the compiler will show
syntax errors.
For example, consider the statements,
int a,b:
The above statement will produce syntax error as the statement is terminated with :
rather;
Semantic Errors
Semantic errors are reported by the compiler when the statements written in the c
program are not meaningful to the compiler.
Logical errors are the errors in the output of the program. The presence of logical errors leads to
undesired or incorrect output and are caused due to error in the logic applied in the program to
produce the desired output.
Also, logical errors could not be detected by the compiler, and thus, programmers has to check
the entire coding of a c program line by line.
4. Latent Errors
The hidden errors that shows up only when a particular set of data is used.
Example: r=(x+y)/(p-q)
This expression shows up error when p=q.
Debugging
It is the process of isolating and correcting different type of errors. Different debugging
techniques are given below.
1. Error Isolation
2. Tracing
In this technique, printf() statement is used to print the values of some important
variables at different stages of program.
If any value is incorrect, we can easily fix the location of the error.
3. Watch Values
A watch value is the value of variable or an expression, which is displayed continuously
as the program executes.
4. Breakpoints
Testing
It is the process of executing a program or system with the intent of finding errors. It involves
any activity aimed at evaluating an attribute or capability of a program or system and
determining that it meets its required results. Testing is usually performed for the following
purposes:
To improve quality.
For verification and validation.
For reliability estimation.
Testing process may include the following two stages:
Human Testing
It is an effective error detection process and is done before the computer based testing
begins.
It includes code inspection by the programmer and test group and review by a peer group.
The test is carried out statement by statement and is analyzed with respect to checklist of
common programming errors.
In addition to finding the errors, the programming style and choice of algorithm are also
reviewed.
It involves two stages namely compile testing and run time testing.
Compile testing can show different syntax errors and run time errors may produce the run
time error message such as null pointer assignment and stack overflow.
After removing errors, it is required to run the program with test data to check whether
the program is producing the correct result or not.
Program testing can be done either at module (function) level i.e. unit testing or program
level .
An integration testing is done to find the errors associated with interfacing.
Testing Debugging
The purpose of debugging is to correct those bugs
The purpose of testing is to find bugs and errors. found during testing.
It can be done by outsider like client. It must be done only by insider i.e. programmer.
Most of the testing can be done without design Debugging can’t be done without proper design
knowledge. knowledge.
A program analysis document with objectives, inputs, outputs and processing procedures.
Program design document algorithm and detailed flowchart and other appropriate
diagram.
Program verification documents, with details of checking, testing and correction
procedures along with the list of test data.
Log is used to document future program revision and maintenance activity.
Write an algorithm to find the largest among three different numbers entered by user.
Step 1: Start
Step 2: Declare variables a,b and c.
Step 3: Read variables a,b and c.
Step 4: If a>b
If a>c
Display a is the largest number.
Else
Display c is the largest number.
Else
If b>c
Display b is the largest number.
Else
Display c is the greatest number.
Step 5: Stop
Flowcharts
Draw a flowchart to add two numbers entered by user.