Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,620 members, 7,816,551 topics. Date: Friday, 03 May 2024 at 01:01 PM

Is It Compulsory To Write An Algorithm Before Coding - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Is It Compulsory To Write An Algorithm Before Coding (1140 Views)

An Algorithm That Predicts Todays Upcoming Match Between Real Madrid Vs Cardiz / Write An Algorithm To Shuffle An Array / Thread For Nairaland Algorithm Questions (2) (3) (4)

(1) (Reply)

Is It Compulsory To Write An Algorithm Before Coding by orcl200(m): 7:48pm On Mar 15, 2018
A newbie programmer who happened to be Computer Scientist once asked me this question sometimes ago.

What is an algorithm.

An algorithm is a well-defined step by step procedure that describes how to solve a problem. Algorithms are more precise than informal instructions and do not require any insight to follow; they are still not precise enough for a computer to follow in the form they are written, but are precise enough for a human to know exactly what you mean, so they can then work out how to implement your algorithm, either doing it themselves, or writing a computer program to do it. The other important thing with this level of precision is that we can often make a good estimate of how fast it will be.

For example:
Let's say you want to create a program which allows the user to enter a number the program will evaluate if the number is even or odd and print the result on the screen.

So I come up with this Algorithm:
1. First, declare 2 variables. (1 for storing the number the user will enter, 1 for storing the remainder after modulus by 2).
2. Give the user a message informing him/her to enter a number.
3. Allow the user to enter a number and store it in the first variable.
4. Divide in modulus mode by 2 and store the remainder in the second variable.
5. Check with conditional statement IF the remainder is equal to 0, Display on the screen ("Entered number is an even number"wink ELSE Display on the screen (("Entered number is an odd number"wink.
Now the above step-by-step process to create our program is what is known as Algorithm.
Algorithms are often expressed using a loosely defined format called pseudo-code , which matches a programming language fairly closely, but leaves out details that could easily be added later by a programmer. Pseudo-code doesn't have strict rules about the sorts of commands you can use, but it's halfway between an informal instruction and a specific computer program or flowchart a diagrammatical illustration of the algorithm using symbols.

Below is the implementation of the above algorithm in JAVA;


import java.util.Scanner;

public class CheckNumber{
public static void main(String[] args){

int num, remainder;

System.out.println("Please enter a number to check even or odd:"wink;
Scanner inpNum = new Scanner(System.in);
num = inpNum.nextInt();
remainder = num % 2;
if(remainder == 0){
System.out.println("Entered number is an even number"wink;
}
else{
System.out.println ("Entered number is an odd number"wink;
}
inpNum.close();
}
}


By creating algorithm you will be able to evaluate all the possible solution to specific problem.
The most important thing to remember about algorithms is that there can be many different algorithms for the same problem, but some are much better than others. It’s a bad programming methodology to start writing computer program without analyzing and write the best algorithms for the problem at hand. I encourage you to study and inculcate into your carrier, the act of algorithm writing.

Keep coding………

2 Likes

Re: Is It Compulsory To Write An Algorithm Before Coding by dudebuck: 7:01am On Mar 16, 2018
orcl200:
A newbie programmer who happened to be Computer Scientist once asked me this question sometimes ago.

What is an algorithm.

An algorithm is a well-defined step by step procedure that describes how to solve a problem. Algorithms are more precise than informal instructions and do not require any insight to follow; they are still not precise enough for a computer to follow in the form they are written, but are precise enough for a human to know exactly what you mean, so they can then work out how to implement your algorithm, either doing it themselves, or writing a computer program to do it. The other important thing with this level of precision is that we can often make a good estimate of how fast it will be.

For example:
Let's say you want to create a program which allows the user to enter a number the program will evaluate if the number is even or odd and print the result on the screen.

So I come up with this Algorithm:
1. First, declare 2 variables. (1 for storing the number the user will enter, 1 for storing the remainder after modulus by 2).
2. Give the user a message informing him/her to enter a number.
3. Allow the user to enter a number and store it in the first variable.
4. Divide in modulus mode by 2 and store the remainder in the second variable.
5. Check with conditional statement IF the remainder is equal to 0, Display on the screen ("Entered number is an even number"wink ELSE Display on the screen (("Entered number is an odd number"wink.
Now the above step-by-step process to create our program is what is known as Algorithm.
Algorithms are often expressed using a loosely defined format called pseudo-code , which matches a programming language fairly closely, but leaves out details that could easily be added later by a programmer. Pseudo-code doesn't have strict rules about the sorts of commands you can use, but it's halfway between an informal instruction and a specific computer program or flowchart a diagrammatical illustration of the algorithm using symbols.

Below is the implementation of the above algorithm in JAVA;


import java.util.Scanner;

public class CheckNumber{
public static void main(String[] args){

int num, remainder;

System.out.println("Please enter a number to check even or odd:"wink;
Scanner inpNum = new Scanner(System.in);
num = inpNum.nextInt();
remainder = num % 2;
if(remainder == 0){
System.out.println("Entered number is an even number"wink;
}
else{
System.out.println ("Entered number is an odd number"wink;
}
inpNum.close();
}
}


By creating algorithm you will be able to evaluate all the possible solution to specific problem.
The most important thing to remember about algorithms is that there can be many different algorithms for the same problem, but some are much better than others. It’s a bad programming methodology to start writing computer program without analyzing and write the best algorithms for the problem at hand. I encourage you to study and inculcate into your carrier, the act of algorithm writing.

Keep coding………
Correct! But don't forget flowcharts.

(1) (Reply)

Top Universities To Study Computer Engineering In Nigeria / Issue With Vs Code / Python Q|A Ask Any Question?? || Or Drop A Python Tip...

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 20
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.