6)
Program 5 - Simple conversion
Program 5 | Simple conversion | Source code - prog5.c |
From now on things start to get a bit more
interesting, the programs are still small, but after
these next few you should start to see some
"use" for learning how to program. You are
probably thinking by now that it seems like a lot of
taxing stuff to do very little, which would be true
considering what programs have been done so far, but the
best is yet to come. It's just one of those things that
the boring bits have to be understood first :-( The program here shows how to multiply two numbers together and store the result. It would be far more useful if the user of the program could enter the numbers and the program produce the result, but input does not come in until program 8, so it will have to make do by setting the values in the program code for now :-( This is known as "hard-coding" |
Program code - new parts shown in red |
|
Description of the program code | ||||||||||||||||||||||||||||||||||
Apart from the line
in red above, everything else should be straight forward
: two int variables are declared ( inches and feet ). The
value 5 is stored in the feet variable when it is
declared, whereas the value of the inches variable is
undefined when it is declared. The line in red
multiplies the value of the feet variable by 12 and
stores the result in the inches variable. The sign for
multiplication in programming languages is not x
like in standard maths, but * If you
read the line in your mind it says "inches
equals feet times 12" which makes sense to me.
With calculations anything on the left of the equals sign
(=) is where the result is stored, anything on the right
is the calculation to create that result. The output from
the program is shown below :
Mathematical operators in C
Most of the above table is straight forward apart from the multiplication and division symbols. The last three entries ( modulus, increment and decrement ) may need explaining. The modulus of a division is the remainder left over and is represented in C by the percentage sign. For example 5 % 2 = 1 ( 5 / 2 = 2 remainder 1 ).. or .. 11 % 3 = 2 Note : When programming and using division or modulus you have to always be aware that dividing a number by 0 ( zero ) will cause a runtime error .. This is the worse kind of error, as the compiler will compile the code without any problems at all, but when the program is running it will crash with a divide overflow error. If you think about it, it is impossible to divide any number by zero, eg 10 / 0 = ? ( how many 0's in 10 ? ) impossible. The increment and decrement operators are used as an efficient way of adding or subtracting 1 from a variable. The following simple program and output shows the increment operator in action :
A quick question What will be the value of the variable num after this line of code ?
The answer is not 9, it is actually 7. If you don't believe me write a small program to prove it, or use a calculator and enter 1 + 2 x 3 = . The answer both the program and the calculator throw out will be 7 not 9. Why ? The answer lies with BODMAS ( Brackets, Ordinals, Division, Multiplication, Addition, Subtraction ) This order is the order that is used to find the value of equations both in computer programs and maths. First you work out the sums of anything in brackets, then you calculate the values of ordinals ( powers ), then divisions, then multiplications, then additions, and finally last of all subtractions. If you thought the answer was 9 then what you have done is work from left to right through the equation ( 1+2 = 3 * 3 = 9 ) If you follow the BODMAS rule then the 2 x 3 is worked out first ( because multiplication comes before addition BODMAS ). This gives the result 6, then finally the 1 is added giving the answer 7, which is correct. You can force the answer 9 by using brackets because they are worked out before everything else according to the BODMAS rule. eg num=(1+2)*3; This forces the 1+2 calculation to be worked out first because it's in brackets, then that value is multiplied by the final 3, this time giving the answer 9. |
Summary |
Another small
program, with a large description .. sorry. It isn't
important to understand the descriptions fully, as long
as you can look at the code samples and say "I know
what that does" then you're on track. In the
introduction, it was stated that programming requires
some maths, this page has shown it to be true :-( The
maths doesn't get a lot harder than this until a lot
later on, so there's no need to panic yet. The programs are now getting nearer to what computers were invented for, to perform calculations fast. The next program takes takes the maths a bit further and converts a value given in drams to pounds, ounzes and drams. |
Tasks |
5.1) Write the above program again to convert metres into centimetres ? |
(c) J.C.Spooner 2001