1.4 Programming Methodology


An application program is usually written to do a specific task or solve a specific problem. But before you can write your program in a chosen computer language, you must first figure out how to instruct a computer to perform the task or how to solve the problem that need to be solved. In order to do that, you have to analyze the problem: determine the output the program should produce, and then determine the input the program requires in order to produce such output. Next, you need to figure out the steps that need to be carried out to perform the task or solve the problem. The steps are instructions that the program need to need to carry out, that will transform the program’s input into its output, and is usually called the algorithm of the program.

Algorithm

Relook at Problem 1.

Let us look at the earlier example. You want to write a program that will help user convert US dollar into Malaysian Ringgit. You already determined the output of the program. The output will be to display the amount in Malaysian Ringgit on the computer screen. The input of the program is the amount of US dollar, keyed in by the user from the keyboard. There is also a second input, which is the conversion rate. Now you have to figure out the process that will transform the value of the US dollars into an equivalent value in Malaysian Ringgit. In this example, the process in not very complex. To convert the value, the computer has to multiply the value of the US dollars by the conversion rate. We can put these information in an Input-Process-Output (IPO) table as shown:


1.4 Programming Methodology



It is safe to say that an algorithm usually begins with getting input data, then processing that data, and then it usually end with the display or printing of output. In this example, the algorithm is specified as a list of steps, from step 1, to the last step. Some programmers prefer to design the algorithm using a mixture of English words and code-like statements. This is called pseudocode.


1.4 Programming Methodology


Pseudocode

A pesudocode is a an artificial and informal language, which is similar to everyday English, but is also allows you to use some kind of coding statements similar to program language codes. There is no specific standards for pseudocodes; each person may have his or her own style of writing pseudocode. If a programmer is familiar with C++ language, he or she may feel comfortable to use codes similar to C++, codes which are not actually C++ code. But why use codes similar to C++ codes, why not just use actual C++ codes? In pseudocode, he can use a mixture of C++ like codes and English. By doing this, he is able to write the algorithm without having to worry about the details of C++ syntax (rules of writing C++ codes). Below is how the pseudocode for Example 1 may be written.


There is another alternative to design algorithm for a program, and that is by drawing a diagram called a flowchart.


1.4 Programming Methodology


4.3 Flowchart

Unlike pseudocode, flowcharts uses a set of standardized symbols. There are specific symbols for input and output; symbol for processes, symbols for start and stop of program, symbol for decision and symbol to represent the flow of the steps. There are other symbols for other puposes, but the ones that are most common ate summarized in the following diagram.




1.4 Programming Methodology


Below is how the flowchart for Example 1 looks like.



1.4 Programming Methodology


Desk-checking

When you design your algorithm, before you code them into a specific program language, you should ensure that the algorithm is complete and that you have not missed any steps, and that the steps are correct. To do this, you have to perform desk-checking. Some people call this activity hand-tracing. How do you perform a desk-check?

Before you start, you must first choose a set of input values, then you must compute or calculate the steps manually to get the expected outputs for the set of inputs. For example, in the currency converter problem you may choose the following set of input:




1.4 Programming Methodology


For each of the input set, you will then manually calculate the expected outcome. Therefore, the outcome for the input sets are as follows :



Then using the input sets, by going through each step of the algorithm. You can choose to use either the pesudocode version of the flowchart version. Let us check the pseudocode version.




1.4 Programming Methodology


To assist in the desk-checking, first create a table that contains a column for each input, output, any temporary calculation (if necessary). For the program given above, you will need a table as shown in Table 3.

The first instruction in the algorithm is for the user to enter the value of USD to be converted, and the rate of conversion. Place these values in the corresponding columns as shown in the following table.




1.4 Programming Methodology


The next instruction is to calculate the RM value based on the formula given, which is to calculate the value of USD by the value of conversion rate. Using the table, you will get the values that are to be calculated. So, if for example, the USD column in the table is empty, then you know that your algorithm has missed a step. Now, since you have the values in the respective columns, use those values to calculate the RM value. 100 x 3.48 = 348.00, and place that value in the RM column as shown in Table 4 below.



The last instruction is to display the RM value on the computer screen. In this case, the value 348.00 will be displayed, because that is the value that appears in the table’s RM column. If this value is consistent with the manual calculation you performed earlier, prior to desk-checking the algorithm, then the algorithm appears to be correct. You should however, perform more desk checking using different sets of input values. You must now try to conduct desk checking using the second set of values given earlier.