Chapter – 1
Problem Solving using Computer
What is a problem?
Problem is defined as the difference between an existing situation and a desired situation, that is, in accordance with calculation; a problem is numerical situation and has complex form. Solution is desired situation and has simplest form. If a problem is solved by computing using machine called computer, then such process is called Problem Solving using Computer.
If you have studied a problem statement, then you must analyse the problem and
determine how to solve it. First, you should know the type of problem that is, nature of problem. In programming point of view, the problem must be computing. At first you try to solve manually. If it is solvable manually by using your idea and knowledge, then you can use such idea and principle in programming and solve the problem by using computer. So, you must have well knowledge about a problem. In order to get exact solution, you must analyse the problem. To analyse means you should try to know the steps that lead you to have an exact solution.
Suppose you are asked by your father to solve an arithmetic problem and you are not familiar with the steps involved in solving that problem. In such a situation, you will not be able to solve the problem. The same principle applies to writing computer program also. A programmer cannot write the instruction to be followed by a computer unless the programmer knows how to solve the problem manually.
Suppose you know the steps to be followed for solving the given problem but while solving the problem you forget to apply some steps or you apply the calculation steps in the wrong sequences. Obviously, you will get a wrong answer. Similarly, while writing a computer program, if the programmer leaves out some of the instructions for the computer or writes the instructions in the wrong sequences, then the computer will calculate a wrong answer. Thus to produce an effective computer program, it is necessary that the programmers write each and every instruction in the proper sequence. However, the instruction sequence (logic) of a computer program can be very complex. Hence, in order to ensure that the program instructions are appropriate for the problem and are in correct sequence, program must be planned before they are written.
1.2Algorithm Development & Flowcharting:
The term algorithm may be formally defined as a sequence of instructions designed in
such a way that if the instructions are executed in the specified sequence, the desired result will be obtained. An algorithm must posses the following characteristics:
1.Each and every instruction should be precise and unambiguous.
2.Each instruction should be such that it can be performed in a finite time.
3.One or more instruction should not be repeated infinitely. This ensures that the algorithm will ultimately terminate.
4.After performing the instructions, that is after the algorithm terminates, the desire4d results must be obtained.
There are 50 students in a class who appeared in their final examination. Their mark sheets have been given to you. Write an algorithm to calculate and print the total number of students who passed in first division.
Step 1: Initialize Total First Division and Total Mark sheet checked to zero i.e. total_first_div = 0;
total_marksheet_chkd = 0; Step 2: Take the mark sheet of the next student.
Step 3: Check the division column of the mark sheet to see if it is I: if no, go to step 5.
Step 4: Add 1 to Total First Division i.e. total_first_div +1;
Step 5: Add 1 to Total Mark sheets checked i.e. total_marksheet_chkd +1;
Step 6: Is Total Mark sheets checked = 50: if no go to step 2
Step 7: Print Total First Division.
Step 8: Stop (End)
The above mentioned example is simpler one but development of an algorithm of a complex problem is very difficult. It may also be noted that in order to solve a given problem, each and every instruction must be strictly carried out in a particular sequence.
A flowchart is a pictorial representation of an algorithm that uses boxes of different 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 having arrow marks to indicate the flow of operation, that is, the exact sequence in which the instructions are to be executed.
Normally, an algorithm is first represented in the form of a flowchart and the flowchart is then expressed in some programming language to prepare a computer program. The main advantage of this two steps approach in program writing is that while drawing a flowchart one is not concerned with the details of the elements of programming language. Since a flowchart shows the flow of operations in pictorial form, any error in the logic of the procedure can be detected more easily than in the case of a program. Once the flowchart is ready, the programmer can forget about the logic and can concentrate only on coding the operations in each box of the flowchart in terms of the statements of the programming language. This will normally ensure an error-free program.
A flowchart, therefore, is a picture of the logic to be included in the computer program. It is simply a method of assisting the program to lay out, in a visual, two dimensional format, ideas on how to organize a sequence of steps necessary to solve a problem by a computer. It is basically the plan to be followed when a program is written. It acts like a road map for a programmer and guides him/her how to go from starting point to the final point while writing a computer program.
Experienced programmers sometimes write programs without drawing the flowchart. However, for a beginner it is recommended that a flowchart be drawn first in order to reduce the number of errors and omissions in the program. It is a good practice to have a flowchart along with a computer program because a flowchart is very helpful during the testing of the program as well as while incorporating further modifications in the program.
A flowchart uses boxes of different shapes to denote different types of instructions. The communication of program logic through flowcharts is made easier through the use of symbols that have standardized meanings. For example, a diamond always means a decision. Only a few symbols are needed to indicate the necessary operations in a flowchart. These symbols are standardized by the American National Standard Institute (ANSI). These symbols are listed below:
Terminal: The terminal symbol, as the name implies is used to indicate the beginning (START), ending (STOP) and pauses (HALT) in the program logic flow. It is the first symbol and last symbol in the program logic. In addition, pause (HALT) used with a terminal symbol in program logic in order to represent some error condition.
Input/Output: The input/output symbol is used to denote any function of an input/output device in the program. If there is a program instruction to input data from a disk, tape, card- reader, terminal or any other type of input device, that step will be indicated in the flowchart with an input/output symbol. Similarly, all output instructions whether it is output on a printer, magnetic tape, magnetic disk, terminal screen or any output device, are indicated in the flowchart with an input/output symbol.
Processing: A processing symbol is used in a flowchart to represent arithmetic and data movement instructions. Thus all arithmetic processes of adding, subtracting, multiplying and dividing are shown by a processing symbol. The logical process of moving data from one location of the main memory to another is also denoted by this symbol. When more than one arithmetic and data movement instructions are to be executed consecutively, they are normally placed in the same processing box and they are assumed to be executed in the order of their appearance.
Flowlines: Flowlines with arrowheads are used to indicate the flow of operations, that is, the exact sequence in which the instructions are to be executed. The normal flow of flowchart is from top to bottom and left to right. Arrowheads are required only when the normal top to bottom flow is not to be followed. However, as a good practice and in order to avoid ambiguity, flowlines are usually drawn with an arrowhead at the point of entry to a symbol. Good practice also dictates that flowlines should not cross each other and that such intersections should be avoided whenever possible.
Decision: The decision symbol is used in a flowchart to indicate a point at which a decision has to be made and a branch to one of two or more alternative points is possible. The criteria for making the decision should be indicated clearly within the decision box.
Connector: If a flowchart becomes very long, the flowlines start criss-cross at many places that causes confusion and reduces understandability of the flowchart. Whenever a flowchart becomes complex enough that the number and direction of flowlines is confusing or it spreads over more than one page, it is useful to utilize the connector symbol as a substitute for flowlines.
A student appears in an examination that consists of total 10 subjects, each subject having maximum marks of 100. The roll number of the students, his name, and the marks obtained by him in various subjects is supplied as input data. Such collection of related data items that is treated as a unit is known as a record. Draw a flowchart for the algorithm to calculate the percentage marks obtained by the student in this examination and then to print it along with his roll number and name.
ADD MARKS OF
Fig : Flowchart
In order to make a program in any programming language, what we have written is
known as code. The act of writing code in a computer language is known as coding. In other words, code is a set of instruction that a computer can understand.
1.4Compilation & Execution:
The process by which source codes of a computer (programming) language are
translated into machine codes is known as compilation. After compilation if everything is ok, the code is going under other process that is known as execution. We can get the required output after execution process.
1.5Debugging & Testing:
The process of finding and removing errors (also sometimes called buggs) from a
program is known as debugging. One simple method of debugging is to place print statements throughout the program to display the values of variables. It displays the dynamics of a program and allows us to examine and compare the information at various points. Once the location of an error is identified and the error is corrected, the debugging statements may be removed.
Generally programmers commit three types of errors.They are
Syntax errors are those errors which are arised from violating the rules of programming language.On encountering these errors, a computer displays error message.It is easy to debug.Logic errors are those which arised when programmers proceed the logic process in wrong way or miss the some statements.It is difficult to debug such errors because the computer does not display them.Run-time errors are those which occur when programmers attempt to run ambiguous instructions.They occur due to infinte loop statement,device errors,sofware errors, etc.The computer will print the error message .Some of runtime errors are :
•Divide by zero
•Null pointer assignment
•Data over flow
Testing is the process of reviewing and executing a program with the intent of detecting errors. Testing can be done manually and computer based testing.
Manual Testing is an effecting error-detection process and is done before the computer based testing begins. Manual testing includes code inspection by the programmer, code inspection by a test group and a review by a peer group. Computer based testing is done by computer with the help of compiler (a program that changes source codes into machine codes word by word).
Program Documentation refers to the details that describe a program.While writng programs ,it is good programming practice to make a brief explanatory note on the programor program segment.This explanatory note is called comment.It explains how the program works and interact with it.Thus ,it helps other programmers to understand the program.There are two types of documentation:
1.Internal documentation 2.External documentation
Some details may be built-in as an integral part of the program. These are known as internal documentation. Two important aspects of internal documentation are; selection of meaningful variable names and the use of comments. Selection of meaningful names is crucial for understanding the program. For example,
Area = Breadth * Length;
is more meaningful than
A = B * L
And comments are used to describe actions parts and identification in a program. For example,
/* include file * / describes parts of program /* header file * / describes parts of program.
External documentation is an executable statement in a programIt may be message to the user to respond to the program requirement.This is accomplished using output statements .It makes a program more attractive and interactive.Some examples are:
print “Input the number one by one” print “Do you want to continue ?”
Some Important Questions:
1.What is a Problem? What are basic steps in the process of Program Development? Explain each of them briefly.(PU2004)
2.Define algorithm and flowchart. What are the various symbols used to a flowchart? (PU 2003)
3.Differentiate between the flow chart and algorithm with the example. (PU2004)
4. What is debugging? (PU2005)
5. What is flow charting, describe its importance. (PU2006)
6. What is a flowchart? List the various commonly used flowchart symbols.
7.Why do we need documentation and testing for problem solving?(PU2006)