Beginners – Analyzing the Problem

Author(s): Richard Forsen
Publication: Syntax
Pub Details: Syntax/Syntax ZX80 v2 n7 (Volume 2, Issue 7)
Date: July 1981
Pages: 15

The first step in solving a problem with a computer is analyzing how you would solve it without one. Both the human brain and the computer can accomplish these 4 tasks: input, output, storage, and processing.

If you ask a person to add 2 numbers, he must: 1) hear what you ask him and transmit your question to his brain, 2) remember the numbers he must add, 3) add the numbers, and finally 4) tell you the sum.

These steps are equivalent to 1) input, 2) storage, 3) processing, and 4) output. A computer follows the same pattern to solve a problem. To add any 2 numbers using a BASIC program, follow the same steps, using a special vocabulary that your computer can understand, the BASIC language. Here’s a program to add any 2 numbers (within the machine’s range) and print the result:

10 INPUT A
20 INPUT B
30 LET C=A+B
40 PRINT C
50 STOP

The INPUTs in lines 10 and 20 tell the computer to wait for the person at the keyboard to type 2 numbers and then store those numbers as variables A and B. These lines correspond to steps 1 and 2 of our problem-solving process, input and storage.

Line 30 tells the ZX80 to add the value of A to the value of B and call the answer C. This is step 3, processing, and back to step 2 (because the computer also must store the results of processing).

Line 40 prints C, the sum. This is step 4, output. After all, what good is it if the computer knows the answer and you don’t? Line 50 tells the computer that it’s done (usually unnecessary in such a simple program).

To this skeleton program you can add some frills, like PRINT statements to prompt the user to input A and B:

5 PRINT "ENTER FIRST NUMBER"
15 PRINT "ENTER SECOND NUMBER"

Well written programs include such prompts if the memory space permits them. Conversely, when you run out of space in a long program, the first things to ax are PRINT statements just there to make the program convenient.

Whenever you want to solve a problem on your computer, your first job is to break the problem down into steps that your computer can do, starting with requesting and storing necessary data. Then tell it what to do with that data, such as perform arithmetic on it (add, multiply, etc.). Finally you instruct it to tell you the results, usually with a PRINT statement.