Sie sind auf Seite 1von 8

MT 512: Programming Design Page no: 38

4. PROGRAMMING APPROACHES

Organizations and individuals are continually searching for more efficient ways to
perform the software development process.

One-way of reducing the time and cost of development is to standardize software
programs and the programming process. The benefits of standardized programs are that
they are easier to code, maintain, debug, and modify.

In recent years, a variety of techniques have appeared attempting to minimize differences
in the way programmers' design and develop software. A few of the most commonly used
techniques for standardization are described in this lesson.


Programming Approaches: Nonstructured Vs. Structured Approaches

Structured programming is a standardization technique used for software development.
This approach works by having all programmers use the same structured design
techniques. Structured programming was invented to address the shortcomings of non-
structured programming, which frequently employed GO TO branch points to transfer
from one part of the program to another part. Using GO TO codes, one could transfer
backward, forward, or anywhere else within the program. The problem is that the
connections between parts of the program by using GO TO commands can become quite
haphazard.

The haphazard and sometimes convoluted pattern of linkages between parts of the
program has been called spaghetti code. This type of programming is difficult to
understand and debug. Non-structured programming of this nature is now viewed as an
ineffective programming strategy.

To develop good software, developers have to carefully think out and design the
programs. In the earliest days of computing, programmers wrote software according to
their own whims, with the result that programs were often confusing and difficult to work
with. Software today is expected to follow recognised design principles. The prevailing
design standards are structured programming and structured design.


4.1 STRUCTURED PROGRAMMING

Structured programming makes use of the control structures (sequence, selection and
repetition). Structured programming does not use GO TO commands. The sequence
principle implies that program instructions should be executed in the order in which they
appear. The selection principle implies that instructions may be executed selectively
using IF-THEN and/or IF-THEN-ELSE statements. These statements work in the
following way. IF a condition is met or is true, THEN a specific set of instructions will be
executed. If the condition is false, then another set of instructions will be executed.
MT 512: Programming Design Page no: 39

For example, IF an employee works 40+ hours a week, THEN calculate gross pay based
on an overtime rate of time and a half. If the employee does not work 40+ hours a week,
THEN calculate gross pay based on the normal hourly pay rate. IF-THEN-ELSE works in
a similar way, but in this case the word ELSE is substituted for a false case, a condition
that is not met. IF the employee works 40+ hours a week, then calculate gross pay base
on a time and a half pay rate, ELSE calculate gross pay based on the normal rate.
Alternatively when there are many options, one can employ the CASE statement.

The iteration principle indicates that one part of the program can be repeated or iterated a
limited number of times. In most computer languages, the iteration may be activated by
using REPEAT ---UNTIL or using the WHILE loop and the FOR loop.

4.2 STRUCTURED DESIGN

According to structured design principles, a program should be designed from the top-
down or bottom-up as a hierarchical series of modules. A module is a logical way of
partitioning or subdividing a program so that each module performs one or a small
number of related tasks.


4.2.1 Modular Programming

The Modular Approach to programming involves breaking a program down into
subcomponents called modules. Each module is composed of an independent or self-
contained set of instructions. Modules are also referred to as routines, subroutines, or
subprograms or procedures. Each module is designed to perform a specific task in the
overall program, such as to calculate the gross pay of an employee in a payroll program.

The advantage of modular programming is the ability to write and test each module
independently and in some cases reuse modules in other programs. A program consists of
multiple modules. In addition, there is a main module in the program that executes the
other modules.

You can use the following approaches in order to design a program consisting of
modules.

4.2.2 Top-Down Design or Top-Down Decomposition

One programming approach that has proven to be most productive is called top-down
decomposition. Top-down decomposition is the process of breaking the overall procedure
or task into component parts (modules) and then subdivide each component module until
the lowest level of detail has been reached. It is called top-down design or top-down
decomposition since we start "at the top" with a general problem and design specific
solutions to its sub problems. In order to obtain an effective solution for the main
MT 512: Programming Design Page no: 40
problem, it is desirable that the subproblems (subprograms) should be independent of
each other. Then each sub-problem can be solved and tested by itself.

The top level of the decomposition is the level of the overall plan. Unfortunately there is
no single formula that will decompose a complex problem into individual tasks. The
strategy involves top-down reduction of the processing until a level is reached where
each of the individual processes consists of one self-contained task, which is relatively
easy to understand and can be programmed in a few instructions.

This stepwise process may create multiple layers or levels of modules beginning with a
main module at the top. The second level might contain intermediate modules, and the
third level minor modules. The program is developed from the top in stepwise fashion.

Using this method, a complex problem is separated into simpler parts, which can be
programmed easily. At each stage, the instructions or steps can be checked for errors in
logic, corrected or modified without any effect on the other subprograms. The result will
be a truly modular program that satisfies the requirement, which says a good program
can be modified easily.

This programming strategy focuses on developing a software program conceptually
before coding begins. A programming team will likely create a diagram that looks much
like an organisational chart with the main module at the top and subordinate modules
down below connected by lines with each box representing a program module. The chart
shows how modules relate to each other but does not depict the details of the program
instructions in each module. The structure chart is usually referred to as a Hierarchical
Program Organisation (HIPO)

Example top-down decomposition

The payroll system of a company can contain the following modules or tasks

Master file
Earnings
Deductions
Taxing
Net earning
Print reports


The tasks given above can be depicted in a hierarchical chart (Hierarchical Program
Organisation) as shown below.





MT 512: Programming Design Page no: 41



















Identifying and diagramming the relationship between modules in this fashion allows
programmers to focus on the overall organisation and logic of the program without
getting bogged down in the details. Once the overall structure of the program is finalised,
detail coding of individual modules can proceed.

It is the set of principles that enable a problem to be solved by breaking it down into
manageable parts, step-by-step from the overall problem specification, in stages through
to the actual code.

4.2.3 Bottom-up Design
Already existing facilities/designs are used/taken into consideration as a model for a new
(better) design. Using Bottom-up design strategy, we take an already existing computer
program as a model for our new program. We try to utilise the existing facilities or design
in a way, which gives out program a better performance.

4.2.3 Stepwise Refinement
Stepwise refinement is a top-down design strategy. The program is developed by
successively refining levels of procedural detail. In each step of the refinement, one or
several instructions of the given program are decomposed into more detailed instructions.
This successive decomposition or refinement of specifications terminates when all
instructions are expressed in terms of any underlying computer or programming
language.

Every refinement step implies some design decisions. It is important that the programmer
is aware of the underlying criteria.


PAYROLL PROBLEM
Master File Earnings Deductions Taxing
Create
Master
File
Update
Master
File
Create
Earning
File
Update
Earning
File
Create
Deduction
File
Update
Deduction
File
Create or
update Tax
File
MT 512: Programming Design Page no: 42
4.3 SUB-PROGRAMS
In top down design, the problem is broken down into sub-problems. The main problem is
solved by the corresponding main program. Solutions to the sub-problems are provided
by subprograms, known as procedures, functions, or subroutine depending on the
programming language. A subprogram performs or executes the computer instructions
required to solve a given subproblem.










4.3.1 User defined Sub Programs and Functions
A Sub Program is a part of a program that performs one or more related tasks, has its own
name, is written as a separate part of the program, and is accessed via a call statement.

A Function returns a value to the program and shares many of the same characteristics as
a Sub Program (e.g., has its own name, is written as a separate part of the program).

Pros and Cons of Using Procedures
Disadvantage -- Procedure calls consume execution time and memory.
Advantage -- Procedures make the program easier to write, test, debug and
maintain.

Within a program we can normally identify sub-tasks that are performing a specific
function. Where program features clearly identify sub-tasks it is good practice to
implement each sub-task in its own, stand-alone, sub-program.

The advantages of a separate sub-program are considerable:

A stand-alone sub-program can be written and tested independently.
Once written, the sub-program can be called a number of times within a
program.
The sub-program is referred to by a meaningful name chosen by the
programmer, which helps greatly in understanding the code.
The sub-program can also be used in subsequent programs.

The ability to reuse a sub-program is obviously useful. We can include an existing
subprogram in another program whenever you need it.



Main Problem
Subproblem
A
Subproblem
B
Subproblem
C
Main Program
Sub-
program A
Sub-
program B
Sub-
Program C
MT 512: Programming Design Page no: 43
4.3.2 Procedures and Functions
A function or procedure is a sub-program that exists to perform a specific, single task. It
has its own inputs and its own outputs (often called parameters), it can also define its own
variables. It may return the values to the referencing or invoking program or procedure A
function is a special sort of procedure that has a single output.

Procedures
A procedure has a header, followed by declarations of variables used within the
procedure known as local variables (identifiers) and finally statements that are to be
performed when the procedure is invoked.

The procedure header is the mechanism used to give a name to the procedure. The
procedure header also may contain the values that can be communicated between the
procedure and its invocation known as parameters.

The general structure for defining a procedure (in C++) is:
void Procedure_Name(parameter)
{
.
//List of Statements.
}
Example
void WelcomeNote ()
{
Cout<<Welcome to Programming Concepts;
}

Procedures in c++ start with the keyword void.

Functions
The general structure for defining a function (in C++) is:
Data_type Function_Name(parameter)
{
.
.
Return statement;
}
Example
float Cube (float x)
{
float product;
product = x*x*x;
return product;
}
The principle thing to note is that the function must have a result type which dictates
what type of value the function represents.
MT 512: Programming Design Page no: 44

A function is a special sort of sub-program - it is used when a sub-program is needed
which takes a number of inputs and returns a single output value (as with cube above).
This is typical of many algebraic functions (sine, cosine, exp, log, cube) but is otherwise
fairly unusual since the sub-programs that we want to define quite often have either no
output at all (as with manythanks) or very many outputs - in these cases we use a
procedure.

Parameters
As weve seen, procedures and functions can take a list of quantities called parameters.
Initially you might like to regard the parameters as the inputs to the module (but this is
not always true, as we shall see). For example:

void sum_and_difference( float a, float b)
{
float total, difference;
total =a+b
difference =a-b
cout<<The sum is <<total;
cout<<The difference is <<difference;
}
Both total and difference are local variables and only exist within the procedure, as soon
as the procedure finishes they and their values disappear.

The following program is an example of the main program:
void main()
{
float val1, val2;
val1=2;
val2=4;
sum_and_difference(val1,val2);
cout<< val1;
}
The values of val1 and val2 are simply copied into the variables a and b of the procedure
sum_and_difference. At the end of the procedure the values are not copied back!!!! The
value of val1 that the final output statement displays is therefore 2 (not 0).

Sometimes however, we would like the values to be copied back at the end of the
procedure. This is particularly true if there is more than one output of the procedure, as in
the case of sum_and_difference. The sum and difference cannot both be passed back as
the return value of a function, since a function can return only a single value.
void sum_and_difference( float a, float b, float & s, float & d)
{
s =a+b;
d =a-b;
}
MT 512: Programming Design Page no: 45

void main()
{
float val1, val2;
float total, difference;

val1=2;
val2=4;
sum_and_difference(val1,val, total, difference);
cout<<The sum is <<total;
cout<<The difference is <<difference;
}

The symbol & in the procedure parameter list instructs the computer to copy back any
changes to the values of a and b once the procedure has completed.

In summary, if you want to write a sub-program that has a number of inputs and a single
output, write a function. If you want many outputs from the sub-programs, use a number
of var parameters in a procedure


4.3.3 Built in functions
While most programming languages enable you to write your own functions (as above),
most also provide a library of common functions ready for you to use (e.g. sine, cosine).

Exercise
1. Devise the pseudcode for a program that will accept names and marks of each student in
the class of 100 students, and then calculate the average of all marks. The program
should make sure the entered marks entered are valid (in the range of 0 and 100). Use
the idea of modular programming.
2. Write pseudcode for a program that will read two matrices and then display their sum.
3. The scalar product (also called the inner product or the dot product) of the two
vectors (one-dimensional arrays) A and B with n elements is defined as

Write a pseudocode program, which references a Function with three parameters, A,
B, N. The main program should input N and the two arrays A and B. The Function
subprogram should have a name called Product and it should compute the scalar
product according to the formula given

=
+ + + = = =
n
i
n n i i
b a b a b a b a B A product
1
2 2 1 1
... .

Das könnte Ihnen auch gefallen