Introduction
As a programmer you will develop solutions to problems, solutions that transform given data (input) into required results (output). Generally this data is placed in secondary memory. Since the processor can only process the data that is available in main memory, so this data (from secondary memory) has to be transferred to the main memory first). Finally when the final result is produced it is to be preserved for the future use by transferring it from main memory to secondary memory.
It is the programmer who tells the computer what to do. As problem solvers, we use the computer to implement our solutions. These solutions are implemented on a computer using any programming language, such as Pascal, C or C++. A programming language reflects the range of operations a computer can perform. In this chapter we will study some of the strategies we can use to solve problems. Finally we will study about different data structures which are used to organize data in memory and typical operations that can be performed on these data structures.
The Concept of Programming
Have you ever noticed that your behavior and your thoughts are characterized by logical sequences. Every step of your life has a certain order to it. No doubt, a lot of what you do everyday is done automatically, on an unconscious level since it is not necessary for you to consciously think of every step involved in a process as simple as you brush your teeth. If you try to do this at conscious level then it involves all the following actions in a certain order or sequence as follows:
- take a brush
- clean it with water
- put some toothpaste on it
- rub the brush against your teeth for a minute
- take a glass of water and clean your teeth
Initially you had to learn this. But after some time, the action is completely automatic. In other words much of what you do unconsciously you once had to learn. After all every music requires a definite a sequence of notes in order to be recognizable.
The ultimate conclusion is that whatever we do at conscious level or at unconscious level, it takes place in a prescribed order. This ordering is called programming and when this programming is done on a computer language it is called computer programming. On the similar track, a computer program is a sequence of instructions that must be understood by the computer.
The programming process is basically a two step process:
- A problem solving phase
- An implementation phase
Problem Solving Phase
In problem solving phase, we firstly understand (define) the problem or you can say what a problem is. After knowing the problem, we develop a logical sequence of steps to be used to solve this problem because the computer can not itself analyze the problem and come up with a solution. It is the user or you can say a programmer who must arrive at a solution and communicate it to the computer. Thus the programmer begins the programming process by analyzing the problem and developing a general solution called an algorithm. An algorithm is just a step-by-step procedure for solving a problem in a finite amount of time for all possible conditions or you can say an algorithm is simply a verbal or written description of a logical sequence of actions.
After developing a general solution, the programmer “walk through” the algorithm by following logical sequences step by step mentally or manually. If programmer finds that this testing of the algorithm does not produce satisfactory result, he/she repeats the problem-solving phase, understand the problem again and coming up with another algorithm. Of course there may be more than one solution to a problem, it is the programmer who selects which is the best one.
An Implementation Phase
Once the solution is selected, it is programmer’s duty to translate this algorithm into a programming language. Translating an algorithm into a programming language is called coding the algorithm. For this we use ‘C’ programming language in this book. Like any simple language, a programming language is a set of rules, symbols, and special words used to construct a program. A programming language is essentially a simplified form of English (with math symbols) that adheres to a strict set of grammar rules.
After coding the algorithm, the resulting program is tested by executing (running) it on the computer system. If the program produces correct result then it is OK; otherwise the programmer must determine what is wrong and modify the algorithm and program as needed. If the program produces the desired results then it is used in practical application. The combination of coding and testing an algorithm is often referred to as implementing an algorithm.
Figure (a) shows the relationship between a problem solving phase and an implementation phase.
Figure (a)
Here one should not confuse with the definition of a program and an algorithm. An algorithm is just a way of expressing the solution. It may be in a programming language or in English. And when an algorithm is expressed in a programming language it is called a program. Another main point to note is that you should not try to take a short cut in the programming process by going directly from the problem definition to the coding of the program as shown in figure (b)
Of course it saves a lot of time, but if the program does not produce the desired result in first chance then you will spend a lot of extra time in correcting errors (debugging) and revising an ill-conceived program. Therefore the best strategy is to go to problem solving phase first and then implementation phase.
Figure (b)