I felt that this was a good point to start a tutorial on C/C++ programming because clearly we need to have some idea of what we are trying to achieve before we start out. I recently found this definition on the web which I rather like
"Programming is planning how to solve a problem. No matter what method is used -- pencil and paper, slide rule, adding machine, or computer -problem solving requires programming. Of course, how one programs depends on the device one uses in problem solving."
This is taken from the
ROYAL PRECISION, Electronic Computer PROGRAMMING MANUAL for the LGP-30. For those of you that have not heard of the LPG-30 you may be forgiven because it was first made in 1956 and has long since gone out of production. But it has the rather auspicious claim of being the type of computer that
Edward Lorenz was using when he first noted the chaotic nature of weather systems.
Anyway back to what is programming... "planning how to solve a problem", note we are not actually solving a problem, the computer is going to do that for us. If we could solve the problem ourselves we would have no need to write the program. The premise for a program is that we don't have the time, tenacity or memory capabilities to solve a problem but we do know how to solve it so can instruct a computer to do it for us.
A simple example of this is what is the sum of all integers from 1 - 10,000. If you wanted to you could sit down with a pencil and paper or a calculator and work this out however the time involved plus the likelihood that at some point you would make a mistake makes that an undesirable option. However I can write and run a program to calculate this sum in less than 5 minutes
-
#include "stdio.h"
-
-
#define MAX (10000UL)
-
-
int main(int argc, char **argp)
-
{
-
unsigned long sum = 0;
-
unsigned long number;
-
-
for(number=1; number<=MAX; number++)
-
{
-
sum += number;
-
}
-
-
printf("The sum of all integers from 1 - %lu is: %lu\n", MAX, sum);
-
-
return 0;
-
}
-
This gives the result 50005000. As it happens I can verify this because I know that the sum of integers from 1 - N can be calculated as
(N+1)*(N/2)
(10000 + 1)*(10000/2) = 10001*5000 = 50005000
So I have solved the problem of how to calculate the sum of all integers from 1 - 10000 and the computer has solve the problem of calculating the sum of all integers from 1 - 10000.
And this is the crux of all computer programs; you can not program a computer to solve a problem unless you know how to solve that problem. There is no point even sitting at your computer with the intention of programming until you have the knowledge, be that a formula from a text book or a design document or a print out from a web page, of how you are going to set about solving the problem.
So programming is the production of a set of instructions that describe how a problem can be solved. There are many languages in which these instructions may be written, for instance on the back of a bottle of shampoo you will often find the instructions describing how to solve the problem of making dirty hair clean:
- Wet hair
- Rub in shampoo to create a lather
- Rinse Hair
- Repeat
Note that because this set of instructions is aimed at human beings it makes a couple of assumptions. In step 4 for instance it makes the assumption that normal English is in use and that the instruction will actually be read as “Repeat Once”. It is also likely that an human running these instructions will not repeat step 1 because their hair will already be wet so they will judge it unnecessary to repeat that step.
And that is one of the major differences between humans and computers. Humans have judgement and free will and will not run any instruction they deem not required or nonsensical where as a computer will do exactly what it is told with no judgement on the need or sanity of the instruction. Give the instructions above to your computer and it will never get out of the shower.
Tutorial 2: How to Program