Kerala Plus One Computer Science Notes Chapter 4 Principles of Programming and Problem Solving
There are programmers who develop computer programs for solving problem.
Problem Solving using Computers
A computer can solve problems only when we give instructions to it. If it understand the tasks contained in the instructions, it will work accordingly. An instruction is an action oriented statement. It tells the computer what operation it should perform. A computer can execute (carry out the task contained in) an instruction only if the task is specified precisely and accurately.
Approaches in Problem solving:
A problem may be solved in different ways. Each of their approaches may be different, though all of them are solving the same problem. The two popular designing styles of problem solving are:
1. Top down design:
It is the process of breaking the over all procedure or task into component parts (modules) and then sub-dividing each component module until the lowest level of detail is reached. It is also called top down decomposition. Its merits are:
- Breaking the problem into parts helps us to clarify what is to be done in each part.
- At each step of refinement, the new parts become less complicated and therefore, easier to figure out.
- Parts of the solution may turn out to be reusable.
- Breaking the problem into parts allows more than one person to work for the solution.
2. Bottom up design:
Once the overall procedure or task is broken down into component parts (modules) and each component module is further sub divided until the lowest level of detail has been reached,I we start solving from the lowest module onwards. The solution for the main module will be developed only after designing specific solution to its sub modules.
Phases in Programming
- Problem identification: During this phase you will be able to identify the data involved in processing, its type and quality, formula to be used, activities involved ; and the output to be obtained. Once we studied the problem clearly, and are convinced about the sequence of tasks required for the solution, we can go to the next phase. This is the challenging phase as it exploits the efficiency of the programmer.
- Preparing algorithms and flowcharts: Once the problem is identified, it is necessary to develop a precise step-by-step procedure to solve the problem. This procedure is not new or confined to computers.
A finite sequence of instructions to solve a I problem, where each step represents a specific task to be carried out. An algorithm must possess the following characteristics:
- It should begin with instruction(s) to accept inputs.
- Variable must be used for inputting data and assigning values or results.
- Each and every instruction should be precise and unambiguous.
- Each instruction must be sufficiently basic such that it can, in principle, be carried out in finite time by a person with paper and pencil.
- The total time to carry out all the steps in the algorithm must be finite.
- After performing the instructions given in the algorithm, the desired results (outputs) must be obtained.
eg., Write an algorithm to find the sum of two numbers.
Let a, b, sum are variables.
Step 1: Start
Step 2: Read a, b
Step 3: sum = a + b
Step 4: Print sum
Step 5: Stop
Types of Instructions
We can use input instructions using Input, Accept or Read. Data can directly be stored in a memory location, results of arithmetic operations on data can be also be stored in memory locations. Variable followed by the equal symbol (=) can be used for storing value, where variable refer to memory locations. A computer can compare data values (logical operation) and make decisions based on the result.
It is the pictorial representation of an algorithm with specific symbols for instructions and arrows showing the sequence of operations. Flowcharts commonly use some basic geometric shapes to denote different types of instructions. The actual instructions are written within these boxes using clear and concise statements. These boxes are connected by solid lines with arrow marks to indicate the flow of operation.
The communication of program logic through flowcharts is made easier through the use of symbols that have standardised meanings.
It is used to indicate the beginning (START) and ending (STOP) in the program logic flow. It has the shape of an ellipse.
The parallelogram is used as the input/output symbols. All the input/output instructions are expressed using this symbol. It will be attached with one entry flow and one exit flow.
A rectangle is used to represent the processing step. Arithmetic operations as well as assigning a value to a variable are expressed using this symbol.
The rhombus is used as decision symbol and is used to indicate a point at which a decision has to be made. A branch to one of two or more alternative points is possible here.
Flow lines with arrow heads are used to indicate the flow of operation, that is, the exact sequence in which the instructions are to be executed. The normal flow is from top to bottom and left to right.
Whenever a flowchart becomes complex and the number and direction of flow lines is contusing or it spreads over more than one page, a pair of connector symbols can be used to join the flow lines that are broken. This symbol represents an “entry from”, or an “exit to” another part of the flowchart. Connector symbol is represented by a circle and a letter or digit is placed within the circle to indicate the link.
Main advantages of flowcharts are:
- Better communication: Since a flowchart is a pictorial representation of a program, it is easier for a programmer to explain the logic of the program to some other programmer through a flowchart rather than the program itself.
- Effective analysis: The whole program can be analysed effectively through the flowchart as it clearly specifies the flow of the steps constituting the program
- Effective synthesis: If a problem is divided into different modules and the solution for each module is represented in flow charts separately they can finally be placed together to visualize the overall system design.
- Efficient coding: Once a flowchart is ready, programmers find it very easy to write the concerned program because the flowchart acts as a road map for them.
Limitations of flowcharts
Flowcharts have some limitations.
- Flowcharts are very time consuming and laborious to draw with proper symbols and spacing.
- Owing to the symbol-string nature of flow-charting, any change or modification in the logic of the algorithm usually requires a completely new flowchart.
- There are no standards determining the amount of detail that should be included in a flowchart.
eg., Draw a flowchart to find the sum of two numbers.
3. Coding the Program:
The process of writing program instructions to solve a problem is called coding. Text editor programs are available to write the code in computer. The program written in any HLL is known as source code.
Hence, to be programmer we have to be well versed in any HLL to express the instructions in a program. Once the program is written using a language, it should be saved in a file (source file), and then proceed to the next phase of programming.
It is the process of converting a program written in high level language into its equivalent version in ma-chine language. The compiler or interpreter is used for this purpose. During this step, the syntax errors of the program will be displayed. These errors are to be corrected by opening the file that contains the source code. The source code is again given for compilation (translation). This process will be continued till we get a message such as “No errors or warnings” or “successful compilation”. Version of the source code is known as object code and it will be usually stored in a file by the compiler itself.
It is the stage where programming errors are discovered and corrected. Programming errors are known as ‘bugs’ and the process of detecting and correcting these errors is called debugging. In general, there are two types of errors that occur in a program.
- Syntax errors result when the rules or syntax of the programming language are not followed. Such program errors typically involve incorrect punctuation, incorrect word sequence, undefined term, or illegal use of terms or constructs. In the case of interpreters, the syntax errors will be detected and displayed during execution.
- Logical error is due to improper planning of the program’s logic. The language processor successfully translates the source code into machine code if there are no syn-tax errors. Computer actually follows the program instructions and gives the output as per the instructions. But the output may not be correct. This is known as logical errors. In order to determine whether or not there is a logical error, the program must be tested.
6. Execution and testing:
The purpose of testing is to determine whether the results are correct. The testing procedure involves running the program to process the test data that will produce ‘known results’. The operations involves in the program should be done manually and the output thus obtained should be compared with the one given by the computer. The accuracy of the program logic can be determined by this testing. The selection of proper test data is important in program testing. The error messages may be displayed by the error- handling function of the language. These errors are known as Runtime error. These errors can be rectified by providing instruction for checking the validity of the data before it gets processed by the subsequent instructions in the program.
We can write comments in the source code as part of documentation. It is known as internal documentation. It help the debugging process I as well as program modification at a later stage.
If the program is documented, it will help to understand the logic we applied, the reason why a particular statement has been used and so on. Writing comments in pro-grams is only a part of documentation. Another version of documentation is the preparation of system manual and user manual. These are hard copy documents that contain functioning of the system, its requirements etc. And the.procedure for installing and using the programs. While developing software for various applications, these manuals are mandatory. This kind of documentation is known as external documentation.