Just a sample of the Echomail archive
Cooperative anarchy at its finest, still active today. Darkrealms is the Zone 1 Hub.
|    CBM    |    Commodore Computer Conference    |    4,328 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 1,613 of 4,328    |
|    Stephen Walsh to All    |
|    Commodore Free Magazine, Issue 91 - Part    |
|    19 Mar 16 11:21:50    |
      **************        ALGORITHMS FOR COMMODORE BASIC #01        By Commodoreman       *************************************              (c) 2015 Commodoreman              I was looking through various books and took an interest to mathematical       formulas. Being interested in Commodores, I wondered how I could convert       them into BASIC algorithms. So, I started working out the conversions. I       know there are many programs that do these conversions, but I thought,       "what the heck!", I will just go ahead and do them anyway. Maybe I will       cover some that have not been done yet, or find a better way of doing them       (that and it is a great way for me to improve my programming and math       skills).              This is a first in a series of articles and is not meant to be a how-to on       general BASIC programming. Having a background will be beneficial, though,       not essential. If you are learning BASIC, you should be able to figure out       what is going on. I will be focusing more on the algorithms and their use.       I will admit, too, that I am not much into math, so there may be errors. I       do not expect to follow any set or organized method of approach when       choosing formulas - I will likely just pick one or a related ones at random       and just go with it.              I am going to start the series with temperature conversions. It is a nice       way to get some of the basics out of the way (I noticed there are formulas       that use temperatures and these algorithms can be incorporated into them).       I will use a functioning BASIC program (BASIC 7.0) to illustrate one way       they could be used. Of course, math being math and BASIC being BASIC there       are different approaches that could be made, so you will have to decide on       what works best for you.              Also note that when I use superscripts around formulas, I will space them       out a little to prevent confusion.              TEMPERATURE CONVERSION PROGRAM              Here are the formulas:              1. I found two ways to convert Fahrenheit to Celsius...              (F-32) / 180              &              Fahrenheit = Celsius x (9/5) + 32              In the first one(1), I found that by multiplying the results by 100 gives a       whole number. However, I decided to use the second(2) formula for the       program.              2. Celsius = (5/9) x (Fahrenheit -       32) (2)              3. The algorithm for Rankine was       devised from the following table (3):               Fahr. Cent. Kelvin Rankine       (A) 212 100 373 671       (B) 32 0 273 491       (C) 180 100 100 180              (A) Boiling point of water       (B) Melting point of ice       (C) Divisions between fixed points              4. Kelvin = Celsius + 273 (3)              Variables used in the algorithms:              * F - Fahrenheit       * C - Celsius       * R - Rankine       * K - Kelvin              BASIC algorithms...              1. F=C*(9/5)+32       2. C=(5/9)*(F-32)       3. R=F+459       4. K=C+273              Here is the BASIC program...              1 rem ***************       2 rem ** main menu **       3 rem ***************       10 scnclr       20 print:print"temperature conversion        program"       30 print:print"1. convert fahrenheit         to celsius/kelvin/rankine"       40 print"2. convert celsius to fahren        heit/kelvin/rankine"       50 print"3. convert kelvin to celsius        /fahrenheit/rankine"       60 print"4. convert rankine to fahren        heit/celsius/kelvin"       70 print:print"5. end conversion prog        ram"       80 get a$:if a$="" then 80       90 a=val(a$)       100 on a gosub130,230,330,420,540       110 if z=1 then z=0:goto 10       120 goto 80       121 rem *************       122 rem ** option 1**       123 rem *************       130 scnclr       140 input"what is the temperature in         fahrenheit";f       150 c=(5/9)*(f-32)       160 k=c+273:r=f+459       170 print"converted temperatures -"       180 print"celsius: ";c       190 print"kelvin: ";k       200 print"rankine";r       210 gosub 510       220 z=1:return       221 rem **************       222 rem ** option 2 **       223 rem **************       230 scnclr       240 input"what is the temperature in         celsius";c       250 f=c*(9/5)+32       260 k=c+273:r=f+459       270 print"converted temperatures -"       280 print"fahrenheit: ";f       290 print"kelvin: ";k       300 print"rankine: ";r       310 gosub 510       320 z=1:return       321 rem **************       322 rem ** option 3 **       323 rem **************       330 scnclr       340 input"what is the temperature in         kelvin";k       350 c=k-237:f=(c*9/5)+32:r=f+459       360 print:print"converted temperature        -"       370 print:print"celsius: ";c       380 print"fahrenheit: ";f       390 print"rankine: ";r       400 gosub 510       410 z=1:return       411 rem **************       412 rem ** option 4 **       413 rem **************       420 scnclr       430 input"what is the temperature in         rankine";r       440 f=r-459:c=(5/9)*(f-32):k=c+273       450 print"converted temperatures -"       460 print"fahrenheit: ";f       470 print"celsius: ";c       480 print"kelvin: ";k       490 gosub 510       500 z=1:return       501 rem *****************************        *       502 rem ** press any key subroutine *        *       503 rem *****************************        *       510 print:print"press any key..."       520 get a$:if a$=""then 520       530 return       540 end              Kelvin is an absolute temperature scale based on centigrade, Rankine is the       absolute scale based on Fahrenheit. Even though these absolutes might not       be used in everyday situations, it might be advantageous to have them       available for more sophisticated calculations.              In this program a menu is presented offering the options needed to convert       throughout the temperature scales. It seemed to me a reasonably organized       approach to the application of these algorithms.              Converting from a formula to a BASIC algorithm takes breaking down the       formula into a series of steps that BASIC understands. Typically this is a       logical series of steps where calculations are performed in series. The       formulas for these conversions are rather simple (just wait for the more       complex ones!). The proper order in which the algorithm is calculated is       organized by using parenthesis to force BASIC to perform the calculations       in proper order (I know for those familiar with BASIC that this is simple       stuff, probably learned long ago).              Breaking down the second conversion formula to the algorithm:              Fahrenheit = Celsius x (9/5) + 32              This is a simple conversion. All that needs to be done here is to follow       the order of operations in the formula to obtain the algorithm:              F=C*(9/5)+32              We assign variable 'F' with the results of our conversion. Division within       parenthesis is performed first. Then this is multiplied by the value       assigned to variable 'C', to which we then add 32. There are no issues       with the way the microprocessor crunches the numbers. As just a note,       there are some algorithms (not in this project) that have to be processed       in steps (not in just a one-line algorithm like that above). The       6502-based processors are great, but not impervious to errors. I'll       illustrate this in a future article(4).              After figuring out how to turn the formula to a BASIC algorithm, the next       thing to do is incorporate it into the BASIC program. Logical steps could       be:              1. Query the user on what temperature scale is known              2. Perform the calculation (assigning values to their proper variable)              3. Print it out in a useable and meaning form              4. Test for correctness              Note: In a more sophisticated program, we would likely have code for       printing the results to a printer and possibly saving to an array for other       uses. If you wanted to, the results could even be saved to a safe spot in       RAM for a different BASIC program to access (providing the computer is not       reset). As far as testing for correctness, the books I use have examples       where I will test the algorithm based on the information provided. If the       results are the same (some answers are rounded off - I generally ignore       this) I assume I have a correct and functioning algorithm. REM statements       are there just for pointing out the segments of the program - no need to       actually add them to the program.              For quick reference, the following lines are the algorithms:              150, 160, 250, 260, 350, 440              REFERENCES:              1. Hausmann-Slack. Third Ed. 1948. D. Van Nostrand Company, Inc. Page 246.              2. Magic & Mastery. Third Ed. 1967. D. Van Nostrand Company, Inc. Page 160.              3. Hausmann-Slack. Third Ed. 1948. D. Van Nostrand Company, Inc. Page 245.              4. Science and Engineering. Abacus. p 64-70.                            Editor: Although written for the Commodore 128 if you change lines              10 scnclr       130 scnclr       230 scnclr       330 scnclr       4              --- MBSE BBS v1.0.4 (GNU/Linux-i386)        * Origin: Dragon's Lair ---:- dragon.vk3heg.net -:--- (3:633/280)    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca