(from ZX Computing Aug/Sep'84) Conversions ----------- A program with many uses from kitchen to work shop from Jonathan Court of Poole. -------------------------------------------------- This program alows the user to convert, for instance, a length measured in yards, into metres. The 'length' option includes twelve units of length from millimetres to miles and any one can be converted to any other, so any length expressed in units of mm, cm, m, km, inches, feet, yards, fathoms, poles (rods), chains, furlongs or miles, can be easily converted into the units of your choice. This section of the program is very useful for anyone who prefers miles to km, inches to cm, or vice versa. The program has 6 sections. As well as length, it allows conversion between units of area, volume, liquid, capacity, weight and temperature. All calculations are performed to an accuracy of 7 significant figures using conversion factors which have been painstakingly checked. The program is menu-driven, and fig.1 shows a screen dump of the menu. The user enters the number he requires, and one of six lists results (see fig.2). You are then required to enter the number of the units that you wish to change. Let us suppose that you wish to convert 10 cm into inches. You enter "2" in reply to the prompt "FROM?", and "5" in reply to the prompt "TO?". The screen will then appear as shown in fig.3. You are required to enter the number of cm, 10, in our case, and this is immediately converted into inches and displayed to seven significant figures (fig.4). Pressing "L" will return the user to the list of lengths and the "FROM?" prompt, and pressing "M" will return you to the menu. This pro- cedure is the same for the other sections. The temperature section only contains three units of temperature: degrees Centigrade, Fahrenheit and Kelvin. This is almost the same as before, the answer being given to two decimal places, since seven-figure accuracy seemed inappropriate. An example temperature is shown in fig.5. Any of the six sections, along with the subroutine at the end of the program (not necessary for temperature) could be used as a program on its own, and my routines for giving significant figures and decimal places, both of which are explained later, could easily be used in other programs. The method I have devised for converting between units is a simple one, and will not produce answers such as 11.99998 when the answer ought to be 12 or whatever. This is achieved by using only one complicated conversion factor, the others being simple powers of ten of the number (for metric units) or simple whole number multiples or reciprocols for imperial units. Hence the answers will always be exact and correct to at least seven significant figures, should this be required. Program notes ------------- Line 8 This provides a neat method of clearing a line whenever necessary without having to type in a line of spaces. Lines 10-120 Here, the menu is printed on the screen. Lines 130-150 This INPUTs the number, checks it for mistakes, and calls the relevant section of the program. Line 1000 This is the start of the section dealing with length conversion. Line 1010 This is the conversion figure used to convert between metric and imperial lengths. It is actually the number of yards in a metre. Line 1050 An array is set up to hold the twelve units of length. Line 1060 This array holds the twelve conversion factors. In this array, every unit of length is expressed in terms of yards. This involves numbers, e.g. 1760 for a mile, for imperial lengths, and the conversion factor from line 1050 multiplied by a power of ten for metric lengths. Lines 1070-1300 The arrays are then filled with the appropriate entries. Lines 1310-1360 The list of lengths is printed on the screen. In line 1350, the part between the brackets prints a space before the number if it is less than ten. This makes for a neater screen layout. Line 1370 This sets the line on the screen at which the prompts will start Line 1380 This is the number of lengths. The subroutine being called by line 1390 needs to know this so that it can check for invalid entries. Line 7000 (This is described here for convenience) This line prints the prompt at the line specified by 1370. This position varies according to the length of the list of units in the section. Lines 7010-7030 The unit to be converted from is entered, checked for invalidity, and displayed next to the word "FROM", over the question mark. Lines 7040-7070 The unit to be converted to is chosen, checked and displayed. Lines 7080-7100 The actual length is entered and checked in case it is zero. Entering zero would be pointless unless it was a temperature, and the temperature section does not use this subroutine. Line 7110 The three (or possibly four) lines just used are cleared using S$ (line 8). Lines 7120-7130 The original length is displayed along with "IS EQUAL TO">. Line 7140 This is a very important line. The length (X) is multiplied by the necessary number to give it in terms of its new units. This number is obtained by dividing the value in B (the lengths expressed in yards) of the final units by the value for the original units. The outcome, Y, is the answer. It may be acceptable as it is, but it may be recurring or expressed to a number of decimal places greater than the accuracy of the conversion figures. Lines 7150-7160 This routine determines the final number of significant figures. The two (6-Z)s give seven significant figures. Any number, x, can be used in place of 6, and the number Y will be reduced (if ncessary) to x+1 significant figures. Lines 7170-7180 Since the ZX81 prints numbers smaller than once as .xyz rather than 0.xyz, I have included these two lines to add a O where necessary to tidy up the answer. Line 7190 The answer is printed. Line 7200 We now return to line 1390 Lines 1400-1410 Instructions to proceed are printed at the bottom of the screen. Line 1420 The program waits until a key is pressed. Lines 1430 In order to check validity, a variable is assigned. This is probably not necessary, but if the validity checking is more than a couple of lines, it is possible to take one's finger off the appropriate key before the computer has finished. Lines 1440-1460 The user is returned to the list of lengths or to the menu. 1460 returns to 1410 if an invalid key has been pressed by mistake. Line 2000 This is the beginning of the section dealing with area conversion. The structure of this section is exactly the same as the length section. The number in line 2010 is the number of square chains in 1000 square metres. Line 3000 This is the beginning of the volume section. This is also the same as the length section, and the number in line 3010 is the number of cubic inches in 100 cubic centimetres. Line 4000 This is the liquid capacity section. The number in line 4010 is the number of pints in a litre. Line 5000 This is the section dealing with weight. The number in line 5010 is the number of pounds in a kilogram. I have used the English spellings of the metric weights rather than the French spellings which seem to have fallen into disuse. Line 6000 This is the temperature section. It is not the same as the others because simple scalar conversion factors cannot be used since none of the scales share the same zero point. Lines 6010-6040 The names of the scales are put into the array A$. Lines 6050-6090 The list is printed on the screen. Lines 6100-6240 This is similar to the other sections, and allows the user to choose from and to which he is converting, and to input his temperature. Lines 6250-6260 The temperature is converted into degress Centigrade if it was originally in Kelvin or degrees Fahrenheit. Lines 6270-6280 The temperature, now in degrees Centigrade, is here converted to the units of your choice if they are not degrees C. Line 6290 This line reduces the answer to two decimal places. The two 100s will give two decimal places. 1000s would give three, and so on. Line 6300 The answer is printed by this line. Lines 6310-6380 This sends the user wherever he now wishes to go, as in previous sections. Lines 7000-7200 This is the subroutine used in sections one to five and described with section one. Lines 8000-8010 This SAVEs the program so that it will RUN automatically on loading. The program should be SAVEd by: RUN 8000 Spectrum compatibility ------------- To the best of my knowledge, this program will run if typed into any Spectrum. It should be noted that the ** appearing twice in line 7160 is not two *s, but the ZX81 version of the symbol, found on the 'H' key.