Given a problem, how can you come up with a program to solve a problem.

When you write a program, you have to tell the computer every small detail of what to do. And you have to get everything exactly right, since the computer will blindly follow your program exactly as written. It’s a matter of learning to think in the right way.

A program is an expression if an idea. A programmer starts with a general idea of a task for the computer to perform. Presumably, the programmer has some idea of how to perform the task by hand at least in general outline. The problem is to flesh out that outline into a complete, unambiguous, step-by-step procedure for carrying out the task. Such a procedure is called an “algorithm”.

Technically an algorithm is an unambiguous, step by step procedure that terminates after a finite number of steps (we don’t want to count procedures that go on forever).

An algorithm is more like the idea behind the program, but it’s the idea of the steps the program will take to perform its task, not just the idea of the tasks itself.

3.2.1 Pseudocode and Stepwise Refinement

In “programming in the small” you have learned the few basics to work with: variables, assignment statements, and input/output routines. You might also have some methods, objects or other building blocks that already been written by you or someone else. You can build sequences of these basic instructions, and you can also combine them into more complex control structures such as while loops and if statements.

How to write algorithm / program with the top-down technique

Suppose you have a task in mind that you the computer to perform. One way to proceed is to write a description of the task, and take that description as an outline of the algorithm you want to develop. Then you can refine and elaborate that description, gradually adding steps and detail, until you have a complete algorithm that can be translated directly into the programming language. This method is called stepwise refinement, and it is a type of top-down design. As you proceed through the stages of stepwise refinement, you can write out description of your algorithm in Pseudocode — informal instruction that imitate the structure of programming languages without the complete detail and perfect syntax of actual program code.

Note: that the pseudocode algorithm uses indentation to show which statements are inside the loop. In java, indentation is completely ignored by the computer, so you need a pair of braces to tell the computer which statements are in the loop.

Please recommend this post if you liked it.
One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.