Sei sulla pagina 1di 44

Problem Solving

Outcome expected
Ability to explain the basic concepts of problem solving. To Analyze and Define the Problem . Specify the input ,output and processing requirements. Specify step by step description of solution to a Problem. List the steps involved in program development.

Regardless of the area of study, computer science is all about solving problems with computers. The problems that we want to solve can come from any realworld problem or perhaps even from the abstract world. We need to have a standard systematic approach to solving problems.

Types of problems
Problems do not always have straightforward solutions. Some problems, such as balancing a check book or baking a cake, can be solved with a series of actions. These solutions are called algorithmic solutions. Once the alternatives have been eliminated, Example: once one has chosen the best among several methods of balancing the check book, the solution can be reached by completing the actions in steps. These steps are called the algorithm.

The solutions of other problems, such as how to buy the best stock or whether to expand the company, are not so straightforward. These solutions require reasoning built on knowledge and experience, and a process of trial and error. Solutions that cannot be reached through a direct set of steps are called heuristic solutions. The problem solver can use the six steps for both algorithmic and heuristic solutions.

However, in step 6, evaluating the solution, the correctness and appropriateness of heuristic solutions are far less certain. Its easy to tell if your completed check book balance is correct and satisfactory. but its hard to tell if you have bought the best stock. With heuristic solutions, the problem solver will often need to follow the six steps morethan once, carefully evaluating each possible solution before deciding which is best.

Many definitions of problem solving


Problem Solving is the sequential process of analyzing information related to a given situation and generating appropriate response options. Computer problem solving can be summed up in one word it is demanding. It is an intricate process requiring much thought, careful planning ,logical precision, persistence and attention to detail. Problem solving is the act of finding a solution to a perplexing, distressing, vexing, or unsettled question

Can Computer solve problems


A computer cannot analyze a problem and come up with a solution A computer can only do what it is programmed to do A programmer must analyze the problem and develop the instructions for solving the problem and have the computer carry out the instructions

Steps in problem solving


There are 6 steps that should be followed in order to solve a problem: 1. Understand the Problem 2. Formulate a Model 3. Develop an Algorithm 4. Program development 5. Test the Program 6. Evaluate the Solution

Step 1:Understand the Problem


Success in solving any problem is only possible after we had made an effort to come to terms with or understand the problem at hand. We cannot hope to make useful progress in solving a problem until we fully understand what we are trying to solve. This preliminary investigation can also be thought as the problem definition phase. What we must do during this phase is to workout what must be done rather than how to do it .

Read the problem. Look up unfamiliar words. Identify the unknown. Assign a variable to the unknown. Identify needed information and extraneous information.

Getting Acquainted
We must extract from the problem statement a set of precisely defined tasks. Where should I start ? Start from the statement of the problem. What can I do ? Visiualize the problem as a whole as clearly and as vividly as we can, donot concentrate yourself with details for a moment. What can I gain by doing so? You should understand the problem ,familiarze yourself with it , Impress its purpose on your mind

2. Formulate a Model
Now we need to understand the processing partof the problem. Many problems break down into smaller problems that require some kind of simple mathematical computations in order to process the data. Example: we are going to compute the average of the incoming grades. So, we need to know the model (or formula) for computing the average of a bunch of numbers. If there is no such formula, we need to develop one. Often, however, the problem breaks down into simple computations that can be understandable. Sometimes, we can look up certain formulas in a book or online if we get stuck.

In order to come up with a model, we need to fully understand the information available to us. Assuming that the input data is a bunch of integers or real numbers x1,x2,,xn representing a grade percentage, we can use the following computational model: Average1 = (x1+ x2+ x3+ + xn) / n where the result will be a number from 0 to 100. That is very straight forward (assuming that we knew the formula for computing the average of a bunch of numbers). However, this approach will not work if the input data is a set of letter grades like B-, C, A+, F, D-, etc.. because we cannot perform addition and division on the letters. This problem solving step must figure out a way to produce an average from such letters. Thinking is required.

After some thought, we may decide to assign an integer number to the incoming letters as follows: A + = 12 B + = 9 C + = 6 D + = 3 F= 0 A= 11 B= 8 C= 5 D= 2 A -= 10 B -= 7 C -= 4 D -= 1 If we assume that these newly assigned grade numbers are y1,y2,,yn, then we can use the following computational model: Average2 = (y1+ y2+ y3+ + yn) / n where the result will be a number from 0 to 12. As for the output, if we want it asa percentage, then wecan use either Average1 directly or use (Average2 / 12), depending on the input that we had originally. If we wanted a letter grade as output, then we would have to use (Average1/100*12) or (Average1*0.12)or Average2and then map that to some kind of lookup table that allows us to look up a grade letter according to a number from 0 to 12.

3. Develop an Algorithm
Now that we understand the problem and have formulated a model it is time to come up with a precise plan of what we want the computer to do. An algorithm is a precise sequence of instructions for solving a problem. To develop an algorithm, we need to represent the instructions in some way that is understandable to a person who is trying to figure out the steps involved

Pseudo code: Broken lamp problem

Flow chart : Broken Lamp

Representations for an Algorithm


Two commonly used representations for an algorithm is by using (1) pseudo code (2) flow charts Considering example of solving the problem of a broken lamp, we will represent an algorithm for solving the problem.

Pseudocode
Pseudocode is a simple and concise sequence of English-like instructions to solve a problem. Pseudocode is often used as a way of describing a computer program to someone who doesnt understand how to program a computer When learning to program, it is important to write pseudocode because it helps us to clearly understand the problem that you are trying to solve. It also helps you avoid getting bogged down with syntax details(i.e., like spelling mistakes) when you write your program later

Although flowcharts can be visually appealing, pseudocode is often the preferred choice for algorithm development because: It can be difficult to draw a flowchart neatly, especially when mistakes are made. Pseudocode fits more easily on a page of paper. Pseudocode can be written in a way that is very close to real program code, making it easier later to write the program Pseudocode takes less time to write than drawing a flowchart

There are some common control structures that appear whenever we write pseudocode. Sequence : Listing instruction step by step often numbered. Condition : making a decision and doing one ting or something else depending on the outcome of the decision. Repetition: repeating something until some condition occurs Storage :storing information for use in instructions which are further down the list.

Pseudo Code for broken lamp


1. IF lamp works, go to step 7. 2. Check if lamp is plugged in. 3. IF not plugged in, plug in lamp. 4. Check if bulb is burnt out. 5. IF blub is burnt, replace bulb. 6. IF lamp doesnt work buy new lamp. 7. Quit ... problem is solved

FLOWCHARTING
The flowchart is a means of visually presenting the flow of data through an information processing systems, the operations performed within the system and the sequence in which they are performed. Here we emphasize on the program flowchart, which describes what operations (and in what sequence) are required to solve a given problem. The program flowchart can be likened to the blueprint of a building. As we know a designer draws a blueprint before starting construction on a building. Similarly, a programmer prefers to draw a flowchart prior to writing a computer program

MEANING OF A FLOWCHART
A flowchart is a diagrammatic representation that illustrates the sequence of operations to be performed to get the solution of a problem. Flowcharts are generally drawn in the early stages of formulating computer solutions. Flowcharts facilitate communication between programmers and business people. These flowcharts play a vital role in the programming of a problem and are quite helpful in understanding the logic of complicated and lengthy problems. Once the flowchart is drawn, it becomes easy to write the program in any high level language.

GUIDELINES FOR DRAWING A FLOWCHART


Flowcharts are usually drawn using some standard symbols; however, some special symbols can also be developed when required. Some standard symbols, which are frequently required for flowcharting many computer programs are shown in below .

Flow chart symbols


Start or end of the program Computational steps or processing function of a program Input or output operation Decision making and branching Connector or joining of two parts of program

Flow chart symbols


Magnetic Tape Magnetic Disk Off-page connector

Flow line Annotation Display

GUIDELINES FOR DRAWING A FLOWCHART


In drawing a proper flowchart, all necessary requirements should be listed out in logical order. The flowchart should be clear, neat and easy to follow. There should not be any room for ambiguity in understanding the flowchart. The usual direction of the flow of a procedure or system is from left to right or top to bottom. Only one flow line should come out from a process symbol.

GUIDELINES FOR DRAWING A FLOWCHART


Only one flow line should enter a decision symbol, but two or three flow lines, one for each possible answer, should leave the decision symbol. Only one flow line is used in conjunction with terminal symbol. Write within standard symbols briefly. As necessary, you can use the annotation symbol to describe data or computational steps more clearly. If the flowchart becomes complex, it is better to use connector symbols to reduce the number of flow lines. Avoid the intersection of flow lines if you want to make it more effective and better way of communication.

Ensure that the flowchart has a logical start and finish. It is useful to test the validity of the flowchart by passing through it with a simple test data.

ADVANTAGES OF USING FLOWCHARTS


Communication: Flowcharts are better way of communicating the logic of a system to all concerned. Effective analysis: With the help of flowchart, problem can be analysed in more effective way. Proper documentation: Program flowcharts serve as a good program documentation, which is needed for various purposes

ADVANTAGES OF USING FLOWCHART


Efficient Coding: The flowcharts act as a guide or blueprint during the systems analysis and program development phase. Proper Debugging: The flowchart helps in debugging process. Efficient Program Maintenance: The maintenance of operating program becomes easy with the help of flowchart. It helps the programmer to put efforts more efficiently on that part

LIMITATIONS OF USING FLOWCHARTS Complex logic: Sometimes, the program logic is quite complicated. In that case, flowchart becomes complex and clumsy. Alterations and Modifications: If alterations are required the flowchart may require re-drawing completely. Reproduction: As the flowchart symbols cannot be typed, reproduction of flowchart becomes a problem. The essentials of what is done can easily be lost in the technical details of how it is done.

EXAMPLE 1 : ON FLOWCHARTING Flowchart to find the sum of first 50 natural numbers.

Example 2
Flowchart to find the largest of three numbers A,B, and C.

Example 3:computing factorial N (N!)


Flowchart for computing factorial N (N!) Where N! = 1 x 2 x 3 x N .

4. Program development
Now that we have a precise set of steps for solving the problem, most of the hard work has been done. We now have to transform the algorithm from step 3 into a set of instructions that can be understood by the computer. Writing a program is often called "writing code" or implementing an algorithm. So the code (or source code ) is actually the program itself.

The computer requires precise instructions in order to understand what we asking it to do. For example, if you removed one of the semi-colon characters (; ) from the program above, the computer would become confused as to what you are doing because the ( ; ) is what it understands to be the end of an instruction. Leaving one of them off will cause your program to generate what is known as a compile error.

5. Test the Program


Once we have a program written that compiles, you need to make sure that it solves the problem that it was intended to solve and that the solutions are correct. Running a program is the process of telling the computer to evaluate the compile instructions. When we run your program, if all is well, we should see the correct output. It is possible however, that our program works correctly for some set of data input but not for all. If the output of our program is incorrect, it is possible that we did not convert our algorithm properly into a proper program. It is also possible that we did not produce a proper algorithm back in step 3 that handles all situations that could arise. Whatever happened, such problems with your program are known as bugs.

Debugging
Bugs are problems/errors with a program that cause it to stop working or produce incorrect or undesirable results. To find bugs effectively, you should test your program with many test cases (called a test suite). It is also a good idea to have others test on program because they may think up situations or input data that you may never have thought of. The process of finding and fixing errors in your code is called debugging and it is often a very time-consuming chore when it comes to being a programmer. If you take your time to carefully follow problem solving steps 1 through 3, this should greatly reduce the amount of bugs in your programs and it should make debugging much easier.

6. Evaluate the Solution

Potrebbero piacerti anche