0% found this document useful (0 votes)
39 views12 pages

Unit 2

C programming techniques

Uploaded by

Online User
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views12 pages

Unit 2

C programming techniques

Uploaded by

Online User
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

C Programming – Unit 2 BCA-TU ict.instructor90@gmail.

com

Unit-2: Programming Technique

Introduction to Programming Technique

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

Fig: Example of software module


Top down and bottom up approach
Top-Down:
 Top-down programming starts by implementing the most general modules and works toward
implementing those that provide specific functionality.
 In top down model, an overview of system is formulated without going into details for any part of
it. Each part of it then refined into more details, defining it in yet more details until the entire
specification is detailed enough to validate the model.

Advantages:

 Breaking problems into parts help us to identify what needs to be done.

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.

Difference between top down approach & bottom up approach

S.no. Top down approach Bottom up approach

In this approach We focus on


breaking up the problem into smaller In bottom up approach, we solve smaller problems
1. parts. and integrate it as whole and complete the solution.

Mainly used by structured


programming language such as Mainly used by object oriented programming
2. COBOL, FORTAN, C etc. language such as C++, C#, Python.

2
C Programming – Unit 2 BCA-TU [email protected]

S.no. Top down approach Bottom up approach

Each part is programmed separately Redundancy is minimized by using data


3. therefore contain redundancy. encapsulation and data hiding.

In this the communications is less


4. among modules. In this module must have communication.

It is used in debugging, module


5. documentation, etc. It is basically used in testing.

In top down approach, decomposition


6. takes place. In bottom up approach composition takes place.

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.

In this implementation details may


8. differ. This is not natural for people to assemble.

Structured and Unstructured Programming


Structured Programming
 In Structured Programming, the code is divided into functions or modules. It is also known
as modular programming.
 Modules or functions are a set of statements which performs a sub task. As each task is a
separate module, it is easy for the programmer to test and debug.
 It is also easy to do modifications without changing the whole program. When changing
the code, the programmer has to concentrate only on the specific module.
 C language and Pascal are some examples of Structural Programming languages.
 A programming language like C can use user-defined functions. Functions are called by
the main program. Variables in the functions are called local variables, and global variables
can be accessed by all the functions.
 Structured programming languages also use selections (if/ else) and iterations (for /do,
while). The program in Figure 01 shows the functions using Structured programming
language C.
Un-Structured Programming
 In Unstructured Programming, the code is written as a single whole block.
 The whole program is taken as a single unit.
 It is harder to do changes in the program.

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

Structured Programming is a programming


Unstructured Programming is the paradigm in which
paradigm which divides the code into modules or
the code is considered as one single block.
function.

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

Structured Programming is easier because of Unstructured programming is harder when


modules. comparing with the structured programming.

Application

Structured programming can be used for small and Unstructured programming is not applicable for
medium scale projects. medium and complex projects.

Modification

It is hard to do modifications in Unstructured


It is easy to do changes in Structured Programming.
Programming.

Data Types

Unstructured programming has a limited number of


Structured programming uses many data types.
data types.

Code Duplication

Unstructured programming can have code


Structured programming avoids code duplication.
duplication.

Testing and Debug

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]

Coupling and Cohesion


Coupling
 In software engineering, the coupling is the degree of interdependence between software
modules.
 Two modules that are tightly coupled are strongly dependent on each other. However, two
modules that are loosely coupled are not dependent on each other.
 Uncoupled modules have no interdependence at all within them.
 A good design is the one that has low coupling.
 Coupling is measured by the number of relations between the modules.
 That is, the coupling increases as the number of calls between modules increase or the
amount of shared data is large. Thus, it can be said that a design with high coupling will
have more errors.

Types of Module Coupling

 No Direct Coupling: There is no direct coupling between M1 and M2.

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."

Types of Modules Cohesion

 Functional Cohesion: Functional Cohesion is said to exist if the different elements of a


module, cooperate to achieve a single function.
 Sequential Cohesion: A module is said to possess sequential cohesion if the element of a
module form the components of the sequence, where the output from one component of
the sequence is input to the next.
 Communicational Cohesion: A module is said to have communicational cohesion, if all
tasks of the module refer to or update the same data structure, e.g., the set of functions
defined on an array or a stack.
 Procedural Cohesion: A module is said to be procedural cohesion if the set of purpose of
the module are all parts of a procedure in which particular sequence of steps has to be
carried out for achieving a goal, e.g., the algorithm for decoding a message.

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]

Key Deterministic Algorithm Non-deterministic Algorithm

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.

Iterative and recursive logic

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

 Recursion uses selection structure.


 Infinite recursion occurs if the recursion step does not reduce the problem in a manner
that converges on some condition (base case) and Infinite recursion can crash the system.
 Recursion terminates when a base case is recognized.
 Recursion is usually slower than iteration due to the overhead of maintaining the stack.
 Recursion uses more memory than iteration.
 Recursion makes the code smaller.
Iteration

 Iteration uses repetition structure.


 An infinite loop occurs with iteration if the loop condition test never becomes false and
infinite looping uses CPU cycles repeatedly.
 An iteration terminates when the loop condition fails.
 An iteration does not use the stack so it's faster than recursion.
 Iteration consumes less memory.
 Iteration makes the code longer.

PROPERTY RECURSION ITERATION

Definition Function calls itself. A set of instructions repeatedly executed.

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.

Code Size Smaller code size Larger Code Size.

10
C Programming – Unit 2 BCA-TU [email protected]

PROPERTY RECURSION ITERATION

Time
Complexit Very high (generally exponential) time Relatively lower time complexity (generally
y complexity. polynomial-logarithmic).

Modular Approach in Programming

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.

Points which should be taken care of prior to modular program development:

 Limitations of each and every module should be decided.


 In which way a program is to be partitioned into different modules.

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:

 Less code has to be written.


 A single procedure can be developed for reuse, eliminating the need to retype the code
many times.
 Programs can be designed more easily because a small team deals with only a small part
of the entire code.
 Modular programming allows many programmers to collaborate on the same application.
 The code is stored across multiple files.
 Code is short, simple and easy to understand.
 Errors can easily be identified, as they are localized to a subroutine or function.
 The same code can be used in many applications.
 The scoping of variables can easily be controlled.

12

You might also like