Monday, 7 May 2018

COM111 Lecture Schedule 11


COM111 Lecture Schedule 11
Introduction to Computer Programming, Algorithm, Flowchart, Decision Tables, Pseudo code and Program Control Structures

Computer programming and languages
     It can perform a variety of tasks like receiving data, processing it, and producing useful results. A computer needs to be instructed to perform even a simple task like adding two numbers. Computers work on a set of instructions called computer program, which clearly specify the ways to carry out a task. Computer programs are developed using computer or programming languages. Programming involves many stages such as task analysis, flowcharting, coding, program testing, implementation, documentation and maintenance.
Developing a program
     A program consists of a series of instructions that a computer processes to perform the required operation. In order to design a program, a programmer must determine three basic rudiments:
1.      The instructions to be performed.
2.      The order in which those instructions are to be performed.
3.      The data required to perform those instructions.
Program Development Cycle
     Development cycle of a program includes the following phases:
1.      Analyse/Define the Problem: The problem is analysed precisely and completely. Based on understanding, the developer knows about the scope within which the problem needs to be developed.
2.      Task analysis: The developer needs to develope various solutions to solve the given problem. The optimum solution is chosen, which can solve the problem comfortably and economically.
3.      Developing Algorithm: After selecting the appropriate solution, algorithm is developed to depict the basic logic of the selected solution. An algorithm depicts the solution in logical steps. Algorithm is developed by flowcharts and pseudocodes.
4.      Testing the Algorithm for Accuracy: Before converting the algorithms into actual code, it should be checked for accuracy. The main purpose of checking algorithm is to identify major logical errors at an early stage.
5.      Coding: The actual coding of the programs takes place in the chosen programming language. A program can be written by using computer languages of different levels such as machine, assembly or high-level languages.
6.      Test and Debug the program: A program compiler and programmer-designed test data machine tests the code for syntax errors. Depending upon the complexity of the program, several rounds of testing may be required.
7.      Documentation: Once the program is free from all the errors, it is the duty of the program developers to ensure that the program is supported by suitable documentation. Documenting a program enables the user to operate the program correctly. It also enables other persons to understand the program clearly.
8.      Implementation: After performing all the above mentioned steps, the program is installed on the end user’s machine.
9.      Maintenance and Enhancement: After the program is implemented, it should be properly maintained taking care of the changing requirements of its users and system. The program should be regularly enhanced by adding additional capabilities.

Cycle Diagram

Program Development Cycle

ALGORITHM
     Algorithm is a precise rule which specifies how to solve some problem. An algorithm must be unambiguous and it should reach a result after a finite number of steps. Algorithm can be represented into programs, flowcharts, and pseudocodes.
To determine the largest number out of three numbers A,B,C, the following algorithm may be used.
     Step1: Start                                                                                                  
     Step2: Read three numbers say A,B,C
     Step3: Find the larger number between A and B and store it in MAX_AB
     Step4: Find the larger number between MAX_AB and C and store it in MAX
     Step5: Display MAX
     Step6: Stop

Example 1: Write an algorithm to add two numbers
Example 2: Write an algorithm to find simple arithmetic calculation
Example 3: Write an algorithm to find area of circle



FLOWCHART
     Flowchart is a means of visually presenting the flow of data through an information processing system, the operations performed within the system and the sequence.
SYMBOL
SYMBOL NAME
DESCRIPTION

Flow Lines
To connect symbols. These lines indicate the sequence of steps and direction of flow.

Terminal
To represent the beginning the termination or halt in the program logic.

Input / Output
Represents information entering or leaving the system, such as customer order and servicing.

Processing
Process symbol is used for representing arithmetic and data movement instructions.

Decision
Decision symbol denotes a decision to be made. This symbol has one entry and two exit paths.


Connector
To join different flow lines.

Off-page Connector
To indicate that the flowchart continues on the next page.

Document
To represent a paper document produced during the flowchart process.



 
-----------------


Annotation
To provide additional information about another flowchart symbol.


Manual Input
Represents input to be given by a developer/programmer.

Manual Operation
Shows that the process has to be done by a developer/programmer.

Online Storage
Represents the online data storage such as hard disks, magnetic drums, or other storage devices.

Offline Storage
Represents the offline data storage such as sales on OCR and data on punched cards.


Communication Link
To represent data received or to be transmitted from an external system.

Magnetic Disk
To represent is used to represent data input or output from and to a magnetic disk.

Guidelines for Preparing Flowcharts:

The following guidelines should be used for creating a flowchart:
     1.The flowchart should be clear, neat, and easy to follow.
     2.The flowchart must have a logical start and finish.
     3.In drawing a proper flowchart, all necessary requirements should be listed in logical order.
     4.The direction of the flow of a procedure should always be from left to right or top to bottom.
     5.Only one flow line should come out from a process symbol.








 
                                 
                                                                                  or
6.Only one flow line should enter a decision symbol. However two or three flow lines may leave the decision symbol.
                                                              <0                               >0

                                                                                            =0
7.Only one flow line is used with a terminal symbol.


 


SUM=A+B
 
8.Within standard symbols, write briefly. If necessary, use the annotation symbol to describe data or process more clearly.
                                    ------------ This process will add two numbers , store the value in
9.In case of complex flowcharts, connector symbols are used to reduce the number of flow lines.
10.Intersection of flow lines should be avoided to make it a more effective and better wayof representing communication.
1.It is useful to test the validity of the flowchart by passing through it with normal/unusual test data.
Benefits of Flowcharts:
v Makes Logic Clear: To plan a task, it provides a pictorial representation of the task, which makes the logic easier to follow.
v Communication: Being a graphical representation of a problem solving logic, flowcharts are better way of communicating the logic of a system to all concerned.
v Effective analysis: With the help of a flowchart, the problem can be analysed in an effective way.
v Useful in coding: Once the flowcharts are ready, the programmers can plan the coding process effectively.
v Proper Testing and Debugging: A flowchart helps in detecting the errors in a program, as the developers know exactly what the logic should do.
v Appropriate Documentation: Flowcharts serve as a good program documentation tool.
Limitations of Flowcharts:
     Flowchart can be used for designing the basic concept of the program in pictorial form but cannot be used for programming purposes. Some of the flowchart limatations are given below:
v Complex: The major disadvantage in using flowcharts is that when a program is very large, the flowcharts may continue for many pages,  making them hard to follow.
v Costly: If flowcharts are to be drawn for a huge application, the time and cost factor of program development may get out of proportion, making it a costly affair.
v Difficulty to Modify: Any changes or modification to a flowchart usually requires redrawing the entire the entire logic again, and redrawing a complex flowchart is not a simple task.
v No update: Usually programs are updated regularly. However, the corresponding update of flowcharts may not take place, especially in the case of large programs.
DECISION TABLES
A decision table is a way of representing an algorithm in a tabular form. It contains all the possible conditions for a specific problem and the corresponding results using condition rules that connect conditions with the results. It consists of rows and columns separated into four separate quadrants.
·         Conditions: Contains a list of all the possible conditions pertaining to a problem.
·         Condition Alternatives: Contains the condition rules (set of possible values for each condition) of alternatives.
·         Actions: Contains actions, which can be a procedure or operation to be performed.
·         Action Entries: Contains the action rules, which specify the actions to be performed on the basis of the set of condition alternatives corresponding to that action entry.
Each column in the quadrant can be interpreted as a processing rule. The basic four-quadrant structure is common for all decision tables; however, they may differ in the way of representation of the condition alternatives and action entries. For example, in some decision tables, Yes/No or numbers (0 or 1) may be used. Similarly, in decision tables, action entries are represented by placing check marks, cross marks or numbering the actions to be performed.
Steps to develop a decision table
In order to build the decision tables, first the maximum size of the table is determined. Any impossible situations, inconsistencies or redundancies are then eliminated from the table. The steps to be followed for developing the decision table are:
1.      Determine the actions that can be associated with a specific module or procedure.
2.      Determine all the conditions affecting the decision.
3.      Associate the specified conditions with specific actions. In the simplest case each condition in the decision table will be associated with two alternatives (Yes or No)
4.      Define rules by indicating actions that occur for the specified conditions.
Decision table for finding the largest of three numbers:
Conditions
Rules
1
2
3
A > B
Y
N
-
A > C
Y
-
N
B > C
-
Y
N
Actions

A is largest
X


B is largest

X

C is largest


X

PSEUDOCODE
     Psedocode is a generic way of describing an algorithm without the use of any specific programming language-related notations. It is an outline of a program, written in a form, which can easily be converted into real programming statements.
    For Example the pseudocode given below calculates the area of a rectangle.


PROMPT the user to enter the height of the rectangle
PROMPT the user to enter the width of the rectangle
COMPUTE the user by multiplying the height with width
DISPLAY the area
STOP
 
 




Pseudocode uses some keywords to denote programming processes. Some of them are:
v Input: READ, OBTAIN, GET, and PROMPT
v Output: PRINT, DISPLAY, and SHOW
v Compute: COMPUTE, CALCULATE, and DETERMINE
v Initialise: SET and INITIALISE
v Add One: INCREMENT
Psedocode Guidelines:
1.     Statements should be written in simple English.
2.     Steps must be understandable.
3.     Pseudocodes should be concise.
4.     Each instruction should be written in a separate line.
5.     Capitilise keywords such as READ, PRINT, and so on.
6.     Each set of instructions is written from top to bottom.
7.     It should allow for easy transition from design to implementation.
Benefits of Pseudocode:
v  It allows the developer to express the design in plain natural language.
v  It is easier to develop a program from a pseudocode than with a flowchart.
v  It is easy to translate pseudocode into a programming language, by less experienced programmers.
v  Its simple structure and readability makes it easier to modify.
v  Pseudocodes allow programmers who work in different computer languages to talk to each other.
Limitations of Pseudocode:
·         It does not provide visual representation of the programs’s logic.
·         There are no accepted standards for writing pseudocodes. Programmers use their style of writing pseudocode.
·         Pseudo code cannot be compiled nor executed, and there are no real formatting or syntax rules.

PROGRAM CONTROL STRUCTURES
     Program statements that affect the order in which statements are executed, or that affect whether statements are executed are called control structures.
     Control structures are used to express algorithms, flowcharts, and pseudocodes as actual computer programs. There are three control structures:
1.   Sequence, where information flows in a straight line.
2.   Selection, Where the decisions are made according to some predefined condition.
3.   Repetition, where the logic is repeated in a loop until the desired output is obtained.

1. Sequence Control Structure:
     In a sequence structure, the instructions to be computed simply follow one another in a logical progression. This structure is denoted by writing one action after another, each action on a line by itself, and all actions aligned with the logical indent. The actions are performed in the same sequence in which they are written.
Pseudocode
 
Text Box: Action 2Text Box: Action 1
Flowchart
 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      


                                                                                                                                                                                                                                                                            
                                                                   Action 1
                                                                                                                                                                                                                                                                             Action 2
                        



Text Box: Action N
 
                                                                                             Action N
             
                  Flowchart for                          Pseudocode for
             Sequence construct                    Sequence construct
                                                                  Sequence Control Structure
Example Flowchart
Step By Step Instructions
Start
 
            
















Print Z
 



Stop
 
 







                                             
Begin the flowchart.
Indicate value for memory variables X and Y.
Add X and Y store the result in variable Z.
Print the value of variable Z as output.
End the flowchart.

2.Selection Control Structure:
     A selection structure allows the program to make a choice between two alternate paths, whether it is true or false. The first statement of a selection structure is a conditional statement. Thus the selection structure has a only a single entry and a single exit.
                                                 Flowchart                Pseudocode
                                                                                                  
If Condition
True?

 
                                                        No                                                        Yes                            If (condition is true) THEN
                                                                                                                                                   List of Actions
                                        ELSE                                                                           THEN            ELSE
                                                                                                                                                       List of Different Actions
Action 2
 
Action 1
 
                                                                                                                                                       ENDIF


 
Oval: 0                                                                                                          


                                                 Flowchart for                                                             Pseudocode for
                                                Selection construct                                       Selection construct



Selection Control Structure
Example Flowchart
Step By Step Instructions
Start
 
            









Z= X + Y
 
 




             NO                                      YES








If A > B
 






 


Stop
 
Output B
 
Output A
 
0000
 
                                             
Begin the flowchart.
Read two numbers and store the value in memory variables A and B respectively.
Check whether the value of A is greater than the value of B.
If A is greater than B then display A, otherwise display B.
Connector Symbols are used to join the flowlines.
End the flowchart.

3. Repetition Control Structure:
     Repetition or loop pattern causes an interruption in the normal sequence of processing. It directs the system to loop back to a previous statement in the program, repeating the same sequence over and again, usually with new data. Using looping, the programmer avoids writing the same set of instructions again.

                                                Flowchart               Pseudocode
Flowchart: Decision: condition
Repeated task
 
    
                                                                                             REPEAT
                                                                False                         Sequence 1
                                                                                                                                Sequence 2        
                                                           True                                                                          

                                                                                              Seqence N
                                                                                                     UNTIL Condition is false
                                                  
                                                     Flowchart for                                 Pseudocode for
                                           Repetition construct                   Repetition construct




                                            Repetition Control Structure                                      
Example Flowchart
Step-by-step Instructions
Flowchart: Terminator: Start



















 True
                                   False
Begin the flowchart.
Initialise the memory variable COUNT to zero.
Increment the value of COUNT by 1.
Print the value INDIA.
Check whether the value of COUNT is less than or equal to 10. If yes, then loop back to the third step otherwise go to the next step.
End the flowchart.

        



No comments:

Post a Comment