INTRODUCTION TO
COMPUTER PROGAMMING IN QBASIC LANGUAGE
OVERVIEW:
There
are two facts that dive human reasoning and thoughts. Basically, human beings
cannot speak or write in computer language and computers cannot speak or write
in human language, therefore an “Intermediate” language had to be developed to
allow people to communicate with the computers. These intermediate languages
are known as programming language which allows computer programmers (Users) to
direct the activities of the computer. Thus, programming languages are
structured around unique set of rules that direct the computer to perform a
specific task. A typical instruction in a programming language consists of two
parts:-
1. An
operation code
2. Some
operands
The
operation code tells the computer what to do such as add, subtract, multiply
and divide. The operands tells the computer the data items involved in the
operations. E.g. considering the instruction a = b + 5, the ‘=’ and ‘+’ are
operation codes while ‘a’, ‘b’ and ‘5’ are operands. Some computer uses many
types of operation codes in their instruction format and may provides several
methods for doing the same thing.
Nevertheless,
there are four basic types of instruction namely:
1. Input
– output instructions
2. Arithmetic
instructions
3. Branching
instructions
4. Logic
instructions
Summary:-
An input
instruction directs the computer t accept data from a specific input device and
store it in a specific location. An output instruction tells the computer to
move a piece of data from a computer storage location and record it on the
output screen. All of the basic arithmetic operations can be operated by the
computer. Branch instruction causes the computer to alter the sequence of
execution of instruction within the programme. Logic instructions allow the
computer to change the sequence of execution of instruction, depending on
conditions built into the programme by the programmer.
TYPES OF PROGRAMMING LANGUAGE
The
effective utilization and control of a computer system is primarily through the
software of the system. Just as system software directs the internal operations
of the computer while applications software allows the programmer to use the
computer to solve user made problems.
Programming
languages which means the primarily means of human vs computer communication
which have evolved the early stage programmers enter instruction into the
computer in a language similar to that used in the application software.
Computer programming language can be classified into the following categories:
i) Machine
Language
ii) Assembly
Language
iii) High
level symbolic Language
iv) Very
high level symbolic Language
Summary:-
Machine
language programmes allows the programmer to take advantage of all the features
and capabilities of the computer system for which it was designed. A machine
language is computer dependent. Thus, it is the first generation computers
language.
Assembly
language is as well known as the low level programming language. This allows
the programmer to remember the operations codes easily than when expressed
strictly as binary numbers. It must be translated into a machine language using
a system software programme called an assembler. They are the second generation
computers languages.
High
level language are machine independent, i.e. it can run on a variety of
computers. Its instructions do resemble English language statements and the
mathematical symbols are the symbols used in ordinary mathematics. Examples are
fortran, basic, cobol, pascal, algol, ada and pi/i. They are the third
generation computers language. Very high
level language are the fourth generation computers language that allow the user
to specify what they require, rather, than describing the procedures by which
requirements are met. It also offers the user an English-like set of commands
and simple control structures in which to specify general data processing or
numerical operations.
PROGRAMMING MICROCOMPUTERS IN BASIC
LANGUAGE
There
are some computer languages which are capable of supporting both scientific and
business data processing efficiently and effectively. An example of such
computer language is basic.
Basic
programming language is an easy- to-go use “friendly” language whose
instructions resembled elementary algebraic formular augmented by certain
English Language keywords such as input, read, print, Goto, if, then and so on.
WHAT DOES QBASIC STANDS FOR?
It is an acronyms for:
Q - Quick
B - Beginner’s
A - All-purpose
S - Symbolic
I - Instruction
C - Code
DEFINITION:
QBASIC
is one of the many versions or dialects of the basic language which retains the
user friendly environment characterizes by the implementation of basic
language. This means that QBASIC runs on the windows to facilitate the storing
of coded programmes in the secondary memory for retrieval with the ability to
support real integer and string data typed. It also contains necessary tools
that are required for the manipulation of matrices, graphs and images.
A BRIEF HISTORY OF QBASIC
QBASIC came into existence in the year 1960’s. It was developed by
two mathematical professors and technicians named Prof. John Kemeny and Prof.
Thomas Kurtz, at Dartmouth College, Hanover, NH 03755 USA (603) 646-1110 (E-mail): contact@dartmouth.edu
The story goes that the students were having difficulty learning
FORTRAN so the professors decided that creating a language from scratch would
be an excellent teaching tool for the students. The result was BASIC, which was
eventually placed in the public domain.
On the contrary, QBASIC is a programming language invented and modified by Bill Gates and Hitler designed to make a program that would
launch missiles from outer space in hopes of destroying the cancer
that is killing /b/. Unfortunately, since the earliest versions of
QBASIC could only display text, they had to settle for a word processing
program that could WRITE SHIT ON YOUR SCREEN
instead.
Bill Gates and Paul Allen had something
different in mind. In the 1970's when M.I.T.S.'s Altair personal computer was
being conceived Allen convinced Gates to help him develop a Basic Language for
it. When M.I.T.S. answered with interest, The future of BASIC and the PC began.
Gates was attending Harvard at the time and Allen was a Honeywell employee.
Allen and Gates licensed their BASIC to M.I.T.S. for the Altair. This version
took a total of 4K memory including the code and data used for a source code. Gates and Allen then ported Basic to other
various platforms and moved back to their hometown of Seattle where they had attended grade school
together. It was at this time that the Microsoft Corporation began it's reign
in the PC world.
WHAT IS QBASIC
PROGRAMME ALL ABOUT (AIMS / FOCUS OF QBASIC)
1. It is a friendly people – orientated
easy to learn programme and fun to use.
2. It is operated in an ‘English-like’
commands
3. It is very powerful and popular- high
level programming language.
4. It runs on all microcomputers.
5. It provides enough steps for coding a
sample progrommes.
6. It solves practical mathematical
equations applicable to scientist mathematics.
7. It allow the user to write and play
around QBASIC
8. Its operations share similar ideas on
how to operates other basic programmes such as visual basic, c plus, java, etc.
MORE ADVANCED
ADVANTAGES OF PROGRAMMING LANGUAGE
1. It is friendly, that is, it is
people-orientated. It is easy to learn and fun to use. Any well – organized
person can learn to programme in BASIC.
2. It is very flexible, allowing the
programmer to develop new programmes and to alter existing programme with
relatively little effort.
3. It is well suited for use in an
interactive environment. This includes dedicated microcomputer applications as
well as large computer time sharing applications.
4. It is universally available, on both
large and small computers. It has become the standard programming language for
most microcomputer applications.
5. The commonly used features of BASIC are
relatively standard, though there are some differences between one versions of
Basic to another.
LOADING QBASIC
Loading
QBASIC is very easy depending on your computer. It is commonly found in all
computers except vista. But one can purchase it from CD ROM and install it.
Method: 1
Loading
through Ms. DOS
·
Click ‘start’ button
·
Click ‘run’
·
Type ‘cmd’ and select Ok
·
Type ‘cd\’ and press enter
·
Type ‘qbasic’ and press enter key ones
Note:
If
Ms. DOS display ‘invalid command’, it means QBASIC is not installed on your
computer. But if QBASIC screen pop-up appear, two choices will be shown.
Alternatively:
Method: 2
Loading through “Windows
Desktop”
·
Double – click on “QBASIC” or
“SHORTCUTS TO QBASIC
·
Double click QBASIC and choose to run
if available
QBASIC SCREEN
IDENTITY:
If you press the <Enter> key, you will be taken to
the QBASIC HELP menu where you find information that concerns QBASIC
operations/statements. While pressing the “Esc” key, the pop-up box will
disappear and QBASIC opens up blue painted environment.
In addition, one will find the menus of QBASIC display
after the title bar.
QBASIC MENU
Diagram:
Below the menu is the blue painted area called the
process. QBASIC process is the desktop where the user operates the demand and
coded programmes but the user will not see the outcome / result displayed. It
is expected that the user sees the outcome of his coded programme on the “Black
& White” screen called the output.
Down below the process screen (blue painted) space is the
line of “Immediate”
IMMEDIATE LANE:
Diagram:
The immediate line cannot process any data, therefore it
is “no go area”. Nothing works there. It is useless going there to type or code
your program.
OUTPUT SCREEN
Diagram:
At the output screen the user is prompt to press any key
to return back to the process when the user have finished viewing the outcome
of his/her programme via the message “Press any key to continue”
TERMINOLOGIES
OPERATED ON QBASIC PROGRAMME
1. PROGRAMMING
A COMPUTER IN QBASIC
Programming a computer in QBASIC means that you tell the
computer what you want it to do. In other to tell the computer what to do in
QBASIC, you need those special words called commands. When you put all the
needed/demand special words in typing together in other to execute something,
then it can be regarded as coding. Therefore when the user writes or code a
programme on QBASIC is known as programming.
2. BASIC
RESERVED WORDS
These are commands or keywords that acts in the capacity
of commands and execution, when typed. QBASIC uses it to execute your written
programmes. For instance, some acts in the capacity to calculates, to show
final results, to correct errors, to allow writing of numbers or words to be a
heading, etc.
On the contrary, these reserved words only acts as
commands and execution but in terms of typing your programmes, do not use those
reserved words, because if typed it will cause your programmes written on the
process (blue painted space) to malfunction.
3. Key Basic
Statement
Key basic statement are commands or keywords required to
type as you code/write your programmes on the blue painted space which will
later turn into CAPITAL letters meant for execution of calculation, as well as
running the written/coded programmes.
In addition, each of the basic statement you type must be
on a line and each line must be assign, a number known as LINE number. These
line numbers gives an order of sequence how QBASIC should execute her
programme. Usually a positive multiples numbers from 0, - infinity are expected
to used. This can be optional to the user.
EXAMPLES OF KEY BASIC
STATEMENTS OPERATED IN QBASIC CODED PROGRAMMES
1. Rem Statement:
Rem is a special keyword known as remark. Sometimes when
a programmer write long programme, he or she will like to leave notes inside
their programme so that they can remember why they did certain things. So to
leave a note that explains certain things, use remark. QBASIC knows that when
it sees ‘Rem’ it does not have to do what you wrote after it on that line.
2. Cls Statement:
Cls means clear screen. If you run a programme from
QBASIC, everything that was on the screen before you started QBASIC will show
(i.e. written on the screen), that is why we need the keyword cls.
3. List Statement:
List statement is used to examine the programme and
correct errors before it is executed. This is because QBASIC displays and
execute programmes line-by-line according to the numbers used.
4. Print Statement:
Print is one of the most useful and powerful commands in
QBASIC that causes the information typed between the double quote (“ “) to be sent to the screen or visual display
unit end result where the users sees or runs the programme to get the final
results.
In addition, when Print is typed on the line number
without double quote (i.e. line number bearing print), QBASIC will provide one
empty space between such line and the next line at the output.
5. Input Statement:
This statement allows information to be typed using the
keyboard of the computer in generating the needed data before the final answer
will appear at the output. The computer executes this input statement by using
a representative question mark ( ? ) waiting for the data (i.e. figures) at the
output screen to be typed in.
6. Read Data Statement:
This statement allows information be followed by the word
“read” while the figures will be typed/supply at the same line typing of
“Data”, before the final answer be displayed on the V.D.U. Screen.
7. Let Statement:
This statement will cause the arithmetic expression on
the right hand side typed before an equal sign ( = ) be stored as the result
(answer that belongs to) the variable at the left of the equality sign.
8. End Statement:
This statement stops the execution of the programme. That
is to say, end statement tells the computer that the written/coded programme
done on QBASIC blue painted space (process) has come to an end.
9. Dim Statement:
Dim means dimension. It is the statement used to reserved
different locations of stored values when many locations are involved in a
programme by using just one variable.
10. Colour Statement:
Colour is a statement used to change the colour
appearance of the words on the screen when you run the coded programme. This
allows for fun, making sure you add colour when the need arises.
Nevertheless,
the colour list table is as follows:-
To paint
|
Number
to used
|
To
paint
|
Number
to used
|
Black
Blue
Green
Cyan
Red
Magenta
Brown
White
|
0
1
2
3
4
5
6
7
|
Grey
Light blue
Light green
Light cyan
Light red
Light magenta
Yellow
Bright white
|
8
9
10
11
12
13
14
15
|
11. Making Sound in QBASIC
Sound is an easy statement used to produce sound when you
run your programme. To use the keyword sound, you must type (enter) two (2)
numbers. The first number is the frequency of the sound in high or low capacity
(kw) which ranges from very low frequent (37kw) to (32767kw) very high
frequency. The second number is how long the sound should last.
12. Drawing Pictures/Objects or Shapes with
QBASIC with:
i) The Screen Statement:
Before you even start drawing object, shapes or pictures,
you have to tell QBASIC that you want to draw pictures. To do this, you need to
used screen statement by assigning number to it. Number 12 (mode 12) is one of
the screen numbers that is used to draw pictures.
ii) To Draw Line(s):-
Line Statement is used to draw lines but you must specify
by typing where the line will starts and ends.
iii) Drawing a rectangular box in conjunction
with line statement:
The line statement can also be used to draw other shapes
for instance it can be used to draw boxes filled with colours or empty.
iv) Drawing a Circle:
Another very important shape is circle. The keyword
Circle can be used to draw a circular shape but you also need to specify where
the centre of the circle should be and how far from the centre it will be drawn
and to the edges it will covers.
Watch all these statements when you start the practical.
ELEMENTS OF QBASIC
Every programming language consists of some important
elements which are required to make a programme be executed. Therefore elements
are those typing that are required to make a coded programme function properly,
before running it to see your answers. These elements can involves character
sets, keywords, constants, variables, operators and expressions.
CHARACTER SETS:
These are typing required when coding a programme which
can be type in alphabets both in smaller and capital letters, numbers that
ranges 0 to 9, and infinity. Special symbols and signs are also part of
character set in which each performs meanings and various functions and makes
your program of standard.
Examples of special symbols and signs used as a character
set in QBASIC is shown below:-
Signs
|
Signs
|
Signs
|
Signs
|
+
-
*
/
\
^
|
“ “
‘
,
;
:
=
|
>
<
(
)
.
?
|
s
%
$
!
#
Blank space
|
CONSTANTS
They are the data or the values stored inside variable
coded on qbasic program that cannot changed during the program execution except
for numbers. The data may be a letter, words, numbers, special symbols but they
are stored inside a variable name which QBASIC will not changed the way you
type it, except it is numbers. Why? Because numbers when stored as constants,
QBASIC can perform mathematical calculation on it.
In QBASIC coding of programme, constants are grouped into
two broad headings:
a) String Constant
b) Numeric Constant
String Constants:
It is a letter, words, numbers, combination of letters
with numbers or special symbols enclosed in double quotes (“ “) stored inside a
variable name in which mathematical operations cannot be done on it. It can
also be used to express statement on a program typed to QBASIC. For instance,
“B”, “Apple”, “Symbol No. 10208”, “Welcome to QBasic World!!!”, etc.
Numeric Constants:
Numeric constant refers to a number with or without
decimal point, stored inside a variable name in which mathematical operations
can be done on it. Numbers written in thousands to QBASIC will not consists of
separator (,) which also can not be enclose in double quotes. Generally,
mathematical and logical operations can be done on them e.g. 101, 105.50, 720,
45603, etc.
VARIABLE:
In QBASIC, a variable is a place on the computer memory
which has a name and stores data temporarily. On the otherhand, a variable is
an entity that stores data needed to be used in a program. Each program defines
different number of variables. The value of a variable can be change during the
execution of the program. On declaring (i.e. typing) a variable, one will need
to determine the type of data which QBASIC will stored inside the variable.
There are mainly
two types of variables declaration. They are:-
a) String variable
b) Numeric variable
String Variable:
String variable stores string data (i.e. quotation
statements) with a string variable name declare on that same line whereby
mathematical and logical operations cannot be done on it as directed by the
declaration symbols of dollar sign ($).
Numeric Variable:
A numeric variable stores number data inside it variable
name that mathematical and logical operations can easily be done on it as
directed by its declaration symbols typed.
Example of numeric variable declaration:
It can be expressed under four headings, namely, Integer,
long integer, single precision and double precision variable.
For integers, it stores short whole number with the sign
of percentage (%) typed to the variable name. Long integer stores large range
of whole numbers with ampersand (&) declaration, for single precision, it
stores single decimal numbers with or without declaration of exclamation mark
(!) and for double precision, it stores large decimal numbers declares with the
use of hash sign (#)
RULES FOR TYPING A
VARIABLE TO QBASIC
1. Variable names typing can have maximum
of 40 characters in length typed together without space.
2. Variable names can have alphabets,
numbers and alphanumeric on line numbers.
3. Variable name must begin with a letter
4. Variable names cannot begin with fn or
Fn alphabets for example, fnames$, fnum, etc. It is because fn causes the to
calculate frequency number of statistic data. Don’t use it as a variable name.
5. Variable names cannot be served words.
6. Variable names may be ended with type
declaration characters like $, %, &, !, and #
OPERATORS
Operators are symbols that indicate the type of
mathematical operations QBASIC has to perform on the data or values of the
variable. In QBASIC there are four types/branches of operators used on coded
program namely: Arithmetic, Relational, Logical and String operators.
Arithmetic operators:
These are all the general mathematical signs used to
perform simple and practical mathematical calculations like addition,
subtraction, division, multiplication, exponents, etc
Example 1:
Operation
|
Operators
|
Example
|
Results
|
Addition
Subtraction
Multiplication
Division
Integer division
Exponential
Modular division
|
+
-
*
/
\
^
Mod
|
5 + 8
8 – 6
5 * 4
8/2
9\2
4^3
7 Mod 3
|
13
2
20
4
4
64
1
|
Relational Operators:
They are operators used to perform comparison on two
values of the same typed statements. E.g.
Operators
|
Relation
|
Examples
|
=
>
<
>=
<=
<>
|
Equal to
Greater than
Less than
Greater than or equal to
Less than or equal to
Not equal to
|
A = B, A$ = B$
A > B, “CAT” > “RAT”
A < B, “CAT < “CAT”
A >= B, X$ >= Y$
A <= B, X$ <= Y$
A$ <> B$, X <> Y
|
Logical Operators:
It combine two or more relational expression to evaluate
a singe value as true (non zero answer) or false (zero answer). For example:
And operation:
When all the results (statements) returned are true.
Statement 1 (for P)
|
Statement 2 (for Q)
|
Results (P & Q)
|
F
T
F
T
|
T
F
F
T
|
F
F
F
T
|
Or
operation:
It return true if any one of
the relational statements expressions return true. E.g.
Statement 1 (for A)
|
Statement 2 (for B)
|
Results (A & B)
|
F
T
T
F
|
T
F
T
F
|
T
T
T
F
|
Not
Operation:
It operates on one statement
operand and returns true if the logical statement returns false. E.g.
Statement 1 (for A)
|
Result (for Not A)
|
F
T
|
T
F
|
STRING
OPERATOR
String joins two or more
than two string data (sentences) together to form a simple or compound
sentences.
E.g.
String data (for
A$)
|
String data (for B$)
|
Results (A$ + B$)
|
“Wel”
“50”
|
“come”
“45”
|
Welcome
5045
|
The plus sign (+) is used as
the string operator as the act of combining two strings together which can also
be known as as concatenation.
EXPRESSION
It is the combination of
operators, constants and variables that is evaluated to get a result. The
result is string data, numeric data or logical value and can be stored in a
variable. E.g.
(A + B) > C
A >= B + C
u = t + ½ etc
Note, expression can come from any side of
the mathematics, some from linear simultaneous equation, algebra, statistics,
trigonometry, etc.
Example of algebra
expression:
It must be converted into
QBASIC expression and interpretation before the result is shown at the output.
A = L X B A = L * B
P = 2(L+B) P = 2 * (L + B)
I = (P X T X R)
100 I
= (P * T * R)/100
V = 4 pi r3
3 V
= 4/3 * (PI * R ^ 3)
V = pr2h V = PI X r ^ 2 * h
ORDER
OF OPERATION
The order of operations is
the order (due process) in which all algebraic expression should be simplified.
For instance on QBASIC:
Parenthesis
Exponents (and Roots)
Multiplication and Division
Addition and Subtraction
[PEMDAS]
Alternatively:
Brackets
Exponentiation
Division
and Multiplication
Addition
and Subtraction
[BEDMAS]
Note:
Be
aware that multiplication does not always come before division, and addition
does not always come before subtraction. For example.
3 X 3 – 5 + 2
Your
working will always starts from left to right.
You
work like this. First notice that there is no parenthesis or exponents, so we
move to multiplication and division. There’s only one multiplication, so we do
that first and end up with 9 – 5 + 2
Now
we move to addition and subtraction, so finally we do the subtraction to get 4
+ 2 and finally the addition to give 6. Thus
the order becomes: [PEDMSA]
Example 2: 75/5^(3-1) Using
the PEDMAS
Solve
for bracket (parenthesis) (3 – 1) = 2
= 75/5^2
Solve
for exponentiation (5^2)
= 25
=75/25
Finally,
solve for division =75/25
= 3
Example 3:
In
this example chose the student that solves his/her expression correct.
Student
1 student 2
3 +
4 x 2 3 + 4 x 2
= 7
x 2 = 3 + 8
= 14 = 11
Student
1 is wrong and student 2 is right in the order of operation.
c) f =
a
LET’S
PRACTICE
COMMON PRACTICAL QUESTIONS AND ANSWERS
ON HOW TO CODE QBASIC PROGRAM
ALWAYS RUN YOUR PROGRAM (BY PRESSING F5
KEY) AT THE END OF YOUR CODED PROGRAM TO QBASIC BLUE PAINTED SPACE.
1. Write or
code QBASIC program that demonstrates the steps or procedures involve to
prepare an office tea breakfast with the results shown at the output screen.
Solution:
5 REM – Example 1
10 REM – Coding a program that illustrate
15 REM – how to prepare office tea breakfast
18 CLS
20 LET A$ = “Wash the kettle”
25 LET B$ = “Add clean water into the kettle”
30 LET C$ = “Heat the kettle for 15 minutes”
35 LET D$ = “Check if the water is boiling”
40 LET E$ = “If the water boils after 15
mnutes”
45 LET F$ = “Add coffee, sugar and milk”
47 PRINT
49 PRINT “Directives to prepare office tea”
50 PRINT “………………………………………..”
53 PRINT
55 PRINT “The first procedure is to “; A$
60 PRINT “The next procedure is to “; B$
63 PRINT “The next procedure is to “; C$
65 PRINT “The next process is to “; D$
68 PRINT “The next process is to “; E$
70 PRINT “The last procedure is to “; F$
75 END
2. Write a simple program that illustrate
the below comment:
If
Month = 11, 12, 1 or 2 (November, December, January, or February), then the
word, “Cool Season” appear on the screen. If month = 3, 4, or 5 (March, April,
or May), then the words “Hot Season” appear on the screen. If month is any
number from 6 to 10 (June to October ), then the words “Wet Season” appear on
the screen.
Solution:
DIM
Month AS INTEGER
CLS
REM
– Program to predict season
PRINT
INPUT
“Enter the number for season as: “, Month
SELECT
CASE MONTH
CASE
11, 12, 1, 2
PRINT
“Cool Season”
CASE
3 TO 5
PRINT
“Hot Season”
CASE
6 TO 10
PRINT
“Wet Season”
CASE
ELSE
PRINT
“What kind of season is demanded”
END
SELECT
Alternatively:
3. Write a program that asks for a score then prints the grade
assigned to the score.
Solution:
DIM Score AS INTEGER
CLS
PRINT
INPUT "Enter The Test Score: ", Score
PRINT
SELECT CASE Score
CASE IS >= 97
PRINT "Grade A+"
CASE 93 TO 96
PRINT "Grade A"
CASE 90 TO 92
PRINT "Grade A-"
CASE 87 TO 89
PRINT "Grade B+"
CASE 83 TO 86
PRINT "Grade B"
CASE 80 TO 82
PRINT "Grade B-"
CASE 77 TO 79
PRINT "Grade C+"
CASE 73 TO 76
PRINT "Grade C"
CASE 70 TO 72
PRINT "Grade C-"
CASE 67 TO 69
PRINT "Grade D+"
CASE 63 TO 66
PRINT "Grade D"
CASE 60 TO 62
PRINT "Grade D-"
CASE ELSE
PRINT "Fail"
END SELECT
4. Write a program with the comment: I am 11 years old, soon I
will grow up and be 25.
Solution:
CLS
AGE
= 11
PRINT
“I am”; AGE; “years Old.”
AGE
= 25
PRINT
“But I will grow and soon will be “; AGE
END
5. Write a program that display today’s date in the date format
of dddd/mmm/yyyy
Solution:
CLS
INPUT
“Enter a year”; year
INPUT
“Enter a day”; day
INPUT
“Enter a month”; month
PRINT
“The year is “; year
PRINT
“The month is “; month
PRINT
“The day is “; day
PRINT
“-or-“
PRINT
“The day is”; day; “/”; month; “/”; year
END
ALGEBRAIC
EXPRESSION
6. How can the following formulas be converted to its QBASIC
equivalent?
a) a = 3
+ 3
4 + 4
E.g.
LET a = (3+3)/(4+4)
b) x = (a –
b)(a – c)2
E.g.
LET x = (a – b) * (a – c) ^ 2
|
|
b
E.g.
Let f = a ^ ( 1 / 2 ) / b ^ ( 1 / 3 )
d) d = (8 –
x2) - (4 x 2 – 1)
(x – 9) x3
E.g.
Let d = ((8 – x ^ 2) / ( x – 9) – (( 4 * 2 –
1) / (x ^ 3)
TRIGONOMETRY / CONSTRUCTION / TRIANGLE
QUESTIONS.
7. Write a program that calculate the area
and circumference of a circle on the following values for radius (12, 14, 15,
22 and 45 respectively).
Solution:
REM
‘
Author: Sir Isaac
‘
Class: Computer science 404
‘
Date: 07/09/2011
‘
‘
Purpose:
‘ This program will calculate the Area and
circumference of a circle
‘ after receiving the value for the radius
from the user.
‘
‘
(a) Start
CLS
‘
(b) Declare variable, define
known values
DIM radius AS DOUBLE ‘ Radius
DIM PI AS DOUBLE ‘ Pi
DIM Circumf
AS DOUBLE ‘ Circumference
DIM Area as DOUBLE’ Area
PI = 3.141593
COLOR 9
‘ (c)
Get Required data from user
PRINT “This program calculates the Area and
Circumference of a circle”
INPUT “What is the radius of the circle”;
radius
‘
(d) Make the calculation
Circumf = 2 * PI * radius
Area = PI * radius ^ 2
‘
(e) Send the results to the
screen for the user
COLOR 2
PRINT “For a circle of radius”; radius;
PRINT “and using a value for PI as”; PI
PRINT “The calculated circumference is “;
Circumf
PRINT “The calculated Area is “; Area
‘
‘
(f) Stop
8. Write a program to express these
equation
y = a(b – c)2
d + 2
where
a = 2, b = 8, c = 4 and d = 2
Solution:
5
REM
10
CLS
15 a
= 2
20 b
= 8
25 c
= 4
30 d
= 2
35 e
= a * (b – c) ^ 2
40 f
= d + 2
45 y
= e / f
50
PRINT “Your answer is “; y
55
END
ADVANCED FEATURES TO CODE INTERVIEW
PROGRAM
9. Write a
program to illustrate an interview with the user to make the right choice based
on your instruction.
Solution:
10
REM – Interview program
20
CLS
30
INPUT “Please enter your first name”; N$
40
INPUT “Are you born again? Answer Y or N”; A$
50
IF A$ = “Y” THEN 70
60
IF A$ = “N” THEN 90
70
PRINT N$, “You’ve made the right choice!”
80 GOTO
100
90
PRINT N$, “You need to get born Again”
100
END
CODING LINEAR SIMULTANEOUS EQUATION (
THE ALMIGHTY FORMULA)
10. Write a program to solve the below
quadratic linear equation
x = -b ± Ö b2
– 4ac
2a
Where 3x2 + 8x – 16 = 0 find x.
Solution:
5 REM – Program to evaluate quadratic formula
10 CLS
20 INPUT “Enter the number of 1st
coefficient of x”; a
30 INPUT “Enter number of second coefficient
of x”; b
40 INPUT “Enter number of third coefficient
of x”; c
50 d = - b + SQR (b ^ 2 – ( 4 * a * c))
60 e = 2 * a
70 f = - b – SQR (b ^ 2 – (4 * a * c))
80 g = d / e
90 h = f / e
100 PRINT
105 PRINT “Result of positive x is “; g
110 PRINT “Result for negative x is “; h
115 END
CODING A GAME GUESS PROGRAM ON QBASIC
11. Write a program to play a game of
knowing how many days are there in a week?
Solution:
CLS
Score = 0
PRINT “How many days are there in a week?”
INPUT a
IF a = 7 THEN GOTO 2
PRINT “Wrong answer!”
PRINT “To try again – press ‘y’”
INPUT a$
IF a$ = “y” THEN GOTO 1 ELSE END
Score 10
PRINT “It’s the right answer!”
PRINT “Your Score is now”; Score; “!”
PRINT “Thanks for playing”
END
CODING A RECTANGLE MEASUREMENT ON QBASIC
12. Given that:
5cm
6cm
Write a program to calculate the
area of the rectangle drawn above
Solution:
REM – Example 5.6
‘
-------------------------------------------------------------
‘ Compute the area of a rectangle
‘
Length L and width W
‘
--------------------------------------------------------------
DIM L AS SINGLE
DIM W AS SINGLE
DIM Area
AS SINGLE
CLS
INPUT “Length of the rectangle is “; L
INPUT “Width of the rectangle is “; W
Area = L * W
PRINT “The area of “; L; “by”; W; “rectangle
will be “; Area
END
USING BUSINESS / SMALL SCALE FIRM
INCENTIVE TO CODE QBASIC
13. You have
a Canteen Business and you want to create an incentive bonus for your staff
that meet the stipulated target. Write a program that illustrate these
statement.
Solution:
REM – Program to set Canteen target
CLS
INPUT “Target “; Target
INPUT “Canteen Sales”; Canteensales
IF Canteensales >= target THEN
Performance$ = “We are Happy”
Bonus = 100 + 0.01 * (canteensales – target)
ELSE
Performance$ = “We are NOT Happy”
Bonus = 0
ENDIF
PRINT “*=============================*”
PRINT “Target”; Target
PRINT “Sales”; Canteensales
PRINT “Performance”; Performance$
PRINT “Bonus”; Bonus
14. Alternatively,
the Canteen management held meeting and agreed that where the incentive to the
staff does not meet the target, there will be redundancy. Re-write the program
to show the decision of the management.
Solution:
REM – Canteen Management Incentive
CLS
INPUT “Target “; Target
INPUT “Sales”; Canteensales
IF Canteensales >= 2 * target THEN
Performance = “Excellent”
Bonus = 1000
ELSEIF Canteensales >= 1.5 * target THEN
Performance$ = “Fine”
Bonus = 500
ELSEIF Canteensales >= target THEN
Performance$ = “Satisfactory”
Bonus = 100
ELSE
Performance$ = “Unsatisfactory”
PRINT “You’re FIRED!!!”
ENDIF
PRINT “Target”; Target
PRINT “Sales”; Canteensales
PRINT “Performance”; Performance$
PRINT
“Bonus”; Bonus
15. Write a program to select days of the
week within seven days.
Solution:
CLS
INPUT “Date Number is “; dayNumber
SELECT CASE dayNumber
CASE 1
PRINT “Monday”
CASE 2
PRINT “Tuesday”
CASE 3
PRINT “Wednesday”
CASE 4
PRINT “Thursday”
CASE 5
PRINT “Friday”
CASE 6
PRINT “Saturday”
CASE 7
PRINT “Sunday”
CASE ELSE
PRINT “How many days in your week?”
END SELECT
MATHEMATICAL EXPRESSION
16. Write a programme that will show a list
of times table number of 3’s e.g. 3 x 1 = 3, 3 x 2 = 6, etc.
Solution:
5 CLS
10 REM – Compute times table numbers of 3
15 INPUT “Enter any number for “; n
20 FOR I = 1 to 12
25 PRINT n; “x”; I “=” n * I
30 NEXT I
35 END
17. Write a
program that generates multiples of 5 between 5 and 500, displaying their sum
and products at the output screen.
Solution:
5 CLS
10 REM – Program to generate multiples of 5
15 ‘ between 5 and 500
20 ‘ Calculates their sum and products
30 ‘ displayed their results
40 SUM = 0
50 PRDT = 1
60 FOR L = 5 TO 500 STEP 5
70 SUM = SUM + L
80 PRDT = PRDT * L
85 ‘ Display the current values of L, SUM AND
PRDT
90 PRINT L, SUM, PRDT
100 NEXT L
110 ‘ display the final values of L, SUM and
PRDT
120 PRINT L, SUM, PRDT
130 END
18. Given the below arithmetic sequences:
Find the sum of 12 terms
of the series:
7 + 11 + 15
+ …
Hence, the first term a = 7, common
difference d = 4 and number of
terms n = 12.
Requirement:
Use
QBASIC to write the above sequences and display the sum of the series at the
output.
Solution:
REM Arithmetic sequences
CLS
PRINT “Enter first term”
INPUT A
PRINT “Enter common difference”
INPUT D
PRINT “Enter number of terms”
INPUT N
PRINT “The sequence is “
T = A
S = O
FOR I% = 1 TO N
S = S + T
T = T + D
NEXT I%
PRINT T
PRINT “The sum is “; S
END
CODING BANKING INTEREST/PENSION ADVANCE
PAYMENT ON QBASIC
19. Given
that the goal you set for your retirement in your current employer is N850,000.
You decide to make deposit of your pension payment in advance to ECO BANK PLC
with payment of N1,000 each month with the interest of 5.0% interest charge.
Write a program to illustrate the above comments, showing the number of years
it will take to complete the retirement task.
Solution:
CLS
REM – Program to meet pension retirement goal
DIM Goal
AS DOUBLE
DIM Interest
AS DOUBLE
DIM Payment AS DOUBLE
DIM Years AS INTEGER
Dim Balance
AS DOUBLE
Balance = 0
PRINT “So you want to put some money aside
for retirement, hmmmm.”
INPUT “How much money do you need to retire”;
Goal
INPUT “How much money will you contribute
every year”; Payment
INPUT “Interest rate in % (e.g. use 5.0 for
5.0%) will be”; Interest
Interest = Interest / 100
Balance = Payment
DO WHILE Balance < Goal
Balance = Balance * ( 1 + Interest)
Years = Years + 1
LOOP
PRINT
PRINT “You can retire in “; Years; “years”
PRINT “with”; Balance; “in the ECO BANK”
END
CODING A MENU LIST FOR CHOICE IN QBASIC
20. Write a
menu list program that allow the user to make a wish/choice in a statistical
analysis coding system.
Solution:
CLS
PRINT “Which Statistical Analysis do you
wish”
PRINT
PRINT “1.
Compute an Average”
PRINT “2.
Compute a Standard Deviation”
PRINT “3.
Find the Median”
PRINT “4.
Find the Smallest and Largest data value”
PRINT “5.
Plot the Data”
PRINT
PRINT “0.
Quit/Return to preview menu”
DO
INPUT “Enter your choice ( 0 through 5)”; a%
‘ Use SELECT CASE or IF to check the user
‘ Input, and do the work that has been requested.
LOOP UNTIL a% = 0
MATHEMATICAL CONTINUES
21. Suppose
we are given ten rectangles and required to find the area of each rectangle.
Given below is a QBASIC program that reads the length and breath of each
rectangle, finds its area and prints the areas.
Solution:
5 CLS
10 REM – Program that reads the length “L”
and
15 ‘ breath “B” of ten rectangles
20 ‘ calculates the area of each rectangle
30 ‘ and prints the result
40 KANTER = 0
50 PRINT “Please enter the values of L and B”
60 INPUT L
65 INPUT B
70 Area = L * B
80 PRINT L, B, Area
90 KANTER = KANTER + 1
100 IF KANTER > 10 THEN 120
110 GOTO 50
120 END
DATA:
6 X
5cm, 3 X 4cm, 16 X 6cm,
4 X 5cm, 6 X 8cm, 7 X 3cm,
10 X 4cm, 2 X 2cm, 9 X 2cm, 12 X 6cm.
22. Given
that you are required to calculate the powers of numbers. Write a programme
that list all the resulting powers of an integer that is less than 1000.
For example:
20 = 2, 21 =
2, 22 = 4, 23 = 8, 24 = 16, 25 =
32, 26 = 64, 27 = 128, 28 = 256, 29
= 512, etc.
Solution:
CLS
REM Compute List of powers
Const Maxpower = 1000
DIM Power
AS INTEGER
DIM n AS INTEGER
Power = 1 ‘ The first power n ^ 0 is always 1
COLOR 10, 1
PRINT “This program prints all powers <”;
Maxpower; “of an integer”
INPUT “Enter an integer for”; n
DO WHILE Power < Maxpower
PRINT Power;
Power = Power * n
LOOP
CODING ACCOUNTING RECORDS ON QBASIC
23. Below is
the record of Sales on tickets from each gate of a sports stadium represented
as a set of data in the following form:
i) Identity of the gate as G01
ii) Series of positive integers
representing the income from the sales of ticket at the gate
Sample Records
G01 27.00 254.00 15.00 21.00
G02 2.00 196.00 240.00
G03 25.00 300.00 10.00
G04 36.00 298.00 150.00 etc
Write
a QBASIC programme to read the sample data above, calculate the total income
for each gate separately and calculate the total income for the stadium and the
gate which records the largest income.
Solution:
5 REM Clear the screen
10 CLS
15 REM Enter the number of the gates that are
available
30 INPUT “Enter the number of the gates”; P
35 DIM A$(P), S(P)
40 CLS’
50 FOR K = 1 TO P
60 INPUT “Please enter gate identity”; A$(I)
65 REM
Initialize S(I) which represents total income
70 S(I) = 0
80 INPUT “Income from the gate : = “; C
85 REM Test for the negative income which is
the terminator
90 IF C > 0 THEN
95 REM Add up the income for each gate
100 S(I) = S(I) + C
105 REM Pass the control to line 80
110 GOTO 80
115 REM Close the if statement
117 ELSE
119 GOTO 130
120 ENDIF
130 CLS
140 NEXT K
150 CLS
155 REM Create a for, next loop to print out
the report
160 PRINT “Gate ID. Total Income”
180 FOR I = 1 TO P
190 PRINT A$(I),,, S(I)
210 NEXT I
215 REM Test for the gate with the largest
income
217 LG = 0
220 FOR N = 1 TO P
230 IF S(I) > LG THEN
240 LG = S(I)
250 T$ = A$(I)
260 ENDIF
270 NEXT N
275 REM Print the gate with the largest
income and the amount
290 PRINT “The gate with the largest income”;
LG, “is”; T$
320 END
24. Write
QBASIC Program to read the records/accounts of 1000 employees of STARBLISS
(NIG.) LTD. Each record has the following layout:
Columns
1 – 20 contains employee surname,
Columns
21 – 35 contains employee other names,
Columns
36 – 45 contains employee department,
Columns
46 – 52 contains employee gross monthly income
Columns
53 – 54 contains spaces,
Columns
54 – 61 contains employee gross monthly deduction.
Requirement:
Calculate
the total monthly net pay of each employee and generate the output reports and
accounts.
Solution:
10 REM This programme reads the records of
1000 employee of
20 ‘ STARBLISS (NIG.) LTD. Each employee is
described by
30 ‘ surname, other names, departments,
40 ‘ gross monthly income and gross monthly
deduction.
50 ‘ Calculate for each employee
60 ‘ the total gross income
65 ‘ the total gross deduction and
70 ‘ monthly net pay and print the output
report
90 CLS
100 REM Set all previous key setting off
110 KEY OFF
120 DIM S$(1000), O$(1000), T$(1000),
M(1000), U(1000), N(1000)
130 TINC = 0
140 TDED = 0
150 TNCTPAY = 0
160 REM Set a for and next loop to accept
1000 records
170 FOR I = 1 TO 1000
180 REM accept all the necessary data
190 INPUT “Surname: “; S$(I)
200 INPUT “Other names: “; O$(I)
210 INPUT “Department: “; T$(I)
220 INPUT “Gross monthly income: “; M(I)
230 INPUT “Gross monthly deduction: “; U(I)
240 REM Calculate monthly net pay for each
employee
250 N(I) = M(I) – U(I)
260 TINC = TINC + M(I)
270 TDED = TDED + U(I)
280 TNETPAY = TNETPAY + N(I)
290 CLS
300 NEXT I
310 CLS
320 REM Print the required report
330 FOR I = 1 TO 1000
340 PRINT S$(I), O$(I), T$(I), M(I), U(I),
N(I)
350 NEXT I
360 REM Print total gross income, total gross
deduction and total net pay
370 PRINT “Total gross income: “; TINC
380 PRINT “Total gross deduction: “; TDED
390 PRINT “Total net pay: “; TNETPAY
400 END
25. Write
QBASIC Programme to input the records of a file where each record contains the
identity of a person, the amount of loan in Naira he obtains (the principal),
the interest rate of the loan and the number of payment period per year. The
programme should compute the loan payment required for each pay period to repay
the outstanding principal in one year at the indicated rate in Naira equal
payments. The formula for calculating payments is:
Payment = P(1 + K)
+ 12
n
Where
P = Principal
loan
K = Interest
rate
n = Number of
periods per year
Solution:
10 CLS
20 REM Set previous key setting off
30 KEY OFF
40 REM Enter the number of the debtors
50 INPUT “Please enter the number of debtors
to consider”; d
60 CLS
70 DIM A$(d), B$(d), C(d), D(d), E(d), F(d)
80 REM Create a for and next loop to accept
the records of debtors
90 FOR I = 1 TO d
100 INPUT “Surname: “; A$(I)
110 INPUT “Other names: “; B$(I)
120 INPUT “Principal Loan: “; C(I)
130 INPUT “Interest rate: “; K(I)
140 INPUT “Payment period per year = “; E(I)
150 REM Compute the payment per period for
each debtor
160 F(I) = (C(I) * (1 + K(I)) + 12) / E(I)
170 CLS
180 NEXT I
190 CLS
200 REM Print the report
210 FOR I = 1 TO d
220 PRINT A$(I), B$(I), C(I), K(I), E(I),
F(I)
225 NEXT I
230 END
INTRODUCING BASIC GRAPHICS ON QBASIC
26. Write QBASIC programme that causes a red
“lighting bolt” to be displayed on the screen
Solution:
10 REM Case study of lighting bolt
20 REM
30 KEY OFF
35 CLS
40 SCREEN 1
45 COLOR 0,0
50 LINE (20, 20) – (120, 80), 2
55 LINE – (80, 80), 2
60 LINE – (220, 180), 2
65 LINE – (140, 100), 2
70 LINE – (190, 100), 2
80 LINE – (110, 20), 2
90 LINE – (20, 20), 2
100 END
27. Given
below is a QBASIC Programme which causes a sequence of rectangles to move from
the centre of the screen to the outer edges. This programme also utilizes sound
to enhance the graphics effects.
Solution:
10 REM Case study of concentric rectangles
20 REM
30 KEY OFF
35 CLS
40 SCREEN 1
45 COLOR 0, 1
47 C = 3
50 P = 0
55 Q = -45
70 REM Begin main loop
80 FOR I = 1 TO 10
90 LINE (160 – 1.6 * P, 100 – P) – (160 + 1.6
* P, 100 + P), C, B
100 IF Q > 0 THEN
105 LINE (160 – 1.6 * Q, 100 – Q) – (160 +
1.6 * Q, 100 + Q), O, B
107 ENDIF
110 P = P + S
115 IF P > 95 THEN
117 P = 0
119 C = 1 + INT(3 * RND)
120 Q = Q + 5
123 ENDIF
125 IF Q > 95 THEN
127 Q = 0
129 ENDIF
130 IF P = 0 OR Q = 0 THEN
133 SOUND 20 + INT(100 * RND), 10
135 ENDIF
140 NEXT I
150 END
28. Given
below is a QBASIC programme which causes repeated groups of concentric circles
to be generated, with each group starting at the centre of the screen and
moving outward to the edge.
Solution:
10 REM Case study of concentric circles
20 REM
30 KEY OFF
35 CLS
40 SCREEN 1
45 COLOR 0, 1
50 LINE (39, 0) – (279, 199), 3B
60 CLR = 1
70 REM
80 REM begin loop
90 REM
100 FOR 1 = 5 TO 120 STEP 5
110 CIRCLE (160, 100), R, CLR
120 NEXT I
130 CLR = CLR + I
133 IF CLR > 3 THEN
135 CLR = 1
137 ENDIF
140 GOTO 100
150 END
29. Write an
alternative QBASIC programme that allow any shape objects, circles, ellipses
and rectangles to be filled with other paint colours, with the general form of using
paint statement.
Solution:
5 CLS
10 REM This programme generates concentric
painted circles
20 SCREEN 9
30 FOR K = 1 TO 100
40 CIRCLE(300, 165), K
50 CIRCLE(320, 165), K
60 CIRCLE(100, 183), K
70 CIRCLE(200, 265), K
80 CIRCLE(220, 34), K
90 CIRCLE(240, 55), K
100 CIRCLE(120, 134), K
110 CIRCLE(160, 123), K
120 CIRCLE(80, 265), K
130 CIRCLE(40, 80), K
140 CIRCLE(14, 105), K
150 CIRCLE(140, 25), K
160 CIRCLE(70, 65), K
170 CIRCLE(300, 16), K
180 CIRCLE(400, 84), K
190 CIRCLE(340, 99), K
200 LINE (500, 200 – K + 8) – (600, 230 – K +
8), 2, BF
210 LINE (350, 225 – K + 8) – (500, 215 – K +
8), 7, BF
220 SLEEP 1
230 LINE (500, 200 – K + 8) – (600, 230 – K +
8), 0, BF
240 LINE (350, 225 – K + 8) – (500, 215 – K +
8), 0, BF
250 NEXT K
260 END
ADVANCE GRAPHS OF BAR CHART ON QBASIC
30. Given
below is a QBASIC programme that causes a bar chart to be generated. Each value
is assumed to be non-negative, therefore the values will be displayed above
each of their respective rectangles, that is, above each “bar”
To
run the programme, the input data set are:-
Annual Sales Increases
Increase (%) Year
7.8 1991
8.2 1992
6.7 1993
10.6 1994
12.3 1995
Solution:
10 REM Bar chart generation
20 REM
30 KEY OFF
35 CLS
40 WIDTH 80
42 SCREEN 0
44 COLOR 7, 0
50 DIM Label$(12), Y(12)
60 LOCATE 1, 20: PRINT String$(40, “*”)
70 LOCATE 2, 20: PRINT “*”; SPC(38); “*”
80 LOCATE 3, 20: PRINT “*”; SPC(9); “Bar
Chart Generation”; SPC(10); “*”
90 LOCATE 4, 20: PRINT “*”; SPC(38); “*”)
100 LOCATE 5, 20: PRINT String$(40, “*”)
110 YMAX = 0
120 REM
130 REM Enter data and find largest y
140 REM
150 LOCATE 7, 1: INPUT “Title: “; Title$
160 LOCATE 9, 1: INPUT “How many data items?
(1 – 12)”; ANS$
165 N = VAL(ANS$)
170 IF N < 1 OR N > 12 THEN
173 BEEP
175 LOCATE 9, 29: PRINT Space$(6)
177 ENDIF
179 GOTO 160
180 FOR I = 1 TO N
190 LOCATE 1 + 10: PRINT “I = “; I
200 LOCATE 1 + 10, 15: INPUT “Value: “; ANS$
205 Y(I) = VAL(ANS$)
210 IF LEFT$(ANS$, 1) = “0” THEN 230
220 IF Y(I) < = 0 THEN
222 BEEP
224 LOCATE 1 + 10, 21: PRINT Space$(6)
226 ENDIF
228 GOTO 200
230 LOCATE 1 + 10, 35: INPUT “Label: “;
Label$(I)
240 IF Y(I) > YMAX THEN YMAX = Y(I)
250 NEXT I
260 REM
270 REM Generate and display the bar chart
280 REM
290 CLS
292 W = 60 \ N
300 FOR I = 1 TO N
310 R = 20 – 18 * Y(I) \ YMAX
313 IF R = 20 THAN 370
320 FOR ROW = R TO 20
330 FOR COL = ( I – 1) * W + 11 TO I * W + 8
340 LOCATE ROW, COL: PRINT “*”
350 NEXT COL
360 NEXT ROW
370 NEXT I
380 REM
390 REM Label the bar chart
400 REM
410 FOR I = 1 TO N
420 R = 19 – 19 * Y(I) \ YMAX
430 C = 9 + (I – 1) * W + (W – LEN(Str$(Y(I)
) ) ) / 2
440 LOCATE R, C: PRINT Y(I)
450 C = 10 + (I – 1) * W + (W – LEN
(Label$(I) ) ) / 2
460 LOCATE 21, C: PRINT Label$ (I)
470 NEXT I
480 C = 10 + (N * W – LEN(Title$) ) / 2
490 LOCATE 23, C: PRINT Title$; :LOCATE 23, 1
500 END
ANIMATIONS OF GRAPHICS ON QBASIC
31. Given
below is a basic programme which causes a ball to move freely within a confined
enclosure. Write to generate the animation by means of the GET and PUT
statement.
Solution:
10 REM Bouncing Ball
20 REM
30 KEY OFF
35 CLS
40 DEFINT A – Z
50 RANDOMIZE
55 CLS
60 SCREEN 2
70 DIM BALL (34)
80 LINE (0, 0) – (639, 199), , B
90 LINE (10, 5) – (629, 194), , B
100 PAINT (5, 2)
110 X = 20 + INT(600 * RND)
115 Y = 20 + INT(190 * RND)
120 DX = 1 + INT(10 * RND)
125 DY = 1 + INT(10 * RND)
130 CIRCLE (X, Y), 10
140 PAINT (X, Y)
150 GET(X – 10, Y – 10) – (X + 10, Y + 10),
BALL
190 PUT(X – 1 – , Y – 10), BALL
200 X1 = X + DX
205 Y1 = Y + DY
210 IF X1 < 21 THEN
213 X1 = 21
215 DX = -DX
217 ENDIF
219 GOTO 230
223 X1 = 618
225 DX = -DX
227 ENDIF
230 IF Y1 < 10 THEN
233 Y1 = 10
235 DY = -DU
237 ENDIF
239 GOTO 250
240 IF Y1 > 189 THAN
243 Y1 = 189
245 DY = -DY
247 ENDIF
250 PUT(X1 – 10, Y1 – 10), BALL
260 X = X1
265 Y = Y1
270 FOR K = 1 TO 5
275 NEXT K
280 GOTO 190
290 END
ADVANCE ANIMATION
32. Write a QBASIC program of moving
circular shape objects.
Solution:
10 REM This program makes a particular shape
20 REM scrolls up the screen leaving a
circular design
30 CLS
40 SCREEN 1
50 FOR K = 1 TO 300 STEP 10
60 LINE(50, 120 – K) – (100, 300 – K), 2, BF
70 LINE(100, 70 – K) – (200, 300 – K), , BF
80 LINE(150, 120 – K) – (250, 300 – K), 2, BF
90 SLEEP 1
100 LINE(50, 120 – K) – (100, 300 – K), 0, BF
110 LINE(100, 70 – K) – (200, 300 – K), 0, BF
120 LINE (150, 120 – K) – (250, 300 – K), 0,
BF
130 CIRCLE(150, 150), K, 1
140 NEXT K
33. Write QBASIC program that display a
picture of human face using object.
Solution:
5 CLS
10 REM This programme displays the picture of
a human face
20 SCREEN 1
30 FOR K = 1 TO 10
40 COLOR 7, 2
50 CIRCLE(150, 100), 70
60 CIRCLE(120, 80), K
70 CIRCLE(180, 80), K
80 LINE(150, 80) – (170, 110)
90 LINE-(150, 110 – K)
100 CIRCLE(150, 130), K
110 SLEEP 1
120 NEXT K
34. Write a QBASIC programme that generate a
waving Nigerian flag on a pole using objects.
Solution:
5 CLS
10 REM This program generates Nigerian flag
on a pole
20 SCREEN 9
30 FOR K = 1 TO 100
40 LINE(150, 100) – (200, 140), 2, BF
50 LINE(200, 100) – (250, 140), 7, BF
60 LINE(250, 100) – (310, 140), 2, BF
70 LINE(300, 100) – (310, 300), 1, BF
80 SLEEP 1
90 LINE(150, 100) – (200, 140), 0, BF
100 LINE(200, 100) – (250, 140), 0. BF
110 LINE(250, 100) – (300, 140), 0, BF
120 LINE(300, 100) – (310, 300), 1, BF
130 LINE(310, 100) – (360, 140), 2, BF
140 LINE(360, 100) – (410, 140), 7. NF
150 LINE(140, 100) – (460, 140), 2, BF
160 LINE(300, 100) – (310, 300), 1, BF
170 SLEEP 1
180 LINE(310, 100) – (360, 140), 0, BF
190 LINE(360, 100) – (410, 140), 0, BF
200 LINE(410, 100) – (460, 140), 0, BF
210 LINE(300, 100) – (310, 300), 1, BF
220 NEXT K
OPTIONAL
APPENDIX A: (CASE STUDY OF A RACING CAR)
35. Write QBASIC programme that generates a
racing car using objects
Solution:
43 CLS
44 REM This programme generates a racing car
45 SCREEN 1
46 COLOR 2
47 LINE(10 + K, 100 + J) – (10 + K, 80 + J)
48 LINE –(30 + K, 80 + K)
49 LINE –(50 + K, 60 + J)
50 LINE –(70 + K, 57 + J)
51 LINE –(90 + K, 60 + J)
52 LINE –(120 + K, 80 + J)
53 LINE –(170 + K, 80 + J)
54 LINE –(180 + K, 100 + J)
55 LINE –(180 + K, 110 + J)
56 LINE –(170 + K, 110 + J)
57 LINE(10 + K, 100 + J) – (20 + K, 110 + J)
58 LINE(65 + K, 110 + J) – (140 + K, 110 + J)
59 LINE(10 + K, 100 + J) – (35 + K, 100 + J)
60 LINE(60 + K, 100 + J) – (140 + K, 100 + J)
61 LINE(180 + K, 100 + J) – (170 + K, 100 +
J)
62 LINE(10 + K, 90 + J) – (170 + K, 90 + J)
63 LINE(70 + K, 80 + J) – (80 + K, 90 + J)
64 LINE –(80 + K, 110 + J)
65 LINE(120 + K, 80 + J) – (130 + K, 90 + J)
66 LINE –(130 + K, 110 + J)
67 LINE(50 + K, 80 + J) – (50 + K, 95 + J)
68 LINE(77 + K, 82 + J) – (80 + K, 85 + J),
5, BF
69 LINE(52 + K, 82 + J) – (55 + K, 85 + J),
5, BF
70 LINE(50 + K, 80 + J) – (50 + K, 60 + J)
71 LINE –(90 + K, 60 + J)
72 LINE(30 + K, 80 + J) – (120 + K, 80 + J)
73 LINE(52 + K, 62 + J) – (69 + K, 78 + J),
3, BF
74 LINE(71 + K, 62 + J) – (88 + K, 78 + J), 3,
BF
75 LINE(90 + K, 62 + J) – (118 + K, 78 + J)
76 LINE –(90 + K, 78 + J)
77 LINE –(90 + K, 62 + J)
78 LINE(70 + K, 80 + J) – (80 + K, 90 + J)
79 CIRCLE(50 + K, 110 + J), 15
80 CIRCLE(50 + K, 110 + J), 10
81 LINE(40 + K, 110 + J) – (60 + K, 110 + J)
82 LINE(50 + K, 105 + J) – (50 + K, 115 + J)
83 CIRCLE(155 + K, 110 + J), 15
84 CIRCLE(155 + K, 110 + J), 10
85 LINE(145 + K, 110 + J) – (165 + K, 110 +
J)
86 LINE(155 + K, 105 + J) – (155 + K, 115 +
J)
87 LINE(20 + K, 110 + J) – (35 + K, 110 + J)
88 PRINT “Set! … “; BEEP
89 SLEEP 3
90 LOCATE 10, 30: PRINT “Go! …”; BEEP
91 SLEEP 1
92 CLS
93 SCREEN 9
94 COLOR 2
95 FOR T = 1 TO 5
96 LINE (1, 50) – (700, 140), , B
97 LINE (1, 230) – (700, 230)
98 LINE (1, 320) – (700, 320)
99 FOR J = 1 TO 270 STEP 90
100 FOR K = 1 TO 1000 STEP 50
101 LINE (10 + K, 100 + J) – (10 + K, 80 + J)
102 LINE –(30 + K, 80 + J)
103 LINE –(50 + K, 60 + J)
104 LINE –(70 + K, 57 + J)
105 LINE –(90 + K, 60 + J)
106 LINE –(120 + K, 80 + J)
107 LINE –(170 + K 90 + J)
108 LINE –(180 + K, 100 + J)
109 LINE –(180 + K, 110 + J)
110 LINE –(170 + K, 110 + J)
120 LINE (10 + K, 100 + J) – (20 + K, 110 +
J)
130 LINE (65 + K, 110 + J) – (140 + K, 110 +
J)
140 LINE (10 + K, 100 + J) – (35 + K, 100 +
J)
150 LINE (60 + K, 100 + J) – (140 + K, 100 +
J)
160 LINE (180 + K, 100 + J) – (170 + K, 100 +
J)
170 LINE (10 + K, 90 + J) – (170 + K, 90 + J)
180 LINE (70 + K, 80 + J) – (80 + K, 90 + J)
190 LINE –(80 + K, 110 + J)
200 LINE (120 + K, 80 + J) – (130 + K, 90 +
J)
210 LINE –(130 + K, 110 + J)
220 LINE (50 + K, 80 + J) – (50 + K, 95 + J)
230 LINE (77 + K, 82 + J) – (80 + K, 85 + J),
5, BF
240 LINE(52 + K, 82 + J) – (55 + K, 85 + J),
5, BF
250 LINE (50 + K, 80 + J) – (50 + K, 60 + J)
260 LINE –(90 + K, 60 + J)
270 LINE (30 + K, 80 + J) – (120 + K, 80 + J)
280 LINE(52 + K, 62 + J) – (69 + K, 78 + J),
3, BF
290 LINE(71 + K, 62 + J) – (88 + K, 78 + J),
3, BF
295 LINE (90 + K, 62 + J) – (88 + K, 78 + J)
300 LINE –(90 + K, 78 + J)
310 LINE –(90 + K, 62 + J)
320 LINE (70 + K, 80 + J) – (80 + K, 90 + J)
330 CIRCLE (50 + K, 110 + J), 15
340 CIRCLE (50 + K, 110 + J), 10
350 LINE (40 + K, 110 + J) – (60 + K, 110 +
J)
360 LINE (50 + K, 105 + J) – (50 + K, 115 +
J)
370 CIRCLE (155 + K, 110 + J), 15
380 CIRCLE (155 + K, 110 + J), 10
390 LINE (145 + K, 110 + J) – (165 + K, 110 +
J)
400 LINE (155 + K, 105 + J) – (155 + K, 115 +
J)
410 LINE (20 + K, 110 + J) – (35 + K, 110 +
J)
420 LINE (10 + K, 100 + J) – (10 + K, 80 +
J), 0
430 LINE –(30 + K, 80 + J), 0
440 LINE –(50 + K, 60 + J), 0
450 LINE –(70 + K, 57 + J), 0
460 LINE –(90 + K, 60 + J), 0
470 LINE –(120 + K, 80 + J), 0
480 LINE –(170 + K, 90 + J), 0
490 LINE –(180 + K, 100 + J), 0
500 LINE –(180 + K, 110 + J), 0
510 LINE –(170 + K, 110 + J), 0
520 LINE (10 + K, 100 + J) – (20 + K, 110 + J), 0
530 LINE (65 + K, 110 + J) – (140 + K, 110 +
J), 0
540 LINE (10 + K, 100 + J) – (35 + K, 100 +
J), 0
550 LINE (60 + K, 100 + J) – (140 + K, 100 +
J), 0
560 LINE (180 + K, 100 + J) – (170 + K, 100 +
J), 0
570 LINE (10 + K, 90 + J) – (170 + K, 90 +
J), 0
580 LINE (70 + K, 80 + J) – (80 + K, 90 + J),
0
590 LINE –(80 + K, 110 + J), 0
600 LINE (120 + K, 80 + J) – (130 + K, 90 +
J), 0
610 LINE –(130 + K, 110 + J), 0
620 LINE (50 + K, 80 + J) – (50 + K, 95 + J),
0
630 LINE (77 + K, 82 + J) – (80 + K, 85 + J),
0, BF
640 LINE (52 + K, 82 + J) – (55 + K, 85 + J),
0, BF
650 LINE (50 + K, 80 + J) – (50 + K, 60 + J),
0
660 LINE –(90 + K, 90 + J), 0
670 LINE (30 + K, 80 + J) – (120 + K, 80 +
J), 0
680 LINE (52 + K, 62 + J) – (80 + K, 85 + J),
0, BF
690 LINE (71 + K, 62 + J) – (88 + K, 78 + J),
0, BF
700 LINE (90 + K, 62 + J) – (118 + K, 78 +
J), 0
710 LINE –(90 + K, 78 + J), 0
720 LINE –(90 + K, 62 + J), 0
730 LINE (70 + K, 80 + J) – (80 + K, 90 + J),
0
740 CIRCLE (50 + K, 110 + J), 15
750 CIRCLE (50 + K, 110 + J), 10
760 LINE (40 + K, 110 + J) – (60 + K, 110 + J),
0
770 LINE (50 + K, 105 + J) – (50 + K, 115 +
J), 0
780 CIRCLE (155 + K, 110 + J), 15, 0
790 CIRCLE (155 + K, 110 + J), 10, 0
800 LINE (145 + K, 110 + J) – (165 + K, 110 +
J), 0
810 LINE (155 + K, 105 + J) – (155 + K, 115 +
J), 0
820 LINE (20 + K, 110 + J) – (35 + K, 110 +
J), 0
830 NEXT K
840 NEXT J
850 NEXT T
I WILL STOP HERE FOR NOW. I HOPE THIS WILL HELP YOU FOR YOUR PROGRAMMING.
THANKS AND GOD BLESS YOU.