COMAL Introduction
Total Page:16
File Type:pdf, Size:1020Kb
introduction to computp.r proqramminq w ith comal 80 and the commodore 64/128 d william leary in-broducb ion "bo compu-ber programming with COMAL ©O and the commodore 64/128 j. william leary, ed.d. Cover design, illustrations and drawings: Roselyn Stewart Leary Acknowledgements Without the help and support of the following, this book still would be an idea to pursue "tomorrow": Dr. C. Holland, Superintendent of Schools, Pocahontas County, W.V.; Kenneth E. Vance, Principal, Pocahontas County High School; Dan Curry, Curriculum Development, Pocahontas County Schools; Cora Lee Wyatt, Treasurer; Gwennie Friel, Secretary; and, to these students for those tedious but essential tasks of proofing, duplicating and collating, Tammy Armstrong (first draft), Mary Kay Irvine (Girl Friday, final copy), Tina Roach, Carl Seielstad, Carol Arbogast, Angie Matheny, Kathy Roach and Delana Irvine. A special thank you to Len Lindsay, Editor, COMAL TODAY, for his supportive critique of the manuscript. (Any errors or obfuscous explanations remain the responsibility of the author. ) The author is also indebted to Robert L. Poland, Richmond High School (Indiana) --an unusually gifted teacher, for introducing him to COMAL in the Summer of 1984. Printed in the United States of America. Published by: COMAL Users Group, U.S.A., Limited 6041 Monona Drive Madison, WI 53716 Please note the following trademarks: Captain COMAL of COMAL Users Group, U.S.A., Limited; Commodore 64 of Commodore Electronics Limited; IBM of International Business Machines; Apple of Apple Computer; Copyright 1985 by J. William Leary Copyright 1986 by COMAL Users Group, U.S.A., Limited All rights reserved. No part of this book may be reproduced in any way or by any means, without permission in writing from the publisher. ii CONTENTS 1. introduction 1 2. mathematical operations 12 3. introduction to programming 21 4. computer management techniques 30 5. LIST and EDIT commands 39 6. introduction to Strings 46 7. more mathematical operations 54 8. more on Strings 67 9. FOR / ENDFOR loop 72 10. counting and summing 80 11. binary decisions: IF / THEN / ELIF / ELSE / ENDIF 87 12. binary numbering system 97 13. logic operators 106 14. READ - DATA 111 15. INPUT - numerics and Strings 126 16. REPEAT - UNTIL 135 17. nested loops 142 18. WHILE - DO 152 19. CASE - OF 158 20. procedures 163 21. arrays 181 22. String arrays 196 23. two-dimensional arrays 204 24. sorting 217 25. introduction to files 231 26. the graphic turtle 248 27. for further study 259 index 262 iii to goose and each faith matheny iv This book is written for the COMAL 80 cartridge. It may be obtained from: COMAL Users Group USA, Limited 6041 Monona Drive Madison, WI 53716 phone: 608 222-4432 v VI CHAPTER ONE d -n .-fc a r o dL o n . o t> o e c tiv e s At the completion of the chapter the student will be able to: 1. State what the acronym COMAL represents; 2. Tell what the difference is between a ROM and a RAM; 3. Place the cursor at any place on the monitor screen; 4. Show the difference between tapping the CLR HOME key alone and tapping it along with the SHIFT key; 5. Tell what the function of the RUN/STOP key is; 6. Define what a system reserved word is; 7. Use the operands + , -, /, * and t in simple programs; 8. Know how to implement the AUTO function for numbering lines; 9. Know how to use RENUM to change the numbering of lines; and, 10. Write a simple program to find the area of a polygon or circle. 1.1 COMAL — COMmon Algorithmic Language— is a powerful computer language. Though it has striking similarities with BASIC, it is an over simplification to dismiss COMAL as "just structured BASIC." The painful short comings of BASIC began to surface in the 70*3 and though there have been many (excellent) revisions, they remain just that --revisions. On the other hand, to say COMAL is a pre-Pascal language is to imply it lacks the power and sophistication of that computer language. COMAL, in 111 ©very aspect, can match (and exceed, in the author’s opinion) Pascal in clarity, structure and power. At this writing (1985) COMAL has been adopted as the official school language in seven European countries, the latest being Scotland. In Denmark, COMAL is used in the operation of its airports. 1.2 THE MARVELOUS TECHNOLOGY of the computer will be left to the engineers. The user, however, should understand two primary features: 1. The ROM; and, 2. The RAM. The ROM chip(s) cannot be changed by the user. ROM is an acronym for Read Only Memory. These chips have been programmed by the manufacturer and they are, essentially, what makes each computer distinctive from any other. ROMs are not interchangeable from one brand of computer to another. RAMs (Random Access Memory), on the other hand, can be programmed by the user — as long as he follows the sequence of logic built into the ROM by the designer. Many RAMs are interchangeable and can be purchased from any of several electronic companies. When the computer is turned off, what ever memory the user has "built into" the RAMS is lost. This is not true for the ROMs unless, of course, they are destroyed by a power surge or static electricity. 1 . 3 W H A T I S T Y P E D on the KEYBOARD is changed into machine language (a language based on binary numbers) by the CENTRAL PROCESSING UNIT. Once this has been accomplished, it is "operated" on by the instructions we have programmed into the RAMS. When this task has been completed, the final results are then changed back and displayed on the monitor screen or the printer. 1.-4: BEFORE STARTING, THERE are some things we need to become familiar with on the C=64 keyboard. When we turn on the computer we will note, at the end of the printed message on the screen, a flashing square light. This is called the CURSOR. By tapping certain keys, we can move it about the screen. In the lower right of the keyboard there are two keys. One has arrows pointing up and down with the letters C R S R in the middle. The other has the same letters only the arrows point left and right. By using these keys in conjunction with a S H I F T key we can place the cursor in any position on the screen. If we wish to move the cursor DOWN or to the RIGHT we do so without employing a SHIFT key; to move the cursor up or to the left we must keep the SHIFT key depressed. Because the CURSOR is so important, practice for several minutes moving it about the screen. Put it into the four corners, half way down one C2H side, then the other side, in the middle and, finally, middle top and middle bottom rows. The cursor is the best servant we have. Commodore has designed their ROMs so that we can edit programs (using the cursor) while they are still on the screen --even to changing just one letter or one number. More on this later. Every time we finish typing a line we must tap the R E T U R N key. By doing so we are 'telling1 the computer, "This is what I want. Do it." It may not always agree. We may have asked it to do something it is not programmed by the ROM(s) to do; in which case, it will flash an error mess.age on the screen before allowing us to type further. This is an outstanding strength of COMALi And before we have finished with this book we will come to appreciate it more and more. There are other ways of moving the cursor. If we tap the I N S T / D E L key the cursor will move one position to the left. If there is a character there, it will be erased (a form of editing). If we wish to move the cursor forward with the I N S T / D E L key, then we use it in conjunction with a S H I F T key. We can also use these two procedures to move text back and to push text forward. Tapping the C J L R / H O M E key will move the cursor to the upper left hand corner of the screen, WITHOUT ERASING TEXT. Tapping this key ALONG with a S H I F T key will move the cursor to the upper left hand corner A N D will clear the screen at the same time! The R U N / S T O F key does just that. When a program is running and we wish to stop it, then tapping that key will do so. There are times when the computer "locks up" and depressing the R U N / S T O F key WHILE tapping the R E S T O R E key will often UNLOCK it. Computers are "funny", though, and no matter what we try (short of a sledge hammar), nothing short of turning the computer off and then on seems to work in the effort to "unlock" them. This is why we should save what we are doing after every few lines. Another important key is the long one at the bottom of the keyboard. It is the space bar. It functions the same as the space bar on a typewriter --with one nice exception, it can erase! Type a few characters on the screen and WITHOUT tapping the R E T U R N key, move the cursor back to the beginning of the line using the SHIFT key in conjunction with the left and right arrow CRSR key.