Pin Me

C Programming For Beginners - Part 1

written by: Noel Kalicharan•edited by: J. F. Amprimoz•updated: 9/16/2008

This is the first in a series of articles designed to teach C programming to the complete beginner. Absolutely no programming background is assumed. We explain the first three stages of the programming process.

  • slide 1 of 5

    C Programming For Beginners - Part 1

    In the article The computer programming process we briefly described the following stages in the development of a computer program:

    (1) Define the problem.

    (2) Analyze the problem.

    (3) Develop an algorithm (a method) for solving the problem.

    (4) Write the computer program which implements the algorithm.

    (5) Test and debug (find the errors in) the program.

    (6) Document the program. (Explain how the program works and how to use it).

    (7) Maintain the program.

    We now take a more detailed look at each of these stages. In this article, we describe the first three.

  • slide 2 of 5

    Define the problem

    Suppose we want to help a child work out the areas of squares. This defines a problem to be solved. However, a brief analysis reveals that the definition is not complete or specific enough to proceed with developing a program. Talking with the child might reveal that she needs a program which requests her to enter the length of a side of the square; the program then prints the area of the square.

  • slide 3 of 5

    Analyze the problem

    We further analyze the problem to:

    (a) ensure that we have the clearest possible understanding of it;

    (b) determine general requirements such as the main inputs to the program and the main outputs from the program. For more complex programs, we would, for instance, also need to decide on the kinds of files which may be needed. (Think of a file as a place in the computer used for storing things like documents, pictures, programs, even songs and movies.)

    If there are several ways to solve the problem, we should consider the alternatives and choose the best or most appropriate one.

    In this example, the input to the program is the length of one side of the square and the output is the area of the square. We only need to know how to calculate the area. If the side is s, then the area, a, is calculated by:

    a = s x s

  • slide 4 of 5

    Write an algorithm to solve the problem

    An algorithm is a set of instructions which, if faithfully followed, will produce a solution to a given problem or perform some specified task. When an instruction is followed, we say it is executed. We can speak of an algorithm for finding a word in a dictionary, for changing a punctured tyre or for playing a video game.

    For any problem, there will normally be more than one algorithm to solve it. Each algorithm will have its own advantages and disadvantages. When we are searching for a word in the dictionary, one method would be to start at the beginning and look at each word in turn. A second method would be to start at the end and search backwards. Here, an advantage of the first method is that it would find a word faster if it were at the beginning, while the second method would be faster if the word were towards the end.

    Another method for searching for the word would be one which used the fact that the words in a dictionary are in alphabetical order—this is the method we all use when looking up a word in a dictionary. In any situation, a programmer would usually have a choice of algorithms, and it is one of her more important jobs to decide which algorithm is the best, and why this is so.

    In our example, we must write the instructions in our algorithm in such a way that they can be easily converted into a form which the computer can follow. Computer instructions fall into three main categories:

    (1) Input instructions, used for supplying data from the ‘outside world’ to a program; this is usually done via the keyboard or a file.

    (2) Processing instructions, used for manipulating data inside the computer. These instructions allow us to add, subtract, multiply and divide; they also allow us to compare two values, and act according to the result of the comparison. Also, we can move data from one location in the computer’s memory to another location.

    (3) Output instructions, used for getting information out of the computer to the outside world.

    Before we can write the algorithm for solving this problem, we need to explain a little bit about variables. We do this in our next article «C Programming for Beginners – Part 2».