AGRI (CDAE) 85 COMPUTER APPLICATIONS Lab 3: PROGRAMMING

DISCLAIMER:  The Lab Notebook is meant as a quick summary of assignments.  It is not meant to be a complete users manual, or recipe book that tells you the commands you use to complete the assignments.  You need to come to lab and take notes to be able to complete the assignments.

ANTI-VIRUS SOFTWARE

Computer viruses are a fact of computing life.  The more you use personal computers, the more likely you will encounter a virus.  Viruses are programs written by people as pranks, jokes, or vindictive revenge.  The most benign copy themselves from disk to disk without telling you.  The more virulent erase or re-format you disk and you loose all your programs and personal files.  Viruses are to be avoided.  Most public computer labs have software that searches and destroys virus.

On-campus, antivirus software can be downloaded and installed from http://cit.uvm.edu/software/ .  Scroll down to the Operating system on your PC, and click on the green underlined file on the left-hand column.  This will prompt you to download an installation file to your hard disk.  To install, close all applications on your PC, find the file and double-click on it to begin the installation.  Follow the instructions on the screen.
 
 

Programming in Quick Basic:

Why are we learning the QBASIC Programming Language?

1. To understand that all application software is created using a programming language.

2. To understand how the computer interprets the QBASIC commands into binary instructions.

3. To experience the elementary functions of input & output with the computer.

4. To learn how the computer reads and executes commands from the top down.

5. To be creative, learn to trouble shoot, be patient, discover multiple solutions to a problem, and have fun.

 Here's the link to the qbasic.exe and qbasic.hlp files that you need to download to your PC.  Then click on QBASIC.EXE to launch the qbasic interpreter (blue screen).

PROGRAMMING

1. Definition: a program is a set of source code instructions written in a programming language. They must be detailed, exact, and completely unambiguous. Source Code: the program instructions indicating to the computer how the program is to be carried out. The source code can contain "remark" statements which are not executed by the computer and are notes or internal documentation for the benefit of the human programmer.

2. Languages: Machine language or binary =0101001110010100100111001101011 translator programs: compilers and interpreters are software that take source code and translate it into binary machine language so the machine can run or execute the program. high-level programming language examples: Fortran, Cobol, Pascal, BASIC, LISP, APL, LOGO, etc.  BASIC stands for: Beginners All-Purpose Symbolic Instruction Code and was developed at Dartmouth College.

3. Getting started in BASIC: First you must load the BASIC interpreter into RAM. Do this double click on the file QBASIC.EXE.  ENTER will show you the survival-help guide, Esc gets you to the edit-line where you can start typing in your source code. Once BASIC is loaded, you may use your mouse to pull-down menus. If you have copied the file QBASIC.EXE and QBASIC.HLP files onto your working diskette as recommended, you may easily do this week's lab on almost any IBM or compatible computer on campus.

4. Program statements: Example program source code:

    10 rem -- A simple program to draw circles --
    20 screen 9
    25 color 15,1
    30 cls
    40 a = 300
    50 b = 50
    60 c = 50
    70 for I = 1 to 20
    80 circle(a,b),c
    90 a = a + 5
    100 b = b + 2
    110 c = c + 3
    120 next I
    130 end

EXPLANATION: Program statements (lines of commands) are executed sequentially (from the top down); common numbering sequence is in increments of 10 to allow for addition of other statements at a later time. QBASIC does not require you to use line numbers.

Statement 10 is a remark (REM) statement; remark statements are not executed, therefore anything can follow REM as part of the source code. Remark statements are recommended for internal documentation of the program. For example, REM statements could be used to identify the author of the program, the date, a title and other important information about what is going on inside the program.

Statements 20, 25 and 30 set up the screen: screen 9 tells the computer there are approximately 640 pixels across the screen and 350 down and sets the foreground color to 15 (high-intensity white) and the background color to 1 (Blue). Line 30 (CLS) clears the screen before proceeding with next statement.

Statements 40 through 60 name variables to be used later in the program and give them an initial value.

Statements 70 and 120 identify the beginning and the end of the FOR...NEXT loop, one of several ways of repeating instructions in a program. The counting variable is identified by the letter I. In this case the count is from 1 through 20. The statements bracketed by the FOR (line number 70) and NEXT (line number 120) statements are executed sequentially once for each count. In this case, the FOR...NEXT loop is executed 20 times.

COLOR CHOICES: 0=Black, 1=Blue, 2=Green, 3=Cyan, 4=Red, 5=Magenta, 6=Brown, 7=White, 8=Grey, 9=Light Blue, 10=Light Green, 11=Light Cyan, 12=Light Red, 13=Light Magenta, 14=Yellow, 15=High-intensity White

Line Statement 80 above is the heart of this program. Because of this statement, a circle will be drawn on the screen with the center located at coordinate values a and b; "a" how far across the screen from left to right with 0 = the left edge of the screen, and, "b" how far down with 0 = the top edge of the screen. A negative value for "a" will create a center point completely off the screen to the left and a negative "b" will place the center point completely off the screen above the top of the screen. The letter "c" represents the value of the radius of the circle. The first circle in this program has coordinates of 300 and 50 as per program statements 40 and 50. The radius will be 50 as initially defined by statement 60.

Some variations on the circle command to try:
            To make an Ellipse: CIRCLE(A,B),C,,,,5/18 (or some other X/Y)
            To make an Arc: CIRCLE(A,B),C,1,0,3.141593/2
            To make a Ray: CIRCLE(A,B),C,1, -0.001, -3.141593/2

Statements 90 through 110 change the values of the variables a, b and r by the amounts shown each time through the loop. With this in mind, what will be the values of the coordinates and radius of the second circle of the series and the last circle of the loop?

Statement 130 identifies the end of the program.

5. Some useful things to know: ** Important: be sure to SAVE your program before clearing memory; this command is used before creating a new program or retrieving another existing program. HOLD then TAP Ctrl - Scroll Lock/Break These two keys in combination are used to stop program before the end of execution or to stop the list of the source code. (Hold the Ctrl key down and then tap the Scroll Lock/Break key.)

6. Screen graphics: coordinate references: 0,0 at upper, left corner of screen positive number for horizontal position to right positive number for vertical position from top down resolution: a measure of the accuracy or fineness of detail in a picture or display device. pixels: picture element. A pixel is the smallest display element on the screen.

SCREEN followed by a mode number (see statement 20 of example program source code). Mode options available in lab are: 0, 1, 2 or 9. COLORS can only be used with screen 9.

           0 for text mode,

1 for medium resolution 320 divisions horizontal; 200 div vertical [Note: if, for this screen, you have a number greater than 320 for horizontal you are off the screen to the right and if you have a vertical number greater than 200 you are off the bottom of the screen. If you have negative numbers for either, you are off the screen to the left and/or off the screen to the top.]

2 for high resolution 640 divisions horizontal; 200 div vertical 9 for super high resolution and Color. 640 divisions horizontal; 400 div vertical.


7. Printer control: To print source code in QBASIC: Pull down the file menu and choose print.

TRY THESE COMMAND STATEMENTS: Some variations on the circle command to try:
        To make an Ellipse: CIRCLE(A,B),C,,,,5/18 (or some other X/Y)
        To make an Arc: CIRCLE(A,B),C,1,0,3.141593/2
        To make a Ray: CIRCLE(A,B),C,1, -0.001, -3.141593/2

To draw circles of different colors:
        D=0
        E=1
        FOR I=1 TO 10
        CIRCLE(A,B),C
        COLOR E,D
        A=A+20
        E=E+1
        NEXT I

To draw lines: LINE (A,B)-(C,D)

To make a box, make a line, and then put the color and comand BF (box fill).  Give coordinates for the top left, and bottom right corners of the box, such as (160,100)-(320,150), then the color number, followed by BF:

        LINE (160,100)-(320,150), 2, BF

To fill-in the screen with color: PAINT (A,B)

To make sounds: SOUND frequency,duration

    For example, to have a continuously rising sound:
            A=37
            B=.5
            FOR I=1 TO 100
            SOUND A,B
            A=A+1
            NEXT I
 

Notes in a C Major Scale
Note Frequency Note Frequency
C 130.81 C* 523.25
D 146.83 D 587.33
E 164.81 E 659.26
F 174.61 F 698.46
G 196.00 G 783.99
A 220.00 A 880.00
B 246.94 B 977.70
C 261.63 C 1046.50
D 293.66 D 1174.70
E 329.63 E 1318.50
F 349.23 F 1396.90
G 392.00 G 1568.00
A 440.00 A 1760.00
B 493.88 B 1975.50

* this is middle C on the piano.

Doubling a frequency makes a note approximately one octave higher. Halving a frequency makes the note sound approximately one octave lower.
 

Here's a random number generator:
        A = INT(RND * 500)

This source code draws circles and fills them in with different colors:
SCREEN 9
COLOR 1
CIRCLE(100,100),50
PAINT (100,100)
COLOR 2
CIRCLE(201,100),50
PAINT (201,100)

The command SLEEP (followed by a whole number) is used as a pause (to temporarily have the program stop and wait for a few seconds SLEEP 5 pauses for 5 units of time, SLEEP 2 pauses for 2 units. The time units are different depending on how fast your machine is. The sleep command will be shorter duration on a fast machine (700 Mhz), than on a slow machine (233 Mhz).

ANIMATION: The idea behind animation is you draw an object, pause the computer, then erase the object, and re-draw it in a different position. The easiest way to erase the object is use the cls or clear-screen command. However, you can also have a background color, draw the object, pause it, then change the color to the background color. Then re-draw the object the same color as the background, so it seems to disappear into the background. Then you may re-draw the object in a different place.

ASSIGNMENT:

Create a program. Be as creative as you can, you get points for creativity on this assignment. Tell a story in pictures. Incorporate sound if appropriate. NO VIOLENCE!! Due next week: a diskette with your program file source code. WRITE your name and lab section and the filename of the program on the diskette label. Scan your diskette for viruses before you hand it in (30 points, 0 credit if you hand in an infected diskette). This assignment will take more time to complete than the last two assignments. Start early and work on your program at least a little every day. REMINDER: Bring an extra diskette to lab next week.

Bonus Points for outstanding work!

NOTE: it is OK to encorporate SOME code from web sites or books into your program that you hand in for lab 3.  BUT, your program must be original.  Do not just copy some code and hand it in as your assignment.  You must demonstrate that your program is original and tells a unique story and that you typed in most of the code using commands we learned in lab.

 Return to Lab Notebook Table of Contents