Problem Solving Process
The process of problem-solving is an activity which has its ingredients as the specification of the program and the served dish is a correct program. This activity comprises of four steps :
1. Understanding the problem: To solve any problem it is very crucial to understand the problem first. What is the desired output of the code and how that output can be generated? The obvious and essential need to generate the output is an input. The input may be singular or it may be a set of inputs. A proper relationship between the input and output must be drawn in order to solve the problem efficiently. The input set should be complete and sufficient enough to draw the output. It means all the necessary inputs required to compute the output should be present at the time of computation. However, it should be kept in mind that the programmer should ensure that the minimum number of inputs should be there. Any irrelevant input only increases the size of and memory overhead of the program. Thus Identifying the minimum number of inputs required for output is a crucial element for understanding the problem.
2. Devising the plan: Once a problem has been understood, a proper action plan has to be devised to solve it. This is called devising the plan. This step usually involves computing the result from the given set of inputs. It uses the relationship drawn between inputs and outputs in the previous step. The complexity of this step depends upon the complexity of the problem at hand.
3. Executing the plan: Once the plan has been defined, it should follow the trajectory of action while ensuring the plan’s integrity at various checkpoints. If any inconsistency is found in between, the plan needs to be revised.
4. Evaluation: The final result so obtained must be evaluated and verified to see if the problem has been solved satisfactorily.
Problem Solving Methodology(The solution for the problem)
The methodology to solve a problem is defined as the most efficient solution to the problem. Although, there can be multiple ways to crack a nut, but a methodology is one where the nut is cracked in the shortest time and with minimum effort. Clearly, a sledgehammer can never be used to crack a nut. Under problem-solving methodology, we will see a step by step solution for a problem. These steps closely resemble the software life cycle. A software life cycle involves several stages in a program’s life cycle. These steps can be used by any tyro programmer to solve a problem in the most efficient way ever. The several steps of this cycle are as follows :
Step by step solution for a problem (Software Life Cycle)
1. Problem Definition/Specification: A computer program is basically a machine language solution to a real-life problem. Because programs are generally made to solve the pragmatic problems of the outside world. In order to solve the problem, it is very necessary to define the problem to get its proper understanding. For example, suppose we are asked to write a code for “ Compute the average of three numbers”. In this case, a proper definition of the problem will include questions like :
“What exactly does average mean?”
“How to calculate the average?”
Once, questions like these are raised, it helps to formulate the solution of the problem in a better way. Once a problem has been defined, the program’s specifications are then listed. Problem specifications describe what the program for the problem must do. It should definitely include :
INPUT :what is the input set of the program
- OUTPUT :
What is the desired output of the program and in what form the output is desired?
2. Problem Analysis (Breaking down the solution into simple steps): This step of solving the problem follows a modular approach to crack the nut. The problem is divided into subproblems so that designing a solution to these subproblems gets easier. The solutions to all these individual parts are then merged to get the final solution of the original problem. It is like divide and merge approach.
Modular Approach for Programming :
The process of breaking a large problem into subproblems and then treating these individual parts as different functions is called modular programming. Each function behaves independent of another and there is minimal inter-functional communication. There are two methods to implement modular programming :
- Top Down Design : In this method, the original problem is divided into subparts. These subparts are further divided. The chain continues till we get the very fundamental subpart of the problem which can’t be further divided. Then we draw a solution for each of these fundamental parts.
- Bottom Up Design : In this style of programming, an application is written by using the pre-existing primitives of programming language. These primitives are then amalgamated with more complicated features, till the application is written. This style is just the reverse of the top-down design style.
3. Problem Designing: The design of a problem can be represented in either of the two forms :
- Algorithm : The set of instructions written down to formulate the solution of a problem is called algorithm. These instructions when followed to write the code produces the desired output. The steps involved in the algorithm are usually written in English or any colloquial language which is easier for the programmer to comprehend what the code needs.
Let’s have a mundane example preparing the early morning tea and write an algorithm for the same.
STEP 1 : START
STEP 2 : Pour water in a pan.
STEP 3 : Put the pan on gas burner.
STEP 4 :Light the gas burner.
STEP 5 : Put sugar in the pan.
STEP 6 : Put tea leaves in the pan.
STEP 7 : Pour milk in the pan.
STEP 8 : Filter the prepared tea in the cup.
STEP 9 : Serve it to others and enjoy yourself.
STEp 10 : STOP
The ways to execute any program are of three categories:
- Sequence Statements
Here, all the instructions are executed in a sequence, that is, one after the another, till the program is executed.
- Selection Statements
As it is self-clear from the name, in these type of statements the whole set of instructions is not executed. A selection has to be made. A selected number of instructions are executed based on some condition. If the condition holds true then some part of the instruction set is executed, otherwise, another part of the set is executed. Since this selection out of the instruction set has to be made, thus these type of instructions are called Selection Statements.
- Iteration or Looping Statements
As lucid by the name itself, here a particular set of instructions is being executed multiple times. This set of instructions keeps on executing in a loop until a particular condition gets terminated. These types of instructions are also called iteration statements.
The three ways of executing a program explained above are also termed as ‘control structure’ since they determine and control the flow of execution in a set of statements.
Identification of arithmetic and logical operations required for the solution :
While writing the algorithm for a problem, the arithmetic and logical operations required for the solution are also usually identified. They help to write the code in an easier manner because the proper ordering of the arithmetic and logical symbols is necessary to determine the correct output. And when all this has been done in the algorithm writing step, it just makes the coding task a smoother one.
4. Coding: Once an algorithm is formed, it can’t be executed on the computer. Thus in this step, this algorithm has to be translated into the syntax of a particular programming language. This process is often termed as ‘coding’. Coding is one of the most important steps of the software life cycle. It is not only challenging to find a solution to a problem but to write optimized code for a solution is far more challenging.
Writing code for optimizing execution time and memory storage :
A programmer writes code on his local computer. Now, suppose he writes a code which takes 5 hours to get executed. Now, this 5 hours of time is actually the idle time for the programmer. Not only it takes longer time, but it also uses the resources during that time. One of the most precious computing resources is memory. A large program is expected to utilize more memory. However, memory utilization is not a fault, but if a program is utilizing unnecessary time or memory, then it is a fault of coding. The optimized code can save both time and memory. For example, as has been discussed earlier, by using the minimum number of inputs to compute the output, one can save unnecessary memory utilization. All such techniques are very necessary to be deployed to write optimized code. The pragmatic world gives reverence not only to the solution of the problem but to the optimized solution. This art of writing the optimized code also called ‘competitive programming’.
5. Program Testing and Debugging: Program testing involves running each and every instruction of the code and check the validity of the output by a sample input. By testing a program one can also check if there’s an error in the program. If an error is detected, then program debugging is done. It is a process to locate the instruction which is causing an error in the program and then rectifying it. There are different types of error in a program :
(i) Syntax Error
Every programming language has its own set of rules and constructs which need to be followed to form a valid program in that particular language. If at any place in the entire code, this set of rule is violated, it results in a syntax error.
Take an example in C Language
In the above program, the syntax error is in the first printf statement since the printf statement doesn’t end with a ‘;’. Now, until and unless this error is not rectified, the program will not get executed.
Once the error is rectified, one gets the desired output. Suppose the input is ‘good’ then the output is :
how are you good to see that you are good
(ii) Logical Error
An error caused due to the implementation of a wrong logic in the program is called logical error. They are usually detected during the runtime.
Take an example in C Language:
In the above code, the ‘for’ loop won’t get executed since n has been initialized with the value of 11 while ‘for’ loop can only print values smaller than or equal to 10. Such a code will result in incorrect output and thus errors like these are called logical errors.
Once the error is rectified, one gets the desired output. Suppose n is initialised with the value ‘5’ then the output is :
5 6 7 8 9 10
(iii) Runtime Error
Any error which causes the unusual termination of the program is called runtime error. They are detected at the run time.
Some common examples of runtime errors are :
Example 1 :
If during the runtime, the user gives the input value for B as 0 then the program terminates abruptly resulting in a runtime error. The output thus appears is :
NO OUTPUT Floating Point Exception
Example 2 :
If while executing a program, one attempts for opening an unexisting file, that is, a file which is not present in the hard disk, it also results in a runtime error.
6. Documentation : The program documentation involves :
- Problem Definition
- Problem Design
- Documentation of test perform
- History of program development
- User’s manual
A user’s manual is used by a user to know about a program’s input, processing, and output data.
7. Program Maintenance : Once a program has been formed, to ensure its longevity, maintenance is a must. The maintenance of a program has its own costs associated with it, which may also exceed the development cost of the program in some cases. The maintenance of a program involves the following :
- Detection and Elimination of undetected errors in the existing program.
- Modification of current program to enhance its performance and
- Enhancement of user interface
- Enriching the program with new capabilities.
- Updation of the documentation.
Control Structure- Conditional control and looping (finite and infinite)
There are codes which usually involve looping statements. Looping statements are statements in which instruction or a set of instructions is executed multiple times until a particular condition is satisfied. The while loop, for loop, do while loop, etc. form the basis of such looping structure. These statements are also called control structure because they determine or control the flow of instructions in a program. These looping structures are of two kinds :
- Conditional Control ( Finite Looping )
In this looping, the loop gets executed a finite number of times. Once a particular condition is satisfied, the loop gets terminated.
Take an example in C language :
n = 1, i;
(i = n; i <= 10; i++)
In the above program, the ‘for’ loop gets executed only until the value of i is less than or equal to 10. As soon as the value of i becomes greater than 10, the while loop is terminated.
1 2 3 4 5 6 7 8 9 10 //loop gets terminated
- Infinite Looping
The infinite looping control structure is just the opposite of finite looping control structure. Here, the condition to terminate the loop never gets satisfied and hence, the loop gets executed infinitely.
Take an example in Python language :
n = 1;
(n <= 10)
In the above code, one can easily see that the value of n is not getting incremented. In such a case, the value of n will always remain 1 and hence the while loop will never get executed. Such loop is called an infinite loop.
1 1 1 1 1 1 .. .. .. //loop never terminates
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.