First of all, the answer to last time's question...
#include <stdio.h> int main(void) { float time,speed,distance; speed=2.98E8; speed*=60; distance = 150E6; distance *=1000; time = distance / speed; printf("Time = %.2f minutes\nTime = %.2e minutes",time,time); return 0; }
So what's the program doing?
Firstly, initialise three floating point variables and assign the appropriate values to them. These values are then converted to seconds or meters. The time taken is calculated by rearranging the formula given and finally the output given. The %.2f means to print to two decimal places past the point. The screen should give
Time = 8.39 minutes Time = 8.389261 e +00 minutes
I told you it wasn't that hard! Anyway, let's get on to the business at hand...
Normally, if you had a line such as
IF a%2 = 0 THEN
it would mean that if there is no remainder when a is the modulus of 2 then do something. In C, this can be written the same way, but it is also possible to make this into a short-hand style. A zero is a false value. Therefore, a line such as
if (a%2)
is quite acceptable. If the condition holds (i.e. no remainder), the next stage is processed. Likewise, if the line read
if (!a%2)
then the next stage would be processed if there were a remainder.
These simplifications make working with C not only simpler to understand and debug but, with the fewer instructions, much faster.
C has three main flow statements and three unconditional flow statements.
{ start : printf("Hello") ..... ..... if (a == 3) goto start }
Goto statements should be avoided as they make the task of debugging a potential nightmare.
{ do { a++; printf("%d",a); if (a%2) continue; printf("Not printed if a/2 <> int a/2"); } while (a<100); }
switch (option) { case 1 : do something break; case 2 : do something else break; }
The three conditional flow statements are if .. else, do .... while and ?.
IF a = b THEN.
In C, this has to be
if (a == b)
and there is also no THEN statement.
Unlike Basic, there's no ENDIF statement. The IF statement is assumed to be terminated when the conditions have been met or a BREAK command has been encountered.
The format of the IF ELSE statement is
if (variable <condition> variable) { } else { }
For instance, if I wanted to test if a variable was >1 or <0, then act on it, the code may look like this:
if ((a > 1) || (a < 0)) { printf("The value is >1 or < 0"); } else printf("The value is 0");
The format is:
switch (<variable>) { case <cond_1> : do something case <cond_2> : do something }
In the format given above, the program will test for all conditions; if it finds the correct one (say cond_1), then this is executed. However, as there is no reason to come out of the testing loop, the program will go on to execute all the other conditions. To exit after a condition has been met, the BREAK command is placed after each line.
case <cond_1> : do something break;
This will terminate the testing loop.
<variable> <condition> ? (a) : (b) ;
In other words, it's an IF THEN ELSE statement for C. As part of a piece of code, it would look like this:
a == 2 ? printf("a=2") : printf("a<>2");
in other words:
if a = 2 then print "a=2" ELSE print "a<>2"
These condition statements don't just apply to numbers - they can also apply to text strings. However, I've not enough time to cover text strings in this part - they're next!
Design and write a program which puts up a simple number menu allowing you to add, subtract, modulus, divide and multiply, obtain the inputs, act on it and then print the result. The numbers should be integers.
The final element of the menu is a quit option. To put this into the code, use the following fragment for the input:
At the start (but after the setting of the variable types) begin with a do {.
At the end, have a close } followed by the line:
while (<var> !=6);
This will loop around unless you have asked to quit. The <var> is just a variable name.
The program must also have an error check to ensure that you are not dividing by zero.
Source: | Archive Magazine - 13.3 |
Publication: | Archive Magazine |
Contributor: | Paul Johnson |