Anda di halaman 1dari 5

Lesson 1 Programming Methodologies

Programming methodologies deal with different methods of designing programs. This will teach you how to program efficiently. This book restricts itself to the basics of programming in C and C++, by assuming that you are familiar with the syntax of C and C++ and can write, debug and run programs in C and C++. Discussions in this chapter outline the importance of structuring the programs, not only the data pertaining to the solution of a problem but also the programs that operates on the data. Data is the basic entity or fact that is used in calculation or manipulation process. There are two types of data such as numerical and alphanumerical data. Integer and floating-point numbers are of numerical data type and strings are of alphanumeric data type. Data may be single or a set of values, and it is to be organized in a particular fashion. This organization or structuring of data will have profound impact on the efficiency of the program.


Data structure is the structural representation of logical relationships between elements of data. In other words a data structure is a way of organizing data items by considering its relationship to each other. Data structure mainly specifies the structured organization of data, by providing accessing methods with correct degree of associativity. Data structure affects the design of both the structural and functional aspects of a program. Algorithm + Data Structure = Program Data structures are the building blocks of a program; here the selection of a particular data structure will help the programmer to design more efficient programs as the complexity and volume of the problems solved by the computer is steadily increasing day by day. The programmers have to strive hard to solve these problems. If the problem is analyzed and divided into sub problems, the task will be much easier i.e., divide, conquer and combine. A complex problem usually cannot be divided and programmed by set of modules unless its solution is structured or organized. This is because when we divide the big problems into sub problems, these sub problems will be programmed by different programmers or group of programmers. But all the programmers should follow a standard structural method so as to make easy and efficient integration of these modules. Such type of hierarchical structuring of program modules and sub modules should not only reduce the complexity and control the flow of program statements but also promote the proper structuring of information. By choosing a particular structure (or data structure) for the data items, certain data items become friends while others loses its relations.


Modular Programming is heavily procedural. The focus is entirely on writing code (functions). Data is passive in Modular Programming. Any code may access the contents of any data structure passed to it. (There is no concept of encapsulation.) Modular Programming is the act of designing and writing programs as functions, that each one performs a single well-defined function, and which have minimal interaction between them. That is, the content of each function is cohesive, and there is low coupling between functions. Modular Programming discourages the use of control variables and flags in parameters; their presence tends to indicate that the caller needs to know too much about how the function is implemented. It encourages splitting of functionality into two types: Master functions controls the program flow and primarily contain calls to Slave functions that handle low-level details, like moving data between structures. Two methods may be used for modular programming. They are known as top-down and bottom-up, which we have discussed in the above section. Regardless of whether the top-down or bottom-up method is used, the end result is a modular program. This end result is important, because not all errors may be detected at the time of the initial testing. It is possible that there are still bugs in the program. If an error is discovered after the program supposedly has been fully tested, then the modules concerned can be isolated and retested by them. Regardless of the design method used, if a program has been written in modular form, it is easier to detect the source of the error and to test it in isolation, than if the program were written as one function.


The principles of top-down design dictates that a program should be divided into a main module and its related modules. Each module should also be divided into sub modules according to software engineering and programming style. The division of modules processes until the module consists only of elementary process that are intrinsically understood and cannot be further subdivided.


Bottom-up algorithm design is the opposite of top-down design. It refers to a style of programming where an application is constructed starting with existing primitives of the programming language, and constructing gradually more and more complicated features, until the all of the application has been written. That is, starting the design with specific modules and build them into more complex structures, ending at the top. The bottom-up method is widely used for testing, because each of the lowest-level functions is written and tested first. This testing is done by special test functions that call the low-level functions, providing them with different parameters and examining the results for correctness. Once lowest-level functions have been tested and verified to be correct, the next level of functions may be tested. Since the lowest-level functions already have been tested, any detected errors are probably due to the higher-level functions. This process continues, moving up the levels, until finally the main function is tested.


It is a programming style; and this style of programming is known by several names: Procedural decomposition, Structured programming, etc. Structured programming is not programming with structures but by using following types of code structures to write programs: 1. Sequence of sequentially executed statements 2. Conditional execution of statements (i.e., if statements) 3. Looping or iteration (i.e., for, do...while, and while statements) 4. Structured subroutine calls (i.e., functions) In particular, the following language usage is forbidden: GoTo statements Break or continue out of the middle of loops Multiple exit points to a function/procedure/subroutine (i.e., multiple return

statements) Multiple entry points to a function/procedure/subroutine/method In this style of programming there is a great risk that implementation details of many data structures have to be shared between functions, and thus globally exposed. This in turn tempts other functions to use these implementation details; thereby creating unwanted dependencies between different parts of the program. The main disadvantage is that all decisions made from the start of the project depends directly or indirectly on the high-level specification of the application. It is a wellknown fact that this specification tends to change over a time. When that happens, there is a great risk that large parts of the application need to be rewritten.


After designing an algorithm, it has to be checked and its correctness needs to be predicted; this is done by analyzing the algorithm. The algorithm can be analyzed by tracing all step-by-step instructions, reading the algorithm for logical correctness, and testing it on some data using mathematical techniques to prove it correct. Another type of analysis is to analyze the simplicity of the algorithm. That is, design the algorithm in a simple way so that it becomes easier to be implemented. However, the simplest and most straightforward way of solving a problem may not be sometimes the best one. Moreover there may be more than one algorithm to solve a problem. The choice of a particular algorithm depends on following performance analysis and measurements : 1. Space complexity 2. Time complexity 1.8.1. SPACE COMPLEXITY Analysis of space complexity of an algorithm or program is the amount of memory it needs to run to completion. Some of the reasons for studying space complexity are: 1. If the program is to run on multi user system, it may be required to specify the amount of memory to be allocated to the program. 2. We may be interested to know in advance that whether sufficient memory is available to run the program. 3. There may be several possible solutions with different space requirements. 4. Can be used to estimate the size of the largest problem that a program can solve.


Data structures are broadly divided into two : 1. Primitive data structures : These are the basic data structures and are directly operated upon by the machine instructions, which is in a primitive level. They are integers, floating point numbers, characters, string constants, pointers etc. These primitive data structures are the basis for the discussion of more sophisticated (non-primitive) data structures in this book. 2. Non-primitive data structures : It is a more sophisticated data structure emphasizing on structuring of a group of homogeneous (same type) or heterogeneous (different type) data items. Array, list, files, linked list, trees and graphs fall in this category.

1.13. ARRAYS
Arrays are most frequently used in programming. Mathematical problems like matrix, algebra and etc can be easily handled by arrays. An array is a collection of homogeneous data elements described by a single name. Each element of an array is referenced by a subscripted variable or value, called subscript or index enclosed in parenthesis. If an element of an array is referenced by single subscript, then the array is known as one

dimensional array or linear array and if two subscripts are required to reference an element, the array is known as two dimensional array and so on. Analogously the arrays whose elements are referenced by two or more subscripts are called multi dimensional arrays. 1.13.1. ONE DIMENSIONAL ARRAY One-dimensional array (or linear array) is a set of n finite numbers of homogenous data elements such as : 1. The elements of the array are referenced respectively by an index set consisting of n consecutive numbers. 2. The elements of the array are stored respectively in successive memory locations. n number of elements is called the length or size of an array. The elements of an array A may be denoted in C as A[0], A[1], A[2], ...... A[n 1]. The number n in A[n] is called a subscript or an index and A[n] is called a subscripted variable. If n is 10, then the array elements A[0], A[1]......A[9] are stored in sequential memory locations as follows : A[0] A[1] A[2] ...... A[9] In C, array can always be read or written through loop. To read a one-dimensional array, it requires one loop for reading and writing the array, for example: For reading an array of n elements for (i = 0; i < n; i ++) scanf (%d,&a[i]); For writing an array for (i = 0; i < n; i ++) printf (%d, &a[i]);

A vector is a one-dimensional ordered collection of numbers. Normally, a number of contiguous memory locations are sequentially allocated to the vector. A vector size is fixed and, therefore, requires a fixed number of memory locations. A vector can be a column vector which represents a n by 1 ordered collections, or a row vector which represents a 1 by n ordered collections.

1.15. LISTS
As we have discussed, an array is an ordered set, which consist of a fixed number of elements. No deletion or insertion operations are performed on arrays. Another main disadvantage is its fixed length; we cannot add elements to the array. Lists overcome all the above limitations. A list is an ordered set consisting of a varying number of elements to which insertion and deletion can be made. A list represented by displaying the relationship between the adjacent elements is said to be a linear list. Any other list is said to be non linear. List can be implemented by using pointers.