A program is a set of instruction to be executed by the computer to accomplish a particular task. It is a multi-step process involving identification and definition of a problem, developing computer solutions for the same and preparing a sequence of instruction that can be run on the computer. While writing a program one has to take care of data which the program will process and generate information.
Steps in the programming process
The programming process is a set of activities and is carried out to develop and implement a computer program. Writing a program comprises of the six activities listed below.
- Understanding program specification.
- Design a program model
- Determine the correctness of the program
- Code the program
- Test and debug the program
- Document the program
Understanding program specification
Program specification included two activities:
- Problem definition
- Requirement analysis
Before beginning work on the house, a builder review the blueprints, check the all permits have been obtained and surveys the house’s foundation. A builder prepares differently for building a skyscraper or building a doghouse. No matter what the project the preparation is tailored to it needs and done conscientiously before construction begins.
The first prerequisite you need to fulfill before designing the program model is a clear statement of the problem that the program is supposed to solve. A problem definition defines what the problem is without any reference to the possible solutions. It is a simple statement, may be one to two page and it should sound like a problem. Problem definition comes before the requirement analysis, which is a more detailed analysis of the problem.
The problem definition should be in the user language, and the problem should be described from the user point of view. It usually should not be stated in technical computer terms.
Solving a problem without a clear understanding of its components may turn out to be a futile exercise as the solutions may not meet the requirement of the use. So, a problem statement has to be prepared which explains every minute details of the problem beyond doubt. This can be best achieved by writing down the problem in clear statements. Better problem definitions result in faster, easier and accurate solutions.
Requirement describe in details what a program is supposed to do, and they are the first steps towards a solution. The requirement activity is also known as Functional specification. An explicit set of requirement is important for several reasons. Explicit requirements help to ensure that the user rather than the programmer drives the functionality of the program. If the requirements are explicit, the user can review them and agree to them. Explicit requirement keeps you from guessing what the user wants. Specifying requirements adequately is key to the program success.
The three basic components are:
- What is given as input?
- What is expected as output, and
- How to arrive at the solutions
Let’s take an example and understand the above concept of input, process, and output.
A program is required to retrieve motor vehicle registration records from a file upon receipt of a request from an operator at a terminal. The operator will supply a vehicle registration number and the program will display the details of its vehicle and its owner. An error message will be displayed if the program is unable to locate the vehicle record.
Input: vehicle registration number
Process: Using the registration number, search for it, and if found, retrieve the details of the vehicle and its owner’s name from the disk.
Output: if the retrieval was successful, then allow the details of the vehicle to be displayed on the screen but if unsuccessful, indicate the absence of the vehicle registration on the disk and display a suitable error message.
Design a program model
Once the problem is clearly defined, an algorithm (another term for processing logic) can be developed. This is the most creative part of the programming. At this stage, the algorithm may be constructed in broad terms to help visualize possible alternatives.
An algorithm is a formula, a recipe, a step-by-step procedure to be followed in order to obtain the solutions to a problem. To be useful as a basis for writing a program the algorithm must be:
- Arrive at a correct solution with finite time.
- Be clear, precise, unambiguous and
- Be in a format which lends itself to an elegant implementation in a programming language.
The important tools used in developing a solution and in the preparation of an algorithm are flowcharts and pseudocode among others. Flowcharts provide a visual and graphical representation of the solution while pseudocodes mean writing the program logic in simple English like language. Logic depicted using these tools can then be written using any programming language. In other words, these are generic tools. we shall look at these tools in detail through this course.
Determine the correctness of algorithms
One of the most difficult, and sometimes most tedious steps in the development of an algorithm are ensuring and providing that the algorithm is correct.
The correctness of an algorithm or a program means that it gives the correct output and it does what it is supposed to do. Correctness of an algorithm or a program can be checked by anyone of the following methods:
- Dry run
- Independent inspection.
- Structured walkthrough
This is the process of converting our solutions, i.e. the algorithms (flowchart/pseudocode) into an actual computer program. Here we choose a programming language and using the syntax and semantics of the language, convert our algorithm thus far expressed in the form of a flowchart/pseudocode.
Syntax means the correct way or grammar, of writing a command or series of commands, including all the proper options and command-line statements.
Semantics means the logical meaning of the statement, separate from the grammatical structure.
Test and debug the program
Once the coding is complete, you will enter the program into the computer. The computer will then compile our program into machine code.
Compilation means converting the source code i.e. the user’s program which is written using a high-level language into machine code, which is the machine understandable language.
Compilation results in certain kinds of error which are indicated by the computer after the process is over. These errors have to be corrected and then the program recompiled. The process continues until the program is free of errors. Next, we must verify that our program does everything that is supposed to do. Invariably, computer programs do not work properly for the first time. The three types of errors that are normally encountered are:
Syntax errors occur when a computer language compiler cannot understand a command entered by the programmer. When such an error is encountered the computer rejects the program and print out an error message. These errors are encountered during compilation and are very easy to correct. In many cases, these errors arise due to spelling mistakes, missing commands, etc. and incorrect syntax. For example, in C if we type PRINT instead of printf this causes a syntax error.
These errors are encountered after error-free compilation, at the time of execution of the program. Execution errors are also called Runtime errors. Typical examples are:
- Infinite loops
- Correct output only for selected data
- Data incorrect or in the wrong order
- Incorrect filename
- Divide check errors. These checks appear when a quantity is divided by zero or by a number which is very nearly equal to zero.
Logical errors are due to mistakes made by the programmers while coding the program. The computer does not detect logical errors. For example, for calculating the net salary of an employee the formula is
NETSALARY=BASIC SALARY+ALLOWANCE DEDUCTIONS
But through oversight, while coding, the formula is written as:
This will obviously produce a wrong result. Such error can be detected only by a dry run. To correct these errors, we have to go through the algorithms of the program. These errors are easily detected, by running some sample problems, for which we know the answer. Most of these errors can be eliminated by properly designing the program. A complete scan and careful dry run and walkthrough are some of the ways in which these errors can be removed.
Debugging is the process of identifying the root cause of an error and correcting it. It contrasting with testing, which is the process of detecting the error initially.
Once the program has been written and debugged, it is ready to use and hence requires documentation or a written of how to run the program, enter data, what problems to expect, how to handle them, etc. documentations of a program will consist of flow charts/pseudocodes, programs listings and detailed written statements of algorithms and procedures involved. Documentation is necessary for program maintenance. Without proper documentation, it will be difficult to change a program at a later date.
It is a mix-held notion that documentation is the last step in the algorithm development stage. In fact, it should be interwoven with the entire phase of the programming process, especially with the design and implementation, because documentation is supposed to enable individuals to understand the logic of the program. Just writing and documenting a program is not just enough. One has to follow certain principles while developing algorithms. One such principle is structured programming.
Structured programming techniques
Structured programming techniques imply that program development and coding must follow a top-down approach and structured programming constructs. The top-down approach implies planning the program as a hierarchy of modules. Programming begins with the top (main) module. When the main module is programmed, second-level modules are then programmed.
In order to support the discipline of top-down programming the following basic structured programming constructs are followed in programming:
The sequence indicates that if there is no intervening control structure the flow of control normally passes from one instruction to the next sequence. Selection indicates a decision, from among several options. That is, you may branch out to one of the multiple locations the program depending on the decision. Repetition or looping constructs occur when a set of instruction is to be repeated several times. It saves the trouble of writing the same set of statements repeatedly.
Criteria for a good program
Following are some of the questions which can be asked to judge if a program is good:
- Does it solve the problem started in the specification?
- Does it work correctly under all conditions?
- Does it have proper documentation?
- Is it written using a modular approach?
- Does it make efficient use of computer time and memory?
These criteria can be met automatically if sufficient thought and effort are invested in every stage of program design.
A program flowchart is a pictorial representation of logic required to accomplish a task. It includes all the necessary steps of a program. It is called a flow chart because it charts the flow of a program. It is a symbolic representation of each input, output and processing step. Besides being a good method of writing down the algorithm, it is also a part of program documentation and helps in understanding, debugging and maintaining programs.
In flowcharting techniques, operations are represented by drawing appropriate symbols for the actions. Traditional program flowcharting involves the use of simple geometric symbols to represent the beginning or end of a program, a process, a decision or an input-output process. These flowcharts symbols are connected by arrows to illustrates the sequence of operations.
A graphical presentation using symbols to show the step-by-step sequence of operations, activities, or procedures. Used in computer system analysis, activities analysis, and in general program sequence representation.
The following represents the most commonly used symbols and their generally accepted meanings:
Examples: Flowchart for computing factorial N. (N!)
Flowchart for finding out the largest of three numbers.
The algorithm is the step by step instructions that are required to complete the work you want to do. Each instruction in the algorithm is known as STEP, while in C we say it’s a STATEMENT. Right now, we’ll look how an algorithm looks like. To describe algorithms, you don’t need to know anything about computers, at least for now. What you really need is an open mind and your own mother tongue. Let’s look at this one, suppose you were given two glasses full of colored water, red and green. Now you are asked to swap the water in the glasses. The glass containing red water should contain green water and the other one red. Think about the problem, there are ways to do it, but there is one way that is considered the best.
- Glass 1 HAS water Red
- Glass 2 HAS water green
- Get an EMPTY glass 3
- Transfer RED water from glass 1 to EMPTY glass 3
- Transfer GREEN water from glass 2 to EMPTY 1
- Transfer RED water from Glass 3 to EMPTY glass 2.
An alternative method of representing program logic is pseudocode. Instead of using symbols to represent the program logic steps, pseudocode uses statements which are a bridge between actual programming and ordinary English. In Pseudocode, each step is written using a simple English phrase which is also called a construct.
Pseudocode is an outline of a program, written in a form that can easily be converted into any real programming language statements. Pseudocode cannot be compiled nor executed, and there are no real formatting or syntax rules. It is simply one step-an important one- in producing the final code. The benefit of pseudocode is that it enables the programmer to concentrate on the algorithms without worrying about all the syntactic details of a particular programming language. In fact, you can write pseudocode without even knowing what programming language you will use for the final implementation.
Some of the conventions which are used while writing pseudo-codes are as follows:
- All statements in a loop should be indented.
- All alphanumeric values should be enclosed in a single or double-quotes.
- The beginning and end of pseudocode is marked with keywords like ‘start’ and ‘End’ respectively.
- All statements must include certain keywords which denote an operation.
For example, the pseudo-code for adding two numbers:
For example, the pseudo-code for bubble sort:
WHILE NOT AT END OF LIST
COMPARE ADJACENT ELEMENTS
IF SECOND IS GREATER THAN FIRST
GET NEXT TWO ELEMENTS
IF ELEMENTS WERE SWITCHED
REPEAT FOR ENTIRE LIST