CIS
First BASIC Assignment
(due in 2 weeks) & Related Notes
Use your browser's back-arrow button to return

Contents:

(I) Homework #8
(II) Getting your hands on QBASIC
(III) Essential BASIC instructions

(I) Homework #8: Programming in QBASIC (or, if more convenient, some other simple BASIC system like GWBASIC, BASICA, etc)
Problem 1.
(a) Write a BASIC program (with graceful ending) for Weekly Payroll for exactly 10 employees. Define Hours & Rate as in the examples given in class. Use the following data:

Hrs

40

40

35

40

30

35

40

50

60

8

Rate

12.25

10

15

15

12.25

18

11

20

10

150

Calculate Gross pay as in class. The program must detect overtime (>40 hrs/week) and pay double the rate for hours beyond 40.; Withholding: 10% when Gross pay is £ $600 and 15% if >$600; Net pay: Gross less Withholding. Program prints a report with suitable columns headings for
Hours, Rate, Gross, Withholding, and Net -- one line per employee
(b) How would you modify the program to handle any number of employees?
(c) How would you modify the program to handle employee names?
(d) Write the simple payroll procedure, discussed in class, as a Word macro...
....here, you may want to use
InputBox rather than READ, see VB instructions under (III), below

Problem 2. Write a BASIC program to simulate a calculator. The program will ask the user for an operation code (A, S, M, and D representing Add, Subtract, Multiply, and Divide) and for two numbers and will display the result. In the following illustration the computer speaks in blue and the user in red:

Operation? M
Enter 2 Numbers? 4,5
Product = 20


(II) In Windows 95, you may invoke QBASIC by simply typing qbasic in the RUN dialog box of the Start menu. Should this not work for you, use any other BASIC system you can get hold of. Older computers may have GWBASIC or BASICA. You may also be able to get a copy of QBASIC at the lab (basement of Levin Hall). Ask the consultant and be sure to bring a diskette for the QBASIC program and its help files (will occupy half of a 1.44MB floppy).


(III) A list of some useful BASIC instructions follows. Required keywords are shown in boldface. Variable names (e.g. HOURS) & constants (e.g. 90, 10, etc) are arbitrary. Variables like HOURS are allotted a few bytes of RAM.Strings of text characters can be placed in String variables (e.g. NAME$) or between quotes (in Visual BASIC (VB), '$' not needed). In all cases, string values can be converted to numerical values with the Val(string) function. Conversely, numbers may be converted to strings with the Str$(number) function

Input

READ HOURS, RATE
INPUT "Prompt"; HOURS, RATE
INPUT#1 HOURS, RATE
S = InputBox("prompt","label") ....(VB)

Read data from DATA statement
Request data from user (keyboard)
Read data from file (see OPEN statement)
String variable
S receives entered text

Output

PRINT "Quarterly Report"
PRINT
"Hrs=";HOURS, "Rate=";RATE
LPRINT
HOURS, RATE, GROSS
MsgBox (S) ....(VB)

Print to the screen (a heading)
Print to the screen (numbers & strings)
Print to the printer
String variable
S shows in message box

Branching

GOTO 90
IF N=10 THEN GOTO 90

(Unconditional) "90" is a label
(Conditional) N=10 is the "condition"

Calculation

LET GROSS = HOURS*RATE
IF A>=0 THEN B=100+C

"Let" optional; * multiplication operator
(Conditional) Read >= as "greater or equal"

Counting

LET N = N+1

N is the counting variable

Summing

LET SUM = SUM+X

SUM is the summing variable

Looping

FOR N=1 TO 20 [STEP 3]
NEXT N

Start of loop; [STEP x, optional]
end of loop

Data

DATA 40, 12.25

Can be anywhere in prog.

Subscripted variables

DIM X[12]

Dimension: X is an array of 12 variables

File management

OPEN "filename" FOR INPUT AS #1
CLOSE #1

"filename" is a previously created file
Similarly, ...
FOR OUTPUT AS #2

String Variables

e.g. NAME$, ZIP$
DIM ADDRESS$[50]

For non numeric (text) data
may be dimensioned

Functions
...(small sample)

Val(string)
Str$(number)
Rnd
Int(number)

convert strings to numbers
convert numbers to strings
random number
truncate decimals of number