DEVELOPMENT OF A COMPUTER PROGRAM

After completing this unit, you will be able to:

· Understand the meaning of an Algorithm.

· Understand the properties of an Algorithm.

· Understand the meaning of a Flowchart.

· Comfortably use Flowchart Symbols and the Flow Lines connecting these Symbols.

· Understand the Advantages and Limitations of a Flowchart.

2.1 Introduction

2.2 The Algorithm

2.3 The Flowchart

2.4 Simple Examples

2.5 Summary

2.6 Self-Assessment Questions

A computer program is the means by which we tell a computer to do something for us. To write a computer program, you must know two things: what you want the computer to do, and a means to tell the computer what you want it to do. The first part (also known as algorithm development) is often far more difficult than the second. The second part is the creation of a flowchart. Flowcharts are restricted to a set of instructions that all modern computer languages are guaranteed to have.

The normal problem solving method is as follows:

- Recognize and understand the problem.
- Accumulate relevant facts.
- Select an appropriate theory.
- Make the necessary assumptions.
- Solve the problem.
- Verify results

Performing step 5 in the above process (solving the problem) may involve a computer.

The following steps are involved in using a computer as a problem-solving tool:

- Develop an algorithm and a flowchart.
- Write the program in a computer language (such as Fortran or C).
- Enter the program into the computer.
- Test and debug the program.
- Run the program, input data and get the results from the computer.

Before writing a computer program one must be clear about the steps to be performed by the computer for processing it. To produce an effective computer program it is necessary that every instruction be written in the proper sequence. For this reason, the program must be planned before writing it. An algorithm represents the logic of the program. When an algorithm is expressed in the programming language, it becomes a program.

Let us now look at the characteristics of an algorithm in order to understand it better.

An algorithm must satisfy the following criteria:

Input: Zero or more quantities are externally supplied as inputs.

Output: At least one quantity as output is produced.

Finiteness: An algorithm terminates after a finite number of steps.

Definiteness: Each instruction must be clear and unambiguous.

Effectiveness: Every instruction must be very basic so that it can be carried out in principle by a person using just a pencil and paper

A cooking recipe can be considered as an algorithm as it describes a prescribed method for cooking a certain dish. The algorithm starts with a given problem. The problem is the goal to be achieved. When a problem is given, there may be several alternative algorithms to solve that problem. We must try to choose the best algorithm out of all of them. A program will then be written for the best algorithm.

Let us now look at a few examples of algorithms:

Step 1: Take the first number from the user (input).

Step 2: Take the second number from the user (input).

Step 3: Add the two numbers to get the sum (process).

Step 4: Display the sum on the screen (output).

10

The formula for the sum of 10 numbers can be sum = SUMMATION number i

i = 1

Step 1: Let the sum be initially set to 0.

Step 2: Let i be initially 1.

Step 3: Take the I th number from the user.

Step 4: Add the ith number to the sum.

Step 5: Increase the value of i by 1.

Step 6: Check if the value of i is less than or equal to 10; then go to step 4.

All 10 numbers are similarly entered by the user, until i becomes greater than 10. Then go to step 7.

Step 7: Output the sum.

Step 1: Take the number from the user.

Step 2: If the number is less than 0, display the entered number as negative.

Step 3: If the number is greater than 0, display the entered number as positive.

Step 4: If the number is equal to 0, display the entered number as zero.

A flowchart is a means of visually representing the flow of data through an information processing system, the operations performed within that system and the sequence in which they are performed.

A flowchart is a pictorial representation of an algorithm.

In this unit, we shall concern ourselves with the program flowchart which describes what operations (and in what sequence) are required to solve a given problem. The program flowchart can be likened to the blueprint of a building. As you know, a designer draws a blueprint before starting the construction of a building. Similarly, a programmer prefers to draw a flowchart prior to writing a computer program. As in the case of the drawing of a blueprint, the flowchart is drawn according to defined rules and using standard flowchart symbols prescribed by the American National Standard Institute, Inc.

The algorithm can get complex for long programs. On the other hand, if the algorithms are represented in a graphical or pictorial form, they would be easy to understand. One of the most widely used pictorial representations of an algorithm is a flow chart.

A flowchart is a diagrammatic representation that illustrates the sequence of operations to be performed in order to realize the solution of a problem. Flowcharts are generally drawn in the early stages of formulating computer solutions. Flowcharts facilitate communication between programmers and business managers. These flowcharts play a vital role in the programming of a problem and are quite helpful in understanding the logic of complicated and lengthy problems. Once the flowchart is drawn, it becomes easy to write the program in any high level language. Often we see how flowcharts are helpful in explaining the program to others. Hence, it is correct to say that a flowchart is a must for the better documentation of a complex program.

With the use of flowcharts, you will get a clear idea of the series of events that need to be arranged in a logical order for solving a specific problem.

Flowcharts are usually drawn using some standard symbols; however, some special symbols can also be developed when required. Some standard symbols which are frequently required for flowcharting many computer programs are shown in Fig. 2.1.

The following are some guidelines to effective flowcharting:

a. In drawing a proper flowchart, all necessary requirements should be listed out in logical order.

b. The flowchart should be clear, neat and easy to follow. There should not be any room for ambiguity in understanding the flowchart.

c. The usual direction of the flow of a procedure or system is from left to right or top to bottom.

d. Only one flow line should come out from a process symbol.

e. Only one flow line should enter a decision symbol, but two or three flow lines, one for each possible answer, should leave the decision symbol

f. Only one flow line is used in conjunction with the terminal symbol

g. Write within standard symbols briefly. If necessary, you can use the annotation symbol to describe data or computational steps more clearly.

i. Ensure that the flowchart has a logical start and finish.

j. It is useful to test the validity of the flowchart by passing through it with a simple test data.

Now we shall present a few examples on flowcharting so that you understand this technique well. This will help you in the program development process at a later stage.

Draw a flowchart to find the sum of the first 50 natural numbers.

Answer: The required flowchart is given in Fig. 2.2.

Draw a flowchart to find the largest of three numbers A, B and C.

Answer: The required flowchart is shown in Fig. 2.3.

Write an algorithm and draw a flowchart for computing factorial N (N!) where N! =1* 2 * 3 * ……. * N.

Answer:

Algorithm:

Step 1: Input the value of N.

Step 2: Let factorial F and M be initialized to 1.

Step 3: Multiply F by M (F = F * M).

Step 4: Check whether M = N; if no, go to step 5; if yes, go to step 6.

Step 5: Increase the value of M by 1 and go to step 3.

Step 6: Output factorial F.

The required flowchart has been shown in fig. 2.4

The benefits of using flowcharts are as follows:

1. Communication: Flowcharts are a better way of communicating the logic of a system to all the concerned people.

2. Effective analysis: With the help of a flowchart, a problem can be analysed in a more effective way.

3. Proper documentation: Program flowcharts serve as a good program documentation which is often required for various purposes.

4. Efficient coding: The flowchart acts as a guide or blueprint during the systems analysis and program development phases.

5. Proper debugging: The flowchart helps in the debugging process.

6. Efficient program maintenance: The maintenance of operating a program becomes

Flowcharts do have limitations which all programmers should be aware of.

1. Complex logic: Sometimes the program logic is quite complicated. In that case, flowcharting becomes complex and clumsy.

2. Alterations and modifications: If alterations are required, the flowchart may require re-drawing completely.

3. Reproduction: As the flowchart symbols cannot be typed, reproduction of a flowchart becomes a problem.

4. The essentials of what is done can easily be lost in the technical details of how it is done.

N1=1 *2*3*4 …….. * N

Step 1: Input value of N.

Step 2: Let Factorial F be initialized to 1.

Step 3: Let I = 1.

Step 4: Multiply factorial F by value of I.

F=F * I

Step 5: Increase the value of I by 1.

Step 6: Check whether I > N. If yes, go to step 7, otherwise go to step 4.

Step 7: Output Factorial F.

The flowchart for the above algorithm is given in Fig. 2.5

(Mod is the operation which gives the remainder part after division)

Step 1: Read N.

Step 2: If N Mod 2 = O then print “Number is even” & go to step 4.

Step 3: If N Mod 2 # O then print “Number is odd” & go to step 4.

Step 4: Stop.

The flow chart for the above algorithm is given in Fig. 2.6

Step 1: Read N

Step 2: Set Pnum = 0 & Nnum = 0

Step 3: Let cnt = 1.

Step 4: Read the first number as num.

Step 5: If num > 0, add 1 to Pnum.

(Pnum = Pnum + 1)

Step 6: If num < 0, add 1 to Nnum.

(Nnum = Num + 1)

Step 7: Add 1 to cnt.

Step 8: If cnt < = N, then go to step 4.

Step 9: Print the value of Pnum, Nnum.

Step 10: Stop.

The flowchart of the above algorithm is given in Fig. 2.7

In this unit you were introduced to some concepts which will be helpful in understanding the logic of programming. You should now be able to understand algorithms and their characteristics. You have seen the different symbols which are used in a flowchart and have also seen some simple example of algorithms and flowcharts. Now you have some understanding of how the logic of a program has to be developed.