Unit 2
Unit 2
com
Programming Technique?
Programming technique is a systemic approach to analysis the problem and write the
corresponding code in any platform to solve the problems. There are different approach to write a
program which include:
Top down and Bottom up approach
Cohesion and Coupling
Structured and Unstructured Programming
Deterministic and Non-deterministic technique
Iterative and recursive logic
Modular designing and programming
Advantages:
1
C Programming – Unit 2 BCA-TU [email protected]
At each step of refinement new parts will become less complex and therefore easier to solve.
Parts of solution may turn out to be reusable.
Breaking problems into parts allows more than one person to solve the problem.
Bottom-up
Bottom-up programming implements the modules that provide specific functionality first
and then integrates them by implementing the more general modules.
Most programs are developed using a combination of these strategies.
In this design, individual parts of the system are specified in details. The parts are the linked
to form larger components, which are in turn linked until a complete system is formed.
Object oriented language such as C++ or java uses bottom up approach where each object
is identified first.
Advantages:
Make decisions about reusable low level utilities then decide how there will be put together
to create high level construct.
2
C Programming – Unit 2 BCA-TU [email protected]
In this top function of system might In this sometimes we cannot build a program from
7. be hard to identify. the piece we have started.
3
C Programming – Unit 2 BCA-TU [email protected]
This paradigm was used in earlier versions of BASIC, COBOL, and FORTRAN.
Unstructured programming languages have a limited number of data types like
numbers, arrays, string.
Difference between Structured and Unstructured Programming
Structured vs Unstructured Programming
Readability
Structured Programming based programs are easy to Unstructured Programming based programs are hard
read. to read.
Purpose
Structured Programming is to make the code more Unstructured programming is just to program to solve
efficient and easier to understand. the problem. It does not create a logical structure.
Complexity
Application
Structured programming can be used for small and Unstructured programming is not applicable for
medium scale projects. medium and complex projects.
Modification
Data Types
Code Duplication
It is easy to do testing and debugging in Structured It is hard to do testing and debugging in Unstructured
Programming. programming.
4
C Programming – Unit 2 BCA-TU [email protected]
5
C Programming – Unit 2 BCA-TU [email protected]
In this case, modules are subordinates to different modules. Therefore, no direct coupling.
Data Coupling: When data of one module is passed to another module, this is called data
coupling.
Stamp Coupling: Two modules are stamp coupled if they communicate using composite
data items such as structure, objects, etc. When the module passes non-global data structure
or entire structure to another module, they are said to be stamp coupled. For example,
passing structure variable in C or object in C++ language to a module.
Control Coupling: Control Coupling exists among two modules if data from one module
is used to direct the structure of instruction execution in another.
External Coupling: External Coupling arises when two modules share an externally
imposed data format, communication protocols, or device interface. This is related to
communication to external tools and devices.
Common Coupling: Two modules are common coupled if they share information through
some global data items.
Content Coupling: Content Coupling exists among two modules if they share code, e.g.,
a branch from one module into another module.
6
C Programming – Unit 2 BCA-TU [email protected]
Cohesion
In computer programming, cohesion defines to the degree to which the elements of a
module belong together. Thus, cohesion measures the strength of relationships between
pieces of functionality within a given module. For example, in highly cohesive systems,
functionality is strongly related.
Cohesion is an ordinal type of measurement and is generally described as "high cohesion"
or "low cohesion."
7
C Programming – Unit 2 BCA-TU [email protected]
Temporal Cohesion: When a module includes functions that are associated by the fact
that all the methods must be executed in the same time, the module is said to exhibit
temporal cohesion.
Logical Cohesion: A module is said to be logically cohesive if all the elements of the
module perform a similar operation. For example Error handling, data input and data
output, etc.
Coincidental Cohesion: A module is said to have coincidental cohesion if it performs a
set of tasks that are associated with each other very loosely, if at all.
Differentiate between Coupling and Cohesion
Coupling Cohesion
Coupling is also called Inter-Module Binding. Cohesion is also called Intra-Module Binding.
Coupling shows the relationships between Cohesion shows the relationship within the module.
modules.
Coupling shows the Cohesion shows the module's relative functional strength.
relative independence between the modules.
While creating, you should aim for low While creating you should aim for high cohesion, i.e., a cohesive
coupling, i.e., dependency among modules component/ module focuses on a single function (i.e., single-
should be less. mindedness) with little interaction with other modules of the
system.
In coupling, modules are linked to the other In cohesion, the module focuses on a single thing.
modules.
Deterministic vs non-deterministic
In the context of programming, an Algorithm is a set of well-defined instructions in sequence to
perform a particular task and achieve the desired output. Here we say set of defined instructions
which means that somewhere user knows the outcome of those instructions if they get executed in
the expected manner.
On the basis of the knowledge about outcome of the instructions, there are two types of algorithms
namely − Deterministic and Non-deterministic Algorithms. Following are the main differences
between both of the algorithms −
8
C Programming – Unit 2 BCA-TU [email protected]
Definition The algorithms in which the result of every algorithm On other hand, the algorithms in which the
is uniquely defined are known as the Deterministic result of every algorithm is not uniquely
Algorithm. In other words, we can say that the defined and result could be random are
deterministic algorithm is the algorithm that performs known as the Non-Deterministic
fixed number of steps and always get finished with an Algorithm.
accept or reject state with the same result.
Execution In Deterministic Algorithms execution, the target On other hand in case of Non-
machine executes the same instruction and results Deterministic Algorithms, the machine
same outcome which is not dependent on the way or executing each operation is allowed to
process in which instruction get executed. choose any one of these outcomes subjects
to a determination condition to be defined
later.
Type On the basis of execution and outcome in case of On other hand Non deterministic
Deterministic algorithm, they are also classified as algorithm are classified as non-reliable
reliable algorithms as for a particular input algorithms for a particular input the
instructions the machine will give always the same machine will give different output on
output. different executions.
Execution As outcome is known and is consistent on different On other hand as outcome is not known
Time executions so Deterministic algorithm takes and is non-consistent on different
polynomial time for their execution. executions so Non-Deterministic
algorithm could not get executed in
polynomial time.
Execution In deterministic algorithm the path of execution for On other hand in case of Non-
path algorithm is same in every execution. Deterministic algorithm the path of
execution is not same for algorithm in
every execution and could take any
random path for its execution.
Example The function AVG return always returns the same The GETDATE function which function,
result given the qualification stated above which results the current date time value,
always returns the different result.
The Recursion and Iteration both repeatedly execute the set of instructions. Recursion is when a
statement in a function calls itself repeatedly. The iteration is when a loop repeatedly executes
9
C Programming – Unit 2 BCA-TU [email protected]
until the controlling condition becomes false. The primary difference between recursion and
iteration is that recursion is a process, always applied to a function and iteration is applied to
the set of instructions which we want to get repeatedly executed.
Recursion
Applicatio
n For functions. For loops.
Terminati Through base case, where there will be no When the termination condition for the
on function call. iterator ceases to be satisfied.
Used when code size needs to be small, and Used when time complexity needs to be
Usage time complexity is not an issue. balanced against an expanded code size.
10
C Programming – Unit 2 BCA-TU [email protected]
Time
Complexit Very high (generally exponential) time Relatively lower time complexity (generally
y complexity. polynomial-logarithmic).
Modular programming is the process of subdividing a computer program into separate sub-
programs. A module is a separate software component. It can often be used in a variety of
applications and functions with other components of the system.
Some programs might have thousands or millions of lines and to manage such programs
it becomes quite difficult as there might be too many of syntax errors or logical errors
present in the program, so to manage such type of programs concept
of modular programming approached.
Each sub-module contains something necessary to execute only one aspect of the desired
functionality.
Modular programming emphasis on breaking of large programs into small problems to
increase the maintainability, readability of the code and to make the program handy to
make any changes in future or to correct the errors.
11
C Programming – Unit 2 BCA-TU [email protected]
Communication among different modules of the code for proper execution of the entire
program.
The benefits of using modular programming include:
12