16-01-2013, 04:50 PM
Structured programming
Structured programming.doc (Size: 239 KB / Downloads: 24)
C LANGUAGE
Structured programming (sometimes known as modular programming) is a subset of procedural programming that enforces a logical structure on the program being written to make it more efficient and easier to understand and modify. Certain languages such as Ada, Pascal, and dBASE are designed with features that encourage or enforce a logical program structure.
Structured programming frequently employs a top-down design model, in which developers map out the overall program structure into separate subsections. A defined function or set of similar functions is coded in a separate module or sub module, which means that code can be loaded into memory more efficiently and that modules can be reused in other programs. After a module has been tested individually, it is then integrated with other modules into the overall program structure.
This leads to programs written with only the following code structures:
• Sequence of sequentially executed statements.
• Conditional execution of statements (i.e., "if" statements).
• Looping.
• Structured Subroutine calls (e.g., 'gosub' but not 'goto').
• Stepwise Refinement
C is a structured programming language:-
C has facilities for structured programming and allows lexical variable scope and recursion, while a static type system prevents many unintended operations. In C, all executable code is contained within functions. Function parameters are always passed by value. Pass-by-reference is simulated in C by explicitly passing pointer values. Heterogeneous aggregate data types (struct) allow related data elements to be combined and manipulated as a unit. C program source text is free-format, using the semicolon as a statement terminator (not a delimiter).
Program Style
Programming Style refers to methods/rules of writing readable & understandable computer programs. Computer programming is an art of developing computer programs, which can be written in different styles to solve a given problem. That programming style is the best, which produces:
• Technically correct &
• Easily understandable programs
A good programming style is characterized by following:-
• Simplicity
• Easy to understand
• Good documentation
• Readability
• Easy to modify
• Consistent in input & output
• Modular
• Good structure
• Easy to test
Thus the main objective during coding phase is to write a good quality program & not just working program. For good programming style there are some rules & guidelines. These are:-
1. Clarity & Simplicity of expression:- While designing the programs the clarity & simplicity of the expression must be kept in mind. The clarity & simplicity of expression is not to be sacrificed at the cost of cleverness of implementation.
2. Names:- While writing a program, names should be meaningful. It should not be critical variable. Names should be closely related to the entity they represent & modules names should reflect their activity. This will make the code easier to read & maintain.
3. Control Constructs:- As much as possible single-entry, single exit constructs should be used. This means that the program starts from one defined point & the execution terminates at one defined point. Thus a program can be viewed as a sequence of statements.
4. Goto’s:- Use of goto’s should be avoided as much a possible. If a goto statement must be used then forward transfer is acceptable rather than the backward jump. Use of goto for existing a loop or for calling error handlers is acceptable if the language does not have any other alternatives.
5. Information hiding:- Information hiding is an important style for a good programming language. The principle of information hiding is that: the information captured in the data structures should be hidden from the rest of the system. The access functions on the data structures that represent the data operations, performed on information, should be visible. Information hiding should be supported where possible. It can reduce the coupling between modules & make the system more maintainable.
6. Module Interface:- If a module has complex interface, it should be carefully examined. Such types of modules are not functionally cohesive & are implementing multiple functions. Thus any module which has complex interface must be broken into multiple modules.
7. Nesting:- The control construct if-then-else can be nested. If the nesting becomes too deep, the programs become difficult to understand. Where possible try to avoid deep nesting in coding.
8. Module Size:- The module size should be uniform. The size of module should not be too big or too small. If the module size is too large it is not generally functionally cohesive. If the module size is too small, it leads to unnecessary overhead. Thus, the module size should be decided according to the principle of cohesive & coupling.
9. Program Layout:- Program should be organized in such a way so that it has more readability. It should be clear in the first attempt. Proper indentation, blank spaces, comments etc. should be used to increase the readability of the programs.
10. Robustness:- A robust program is one that produces either correct results or meaningful messages, irrespective of the fact how bad the input data may be. Many times, the input data fed to a program may be invalid & the program may not be able to process this data to produce the correct results & may result in abrupt termination of a program. After informing the user, a roust program handles such situations.
11. Internal Documentation:- Internal documentation helps to increase the readability & understanding. It should be given at appropriate place. It is used when maintenance & editing is done.
Problem Solving
Computer problem solving can be summed up in one word-it is demanding. It is an intricate process requiring much thought, careful planning, logical precision, persistence & attention to detail. At the same time it can be challenging, exciting, & satisfying experience with considerable room for personal creativity and expression. If computer problem solving is approached in this spirit then the chances of success are greatly amplified.
Problem solving is not a simple task. It involves a number of steps. The problem solving process sometimes called Programming process involves following steps:-
i. Problem Identification:- The first step is to identify & understand the problem. Once the problem is identified the inputs, processes, outputs, naming conventions are decided as computers cannot deal with subjective decisions but can only handle the problems that deal with data or Arithmetic, Boolean or Comparison operations.
ii. Task Analysis:- the basic crux of the task analysis is to understand the problem thoroughly. Task analysis can be divided into following groups:
1. the problem must be thoroughly understood.
2. a solution method is chosen, or developed.
3. the solution process is described step by step.
iii. Data Analysis:- this part is mainly concerned about the inputs required & outputs expected. It is initiated by finding out the expected outputs & then subsequently finding the inputs for each desired output. Once this is made clear the databases are designed, relationships are established & data is fed.
iv. Algorithm:- once a problem is defined, a detailed step-by-step procedure for solving it is known as algorithm. Algorithms can be written in ordinary language or using formal procedures. If the minute details are taken care of at the time of developing an algorithm, then a lot of time is saved during the programming process. In an algorithm, all items of instructions are listed in the order in which they are to be carried out. This is done with the help of flowcharts.
v. Flow charting:- once an algorithm is developed, another method to see the steps in a simpler manner is to denote it in pictorial form or the flow charts. It is nothing but diagrammatic representation of various steps involved in the solution. It acts as a map & pictorial depiction to assist the programmer in terms of writing & understanding. It serves as an effective means of communication, as analytical tool & the concise form of documentation.
vi. Coding:- the algorithm is to be converted into a formal computer language in terms of program called as coding. Coding refers to writing & debugging the programs for given specifications whereas programming includes the specification preparation & coding or writing the program as well. the text of program is referred to as code & lines of text are referred to as lines of code.
vii. Debugging & Testing:- finally the program coded should be free from errors and hence is tested for the smooth functioning.
Importance of flowchart in problem-solving on a computer:-
• Flowcharts provide better communication as the users can quickly & clearly get ideas & descriptions of algorithms.
• A clear overview of the complete problem & its algorithm is provided by a flowchart.
• A flowchart can easily be drawn in comparison to writing a program & testing it.
• All the major portions of a program are shown by a flowchart, so the accuracy in logic flow is maintained.
• A program can be easily coded in a programming language using a flowchart.
• A flowchart helps in modification of an already existing program without disrupting the program flow.
• A flowchart gives a permanent storage of program logic pictorially.