I know that many face problems in coding and coding is not everyone’s cup of tea however one can benefit from the following steps. Also, coding is like maths more you practice better coder you become. So, without any further ado let’s begin.
1. Read the problem carefully.
This step is essential while approaching any problem. While there are many ways of approaching a given problem, generally the question gives the user clarity to the programmer about the way he or she must approach the given problem.
For e.g. –
If we have a problem saying write a program to add 2 numbers, then a programmer can assign two numbers to a variable and then add them or the programmer can ask the user to input two numbers and then add those two numbers. So, in this case in the question will specify what one must do.
Here, the example was a trivial one however as one moves further into coding one must decide between various things such as where to put a switch case and where to put an if else statement or where to use loops and where to write recursive functions or where to pass by reference and where to pass by value. So, this must never be neglected.
2. Make an algorithm or a flow chart.
Let’s consider the above example:
Write a program to add two numbers inputted by the user and display the result.
Before writing the code, one must know how one can go about it, hence one must write an algorithm or draw a flow chart before writing the actual code.
How to write an algorithm-
Step 1: Start
Step 2: Declare variables num1 and num2 and sum
Step 3: Read values of num1 and num2
Step 4: Add num1 and num2 and store the value in sum
Step 5: Display sum
Step 6: Stop
How to write a flowchart-
Theses are some basic elements of a flowchart.
Basically, a flowchart or an algorithm gives a programmer clarity of thoughts about how he or she can approach a given problem. Once this clarity is achieved writing a code isn’t difficult.
3. Write the code
Now one can go ahead and write the code for the given program.
I have written the above code in c:
printf(“Enter two numbers\n”);
printf(“The addition of the numbers is:%d\n”,sum);
One can see that that the above steps enables one to easily code.
4. Think about other methods to write the code and implement it, it will definitely help you to clear your concepts.
Like I wrote the above code using pointers-
printf(“Enter 2 numbers\n”);
printf(“The addition of two numbers is:%d\n”,*sum);