Saturday, October 13, 2007

C Fundamentals

What is programming?

When we want a computer to perform a specific task, such as generating a marks sheet or a salary slip, we have to create a sequence of instructions in a logical order that a computer can understand and interpret. This sequence of instructions is called a program. The process of writing programs is called programming.

The task of programming involves a lot of effort and careful planning. Without this, the computer will produce erroneous results. The following steps should go into the planning of program:

Ø Defining and analyzing the problem

Ø Developing the solution logically using an algorithm


Defining and analyzing the problem

Before writing a program, we have to define exactly what

1. data we need to provide (input) and

2. information we want the program to produce (the output).

Once we know these, we can figure out how to develop the solution.

Deciding on input

Suppose we want to write a program to work out the total and average of a student’s marks in five subjects, we would need to mention the marks in the five subjects as input.

Deciding on output

Next, we have to think of the output the elements that should be displayed and those that should not. In the marks example, since the task is to prepare a marks sheet, the marks in all the five subjects, their total and average should be displayed on the screen.


Developing a solution logically

Once we have defined and analyzed the problem decided on the output and the input — we can go on to develop the solution.

The most important aspect of developing the solution is developing the logic to solve the problem. This requires creating a set of step-by-step instructions and/or rules called an algorithm. Each step performs a particular task. We can write these steps in plain English.. The algorithm for the example on finding total marks and average would look like this:

1. Note down the student’s marks in different subjects.

2. Find the total marks scored by the student.

3. Compute the average marks.

4. Assign grade.

5. Display average percentage of marks and grade.

6. End.

For any computer program to work well, it has to be written properly. Formulating an effective algorithm will make writing an effective program easier. For an algorithm to be effective, it has to have the following characteristics:

1. Finiteness: An algorithm should terminate after a fixed number of steps.

2. Definiteness: Each step of the algorithm should be defined precisely. There should be no ambiguity.

3. Effectiveness: All the operations in the algorithm should be basic and be performed within the time limit.

4. Input: An algorithm should have certain inputs.

5. Output: An algorithm should yield one or more outputs that are the result of operations performed on the given inputs.

The famous mathematician, D.E. Knuth, first expressed these characteristics.

Programming: The next step after developing an algorithm

Once we develop the algorithm, we need to convert it into a computer program using a programming language (a language used to develop computer programs). A programming language is entirely different from the language we speak or write. However, it also has a fixed set of words and rules (syntax or grammar) that are used to write instructions for a computer to follow.

Programming languages can be divided into three types. They are:

1. Machine language

This is the basic language understood by a computer. This language is made up of 0’s and 1’s. A combination of these two digits represents characters, numbers, and/or instructions. Machine language is also referred to as binary language.

2. Assembly language

This language uses codes such as ADD, MOV, and SUB to represent instructions. These codes are called mnemonics. Though these codes have to be memorized, assembly language is much easier to use than machine language.

3. High-level languages

High-level languages such as BASIC, FORTRAN, C, C++, and JAVA are very much easier to use than machine language or assembly language because they have words that are similar to English.

A quick comparison of programming languages

Machine Language

Assembly Language

High-level Languages

Time to execute

Since it is the basic language of the computer, it does not require any translation, and hence ensures better machine efficiency. This means the programs run faster.

A program called an ‘assembler’ is required to convert the program into machine language. Thus, it takes longer to execute than a machine language program.

A program called a compiler or interpreter is required to convert the program into machine language. Thus, it takes more time for a computer to execute.

Time to develop

Needs a lot of skill, as instructions are very lengthy and complex. Thus, it takes more time to program.

Simpler to use than machine language, though instruction codes must be memorized. It takes less time to develop programs as compared to machine language.

Easiest to use. Takes less time to develop programs and, hence, ensures better program efficiency.

Developing a computer program

Follow the steps given below to become a successful programmer:

  1. Define the problem: Examine the problem until you understand it thoroughly.

  1. Outline the solution: Analyze the problem.

  1. Expand the outline of the solution into an algorithm: Write a step-by-step procedure that leads to the solution.

  1. Test the algorithm for correctness: Provide test data and try to work out the problem as the computer would. This is a critical step but one that programmers often forget.

  1. Convert the algorithm into a program: Translate the instructions in the algorithm into a computer program using any programming language.

  1. Document the program clearly: Describe each line of instruction or at least some important portions in the program. This will make the program easy to follow when accessed later for corrections or changes.

  1. Run the program: Instruct the computer to execute the program. The process of running the program differs from language to language.

  1. Debug the program: Make sure that the program runs correctly without any errors or bugs as they are called in computer terminology. Finding the errors and fixing them is called debugging. Don’t get depressed when bugs are found. Think of it as a way to learn.

No comments: