Programming Practice And Techniques file · Web viewIDB-BISEW IT Scholarship Project. Page 4 of 16....
Transcript of Programming Practice And Techniques file · Web viewIDB-BISEW IT Scholarship Project. Page 4 of 16....
Programming Practices and Techniques
IDB-BISEW IT Scholarship Project
Round -31
WPSI
Collected By
A H Shumon Mahmud
Programming Practice And TechniquesIDB-BISEW IT Scholarship
Project
What is program?
Collection of instructions to solve any particular problem.
What is programming process?
Collection of instructions to implement a process to solve a problem.
What are the steps of programming process?
1. Defining the problem.2. Preparing the algorithm.3. Preparing flow chart.4. Coding.5. Testing and Debugging6. Documenting.
What do you mean by Defining the problem?
1. What result to be produced?2. How output is to look like.3. Input must also be documented.4. How data is organized to facilitate processing.5. Complex calculations must also be defined in details as part of problem definition.
Preparing an algorithm:I. Can perform arithmetic operation
II. Can perform comparison and on the basis of result perform alternative action.III. Can store data in memoryIV. Can input data and output result.
Despite the above capabilities complex problem can be solved if you can figure out the exact sequence of steps. This sequence of steps is algorithm.
Example:
Problem: Determine how many positive, negative and zero numbers there are in a list of numbers.
Solution :
1. Set positive, negative and zero counters to zero.2. Read a number; if there are no more numbers, go to step 5.3. If the number is positive, add one to the positive counter and go to step 2.4. If the number is negative; add one to the negative counter and go to step 2.5. If the number is zero, add one to the zero counters and go to step 2.6. Print counters.
What is Flowchart?Flowchart is symbolic representation of algorithm.
Table of Flowchart Symbols
Page 1 of 17
Collected By
A H Shumon Mahmud
Programming Practice And TechniquesIDB-BISEW IT Scholarship
Project
Symbol Symbol Name (alias) Symbol Description
Process
Show a Process or action step. This is the most common symbol in both process flowcharts and business process maps.
Flow Line (Arrow, Connector) Flow line connectors show the direction that the process flows.
Terminator (Terminal Point, Oval)
Terminators show the start and stop points in a process.
Decision
Indicates a question or branch in the process flow. Typically, a Decision flowchart shape is used when there are 2 options (Yes/No, No/No-Go, etc.)
Connector (Inspection)
Flowchart: In flowcharts, this symbol is typically small and is used as a Connector to show a jump from one point in the process flow to another. Connectors are usually labeled with capital letters (A, B, AA) to show matching jump points.
Data (I/O)
The Data flowchart shape indicates inputs to and outputs from a process. As such, the shape is more often referred to as an I/O shape than a Data shape.
DisplayIndicates a process step where information is displayed to a person (e.g., PC user, machine operator).
Flowcharting guideline: Should be read the way a printed page is read: from top to bottom and from left to right. Every program flowchart starts with a single terminal outline & ends with one or more
terminal outlines. The descriptive names that identify data items beings used & the operations being
performed should be used consistently. The words used inside the outlines should be chosen so that they will have meaning for
anyone reading the flowchart. The contents of a decision outline should consist of the two items being compared. Each exit from a decision outline should be labeled with appropriate condition:
<,>,=,<=,>= or !=. Leave white space in the flowchart using connectors. Should be one flow line into an outline. The exception is decision outline and input
outline.
Example:
Draw a flowchart to find the sum of first 50 natural numbers.Draw a flowchart to find the largest of three numbers A,B, and C.Draw a flowchart for computing factorial N (N!)Draw a flowchart for adding two numbersDraw a flowchart to accept item name, price, and quantity. You need to calculate value
Page 2 of 17
Programming Practice And TechniquesIDB-BISEW IT Scholarship
Project
as the product of price and quantity, and display the calculated value and the item name using variables.Draw a flowchart to Check if the Age is NegativeDraw a flowchart to Count the Number of Odd and Even NumbersDraw a flowchart to display the Highest of any Ten NumbersDraw a flowchart to display the Average of First Ten Odd NumbersDraw a flowchart to display Fibonacci Series
What is Desk Checking?Desk-Checking is the procedure to know whether the algorithm produces correct result or
not.
Example:
Problem: Determine how many positive, negative and zero numbers there are in the list of following numbers. 16,3,-7,0,4,-6,-2,8
Solution :
1. Set positive, negative and zero counters to zero.2. Read a number; if there are no more numbers go to step 5.3. If the number is positive, add one to the positive counter and go to step 2.4. If the number is negative; add one to the negative counter and go to step 2.5. If the number is zero, add one to the zero counters and go to step 2.6. Print counters.
According to the above algorithm Desk-Checking generates the following table:
Step Number Positive Negative Zero1 None 0 0 02 16 0 0 03 16 1 0 02 3 1 0 03 3 2 0 02 -7 2 0 03 -7 2 0 04 -7 2 1 02 0 2 1 03 0 2 1 04 0 2 1 05 0 2 1 12 4 2 1 13 4 3 1 12 -6 3 1 13 -6 3 1 14 -6 3 2 12 -2 3 2 13 -2 3 2 14 -2 3 3 12 8 3 3 13 8 4 3 12 None 4 3 16 None 4 3 17 None 4 3 1
Coding & Programming Language
Page 3 of 17
Programming Practice And TechniquesIDB-BISEW IT Scholarship
Project
Coding: coding refers to the writing of instructions for the computer.
Programming Language: To give easy instruction to computer programming language is used.
High level Programming Language:
Most widely used Easy to learn Easy to understand and modify Programs written in one language
can be used in another language with minor modification.
Example:
1. BASIC(Beginners all-purpose symbolic instruction code):
Use in solving algebraic problem. Use to handle business data
processing. Widely used on personal computer.
2. COBOL(Common business oriented language):
Use for business data processing
Used on large and local computer.
Widely used on personal computer.
3. FORTAN(Formula Translator):
Designed for mathematical problem solving.
4. PASCAL:
It is a structured programming language
Used on most computers.
5. RPG || ():
Used for business application Used on smaller business
computer, personal computers and large computers.
6. Procedure Oriented Language:
Defines all the steps that must be followed.
Compiler is used to translate source program to object program.
BASIC, COBOL, FORTAN and Pascal are procedure oriented language.
7. Problem Oriented Language:
Describes problem but does not define all the steps that must be followed.
Generator program uses the problem descriptions in the source program to generate an object program.
RPG|| is Problem Oriented Language.
Low Level Language:
Difficult to learn and understand. Programs written in one type of
computer cannot be used on another computer without extensive version.
Not widely used for business data processing.
Page 4 of 17
Programming Practice And TechniquesIDB-BISEW IT Scholarship
Project
Example:
1 Assembler language:
Source programs are translated into object programs by an assembler program.
What about Debugging and testing?
Debugging and testing is done when a program has been coded by using
1. Compiler, generator or assembler2. Interpreter
Compiler, generator or assembler:
If a compiler, generator or assembler is used, the entire program is processed as a unit.
Checks for syntax error and produce a list of detected syntax error.
If syntax errors are detected then will go no further.
If there is no syntax error then check the generated output. If output is incorrect then programmer checks for logical error.
When the errors have been corrected the program is resubmitted to the Compiler, generator or assembler and the process of correcting the errors is repeated.
Interpreter:
Each instruction is checked as it is entered.
Instructions contain syntax error are corrected and reentered.
Instructions without errors are executed immediately.
Resulting output is examined for logical error.
Logical errors must be corrected and interpreted and executed again.
This process continues until all bugs have been removed from program.
No object program is produced.
What is Documenting?
Begins with the definition of the problem.
Must keep a written record of everything we do in creating a program.
Modification or further development is not possible if proper documentation is not available.
What about Structured Programming?
Structured Programming: Structured Programming is a collection of technique to increase programmer productivity by reducing the time required to write, test, debug and maintain program. It emphasizes careful and systematic planning of programs.
What is Modular Programming?
In modular programming, Program is broken down into module. Each of which performs a single, limited function and is written and debugged separately from other modules. Each program contains a main program module, which controls other sub module.
Advantage of Modular Programming:
1. Errors are reduced.2. Modules are independent, different
programmers can work simultaneously on different parts of the same program and reduce time to write program.
3. Modules can be changed without affecting other modules.
4. Easy to maintain the program.
What is Subroutines
Subroutine is a group of instructions that performs a limited processing task.
There are two types of subroutines :
1. Internal subroutine.2. External subroutine.
Internal subroutineI. Task performed by the
Page 5 of 17
Programming Practice And TechniquesIDB-BISEW IT Scholarship
Project
subroutine may be one that is required at more than one place in the program, but the code appears in the program only once.
II. The program transfers control to the subroutine when the task needs to be performed(call and return instructions).control is to be returned is stored in a registar.
III.Disadvantage :
IV. Slower execution.V. Some language impose strict
constraint on how data is passed to and from a subroutine
External subroutineI. External subroutines are used
for tasks that more than one program is likely to want performed.
II. Is stored in a library where it is available to any program requesting it.
III. Programmers should know its location, how to send and receive data from it.
IV.Advantage :
V. provide complex solution seldom needed by programmer
What about Top-Down programming approach ?
a) Technique in planning a modular program.
b) In Top-Down programming approach we first define the main program module, which initiates program execution, calls other modules to perform specific functions and then terminate the program.
c) Large and complex modules are broken into subordinate module.
What about Structure charts?a) It is planning tool in top down
programming.b) Rectangles are used to represent
module.c) Module is assigned a number. 0 is
reserved for main module and referred as level-0 module. Below
it level 1 module. There can be three level 1 module. Below it level 2 modules. The subdivision of modules ends when each module is clearly defined, single function to perform.
How to Identify modules?
A module name is a short description of what the module does. The identification of modules also includes number.
1. 1 in 1st position – modules that show the processing that takes place before the data records are processed (input). For example 100 and its subordinate modules as 110,120 …190. If there are more than 9 subordinate module then - 1010,1020,….,1100 etc.
2. 2 in 1st position - modules that show the processing of data record. For example 200 and its subordinate modules as 210,220 …290. If there are more than 9 subordinate module then - 2010,2020,….,2100 etc.
3. 3 in 1st position – modules for the processing that takes place when there are no more data records(output). For example 300 and its subordinate modules as 310,320 …390. If there are more than 9 subordinate module then - 3010,3020,….,3100 etc.
What about Structure charts and flowcharts?
a) Structure chart is a tool for planning the structure of a program. It shows the functions to be performed and the relationship between modules, but it usually provides too little information to serve as a basis for coding a program.
b) Flow chart show exact processing steps for coding program.
c) Structure chart – less revision, flow chart – more revision.
d) Structure chart is easier than flow chart for user to understand.
About Programming structures
Page 6 of 17
Programming Practice And TechniquesIDB-BISEW IT Scholarship
Project
Three logic patterns have been identified which are sufficient for any structure program
1. Sequence structure2. Loop structure3. Selection structure
Details description of individual Programming structures
* Sequence structure:
Instructions are executed in the order in which they are encountered.
Only way to enter Sequence structure is at the top and only way to exit from it is at the bottom.
Can represent unconditional operation
decision outline is never used. It can perform the following
operation:o I/O operationo Arithmetic operationo Moving data.
* Loop structure:
Loop structure provides repetition of one or more instructions for as long as given condition is true.
* Selection structure:
It has Single entry and exit point. Exits from the decision outline are
labeled “T” and “F” instead of with the relationships “<,” “>,” “=,” etc. The relationship becomes part of the condition inside the outline rather than a label for an exit. A condition is tested, and one of two alternative courses of action is selected depending on whether the condition is true or false.
About Pseudocode
Its a fake code. An extension of and replacement
for algorithm.
It has no accepted standard. It has minimum no of rules and
language independent. It has only syntax rules concerned
with involve the loop and selection structures which are given below.
Example
DO WHILE ----- ---- END DO IF THEN ----- ELSE ----- END IF
Pseudocode for different structure:
Pseudocode for Sequence structure Pseudocode for Loop structure Pseudocode for selection structure
About Pseudocode for Sequence structure
few information more
1. Multiply hours by rate to get gross pay.
2. Add gross pay to total gross pay.3. Move input employee number to
output employee number.4. Print detail line
Pseudocode for Loop structure Tutorial
Example
DO WHILE instruction :
Set I to j DO WHILE (condition-A) Instruction-1 Instruction-2 ----- END DO
Page 7 of 17
Programming Practice And TechniquesIDB-BISEW IT Scholarship
Project
FOR instruction :
FOR i = j to k by 1 Instruction-1 ----- END FOR
REPEAT UNTIL instruction :
REPEAT Instruction-1 ----- UNTIL(condition-p)
Pseudocode for selection structure Tutorial
Example
IF instruction :
IF condition-B THEN Instruction-T ELSE Instruction-F END IF
IF condition-B THEN Instruction-T ELSE (null)END IF
CASE instruction :
CASE field-v OF V1: instruction-1 V2: instruction-2 ----- Vn:instruction-n
What about Comparison of two values
Comparison of two values is presented in flow chart by decision outline.
Comparison of two values are presented in structure programming by IF THEN ELSE instruction. In DO WHILE instruction such comparison is also used.
If we compare the contents of two fields, we can choose from among the alternatives in any one of the
Page 8 of 17
Programming Practice And TechniquesIDB-BISEW IT Scholarship
Project
six ways shown in the following table:
Possibility Alternative-1 Alternative-2
1 Field1 >= Field2 Field1 < Field2
2 Field1 < Field2 Field1 >= Field2
3 Field1 <= Field2 Field1 > Field2
4 Field1 > Field2 Field1 <= Field2
5 Field1 != Field2 Field1 = Field2
6 Field1 = Field2 Field1 != Field2
Example:
Sorting two numbers:
X = larger Y = smaller Logic to sort 2 numbers: If x < y then Exchange x and y Else (Null) End if
How to exchange:
If xMove y to x Move x to y Else (Null) End if
Sorting three numbers:
If xMove x to temp Move y to x Move temp to y Else (Null)End if
Sorting 3 numbers:
A = largest B = next largest C=smallest
If a < b then Move a to temp Move b to a Move temp to b Else
(Null) End if If a < c then Move a to temp Move c to a Move temp to c Else (Null) End if
If b < c then Move b to temp Move c to b Move temp to c Else (Null) End if
What about Boolean Algebra
Combination of conditions is convenient using Boolean algebra.
Boolean algebra deals with symbols that have values of "true" or "false"
Instead of addition, subtraction, multiplication, division in Boolean algebra and, or and not is used.
And, or and not can be summarized as:
A AND B is true if a is true and b is true.
Page 9 of 17
Programming Practice And TechniquesIDB-BISEW IT Scholarship
Project
A OR B is true if a is true or b is true, or both are true.
If A is true then not A is false. If A is false then not A is true.
What is Truth Tables
Truth Table is used to represent the possible values of combinations of conditions.
Developing truth table:
Set up column for each condition. List all possible combinations of
conditions, one combination to a row.
Write each expression to be evaluated as a column heading.
Under each of these column headings, write the value of the expression in the heading for the combination of conditions given for that row.
A B A AND B A OR B NOT A NOT B
T T T T F F
T F F T F T
F T F T T F
F F F F T T
Evaluating Boolean expression
Boolean expression without parentheses is evaluated in the following order:
Not evaluated first And evaluated next Finally or
Example :
A B C A AND B OR C A AND NOT B OR C A OR B AND NOT C
T T T T T T
T T F T F T
T F T T T T
F T T T T F
T F F F T T
F T F F F T
F F T T T F
F F F F F F
In the expression given above "A AND B OR C", A AND B is evaluated first; the resulting value is then ORed with C to get a value for the entire expression.
In the expression given above "A AND NOT B OR C", NOT B is evaluated first; this value is ANDed with A and the result is ORed with C to get a value for the entire
Page 10 of 17
Programming Practice And TechniquesIDB-BISEW IT Scholarship
Project
expression. In the expression given above "A OR B AND NOT C", NOT C is evaluated first; this
value is ANDed with B and the result is ORed with A to get a value for the entire expression.
With parenthesis portion of the expression inside the parenthesis is evaluated first, starting with the inner most set of parenthesis.
A B C B AND C A OR (B AND C) A OR B (A OR B) AND
C
T T T T T T T
T T F F T T F
T F T F T T T
F T T T T T T
T F F F T T F
F T F F F T F
F F T F F F F
F F F F F F F
In the expression given above "A OR (B AND C)", (B AND C) is evaluated first; the resulting value is then ORed with A to get a value for the entire expression.
Similarly In the expression given above "(A OR B) AND C", (A OR B) is evaluated first; the resulting value is then ANDed with C to get a value for the entire expression.
With parenthesis portion of the expression inside the parenthesis is evaluated first, starting with the inner most set of parenthesis.
Special Boolean properties :
Two Boolean expressions are equivalent if they have the same values for all combinations of conditions. Truth tables can be used to prove the equivalence of Boolean expression.
A B C B OR C
A AND (B OR C) A AND B A AND
C(A AND B) OR (A
AND C)
T T T T T T T T
T T F T T T F T
T F T T T F T T
F T T T F F F F
T F F F F F F F
F T F T F F F F
F F T T F F F F
F F F F F F F F
Page 11 of 17
Programming Practice And TechniquesIDB-BISEW IT Scholarship
Project
In the above table expression A AND (B OR C) and (A AND B) OR (A AND C) are equivalent because they have the same values for all combinations of conditions.
What about Input editing
Input editing includes a variety of checks that are made in an attempt to prevent erroneous data from getting into computer file.
Some of the most widely used input editing techniques:
Sequence checking Restricted value test Miscellaneous other test
Sequence checking :
Whenever a program requires that data be sequenced to perform properly, the program should check that data is in the excepted sequence.
Sequence checking consists of comparing the key field (s) of the record read in with the key field (s) of the previous record.
Since reading in a new record destroys the fields from the previous read, the program must save the current key field values somewhere so they are available when the next record is processed.
A special problem is presented when the very first record in the file is processed, for the program logic will try to compare the key field from this first record with some previous values that do not exist. For ascending order initialize the previous key field values to a very small number so that the key field of the first record will be greater than the initial value (it is reverse for descending order).
Restricted value test :
Sometimes data in a field is restricted to a particular value or several values.
Checking the value of a field to be sure it is acceptable is editing input by means of a restricted-value test.
The acceptable values must be in memory so that as the data read in, it can be compared to these values.
Miscellaneous other test :
Numeric checking: A field that is supposed to be numeric can be checked to determine if in fact it contains nonnumeric characters. Blank checking: A field that should have something in it can be checked to see if it is blank.Positive checking: The sign of a field that is supposed to be positive should be checked.
Error routine :
For input error the following processing can be done:
Display error and terminate processing
Display error and wait for correction
Make a record of error for future correction, then continue processing, ignoring the erroneous record.
What about Detecting end of file
End of file can be detected using the following two techniques:
Page 12 of 17
Programming Practice And TechniquesIDB-BISEW IT Scholarship
Project
Sentinel value :
Sentinel value is a predetermined value that is placed in a dummy record at the end of the data file.
Sentinel value is usually placed in the key field position in the dummy record.
It should be one that should never be found in an actual data record. For example the Sentinel value might be a negative number placed in a field that is known to be always positive.
The program must check the field in question every time a record is read to see if it contains the sentinel value. The DO WHILE loop controlling the reading and processing of the data records would look like this:
DO WHILE key field != Sentinel value
Counter :
Number of data records to be read be known in advance.
Number is a constant. Program counts number of records
that are read or processed; when count of the number of records processed equals the number expected, the processing of data records stops.
The pseudocode for handling this technique might look like the following:
Read number Move zero to count Do while count < number Read data record …………………. Add one to count End DO
What is Decision tables
Decision table is used to plan and document processing that involves
complex combinations of condition.
Decision table shows us:
What is to be done Under what condition In what order Don’t show the order in which
conditions will be considered.
Components of decision table:
Condition stub: contains simple condition.
Condition entries: consists of a series of rules, one for each combination of conditions that must be considered.
Action stub: lists the possible action in the order that they will be taken.
Action entries: show for each rule just what actions will be taken.
Stub – left portion of decision table.
<="" td="">
What is Redundancy
Redundancy –exists when there are more rules than necessaryDecision table shows us:
Look for two rules that have the same actions.
hen if all condition entries but one are exactly the same, there is redundancy between the two rules.
Will print the employee name if the following condition is satisfied
Page 13 of 17
Programming Practice And TechniquesIDB-BISEW IT Scholarship
Project
Rule 1: if Sex=‘M’ and Age > 65
Will not print the employee name if the following condition is satisfiedRule 2: if Sex!=‘M’ and Age > 65
orRule 3: if Sex=‘M’ and Age < 65 orRule 4: if Sex !=‘M’ and Age < 65 or
Redundancy between rules 2 and 4 and between rules 3 and 4. In the case of rules 2 and 4, we see an ‘N’ for the first condition for both rules, but a ‘Y’ and an ‘N’ for the second. Therefore the second condition does not matter; if the first condition is ‘N’, regardless of what the second condition is, we want to disregard the employee. In the case of rules 3 and 4, we see that the first condition is immaterial as long as the second condition is ‘N’. This will lead to prepare the following table as a simplified solution.
If we think about a Female whose age is 31 then either rule 2 or rule 3 could apply, and that violates one of the requirements of a decision table: only one rule may apply in any situation.
Where we were lead astray in trying to eliminate two redundancies at once. We must eliminate redundancies one at a time. If we eliminate the redundancy between rules 2 and 4, we get the following table, where the dash(-) indicates that the condition can be ‘Y’ or ‘N’. This table contains no redundancy, and is therefore an acceptable decision table. The case of the 31 year old female is covered by rule 2 in this table.
Page 14 of 17
Programming Practice And TechniquesIDB-BISEW IT Scholarship
Project
There is another way to simplify the table is that If a number of rules result in the same action, we can lump them together under the heading of else, with no conditions indicates as in the following table. A table may have only one ELSE rule.
What is Contradiction
Contradiction – exists when more than one rule applies in a given situation and the actions under the rules are different. In the following table there is a contradiction between rule 1 and 2. The conditions are the same, but the actions are contradictory.
Page 15 of 17
Programming Practice And TechniquesIDB-BISEW IT Scholarship
Project
Page 16 of 17