Search This Blog

Programming with QBASIC

Syllabus:
Introduction to programming: Problem analysis, algorithm, flow chart, coding, compilation, execution, debugging and testing; programme documentation.
Constants, types of variables, array variables, arithmetic operations, precedence rule, parentheses rule, logical operations, few important library functions.
Design of programs: Initialization, input, validation, processing, print, closing a procedure.
Sequential structure of programming with example
Selective structure of programming: IF statement, block IF, nested IF statement syntax and example.
Repetitive structure of programming: FOR … NEXT loop statement syntax and example.
Function and Subroutine: Introduction with a simple example.
Graphics: LINE, CIRCLE, Changing the color of background and text.

INTRODUCTION TO PROGRAMMING
Principle behind programming with any programming language is nothing but computer-aided problem solving. It consists of the following processes:
1. Problem analysis.
2. Algorithm development.
3. Program coding.
4. Program compilation and execution
5. Program debugging and testing.
6. Program documentation.

1. Problem analysis
Whenever a problem is obtained it is first analyzed systematically to feed into a computer program. The following points are emphasized:
(i) Some information/data should be input, so input data is identified. Input information may be some data.
(ii) After program execution some information / data should come out as output, so output data is also identified. Output information may be data or graph or both.
(iii) The input data should be manipulated by some procedure, say by some calculation etc. to obtain the output information.
Example
Given three numbers x1, x2, and x3; find the largest of these numbers.
Problem analysis
Here the only information required as output in the largest of the three numbers x1, x2, and x3. The input information are the numbers x1, x2 and x3. In computer programming those three numbers are given three variable names e.g. X1, X2 and X3 to represent the memory cells containing these data items.
Initially, X1 is compared with X2. If X1 is larger than X2, it is compared with X3. If X1 is larger than X3, X1 is the largest number; otherwise X3 is the largest number. However, if X1 is not larger than X2, X2 is compared with X3. If X2 is larger than X3, X2 is the largest number; otherwise X3 is the largest number.
2. Algorithm development and flow chart
Once the problem input and output data are identified and precise statement of the problem in terms of the input and output data is available, the problem is expressed by abbreviated statements in English (or other natural languages) that specify the steps to be performed in solving the problem are called program design language (PDL) statements or pseudocode.

The finite set of PDL statements, which describe the logic for solving a specific problem, is called an algorithm and the process of defining PDL statements is called the algorithm development.
An algorithm has the following features:
1. it has a finite number of inputs
2. it terminates after a finite number of steps
3. the action taken in each step are precise and unambiguous
4. all operations specific in each step can be done exactly and in a finite amount of time and
5. it has one or more outputs derived from the inputs by applying the algorithm.

Algorithm of the previous problem
1. Input X1, X2, X3.
2. Compare X1 with X2.
3. If X1 is larger than X2, compare X1 with X3.
If X1 is larger than X3, report X1 as the largest; otherwise report X3 as the largest.
4. If X2 is not larger than X2, compare X2 with X3.
If X2 is larger than X3, report X2 as the largest; otherwise report X3 as the largest.
5. End.

Flowcharts: A diagram of the statements of an algorithm showing the relationships among these statements is called flowchart.
Symbols used in flowchart

Name of the symbol
Symbol
Use
1. Terminal Symbol




2. Input / Output Symbol



3. Computation or Process
Symbol


Used to indicate the beginning, or end, of a n algorithm.

A parallelogram is used to indicate the input and output, of information to or from the computer.

A rectangle is used to indicate the assignment of values or computation.

4. Flowline Symbol




5. Decision Symbol





6. Loop Structure Symbol

An arrow is used to indicate the order in which the steps of the algorithms are to be carried out.

A diamond is used to indicate a decision point.
The question inside the diamond can have answers ‘yes’ or ‘no’

A hexagon indicates the beginning of a loop structure.

Flowchart for the above problem
















3. Program coding
Program coding is the process of translating the solution of the problem, as worked out in algorithm, or flowchart, into the exact instructions that will make up the program.
The algorithm must be written using the vocabulary of the a programming language (ForTran, BASIC, C etc.) and must conform to the syntax of that programming language.

REM
REM QBASIC Code for Problem 1
REM
DIM X1 AS SINGLE, X2 AS SINGLE, X3 AS SINGLE
INPUT X1, X2, X3
IF (X1 > X2) THEN
IF (X1 > X3) THEN
PRINT X1
ELSE
PRINT X3
ENDIF
ELSE
IF (X2 > X3) THEN
PRINT X2
ELSE
PRINT X3
ENDIF
ENDIF
STOP
END

4. Program processing
Statements are entered in the computer through a key board, the program is compiled by the QBASIC compiler and then the program is executed.
5. Program debugging and testing
Very often, no matter how simple, or complex, the programs do not execute successful for the first time; the programs contain at least a few errors or bugs. The programs are therefore, debugged, i.e. errors or bugs are eliminated, till they execute successfully.
6. Program documentation
Before putting the program into use, the documentation for the program is provided and this is the last step in the programming process. Program documentation provides a written description of each step of the program.


BASIC stands for Beginner's All-Purpose Symbolic Instruction Code
QBASIC is also a version of BASIC language, called Quick BASIC. It was developed by Bill Gates and Paul Allen way back in 1975.
Advantages of QBASIC
1. It is a very easy to use language because many of its commands are pure English like LET, PRINT, etc. .
2. It has a simple structure for its programs: its lines may be numbered as 10, 20, 30, etc. and are executed in order.
3. It has structural similarity with many other higher level languages.
4. Simple graphics can be drawn with this program.
5. Easy helps are given readily.

PROGRAMMING IN QBASIC
How to start QBASIC program
First of all search the program QBASIC.EXE in the hard disk or floppy disk. Copy it in the floppy. Go to
Start ® Click Program ® Click MSDOS.
A DOS screen will be opened within the Windows. Go to A:\>QBASIC ¿
QBASIC screen will open. Go on typing the codes in the QBASIC window. Press F5 to run the program.

CONSTANTS
A constant is a quantity whose value does not change during program execution. It may be of numeric, character or logical type.

1. Numeric constants
Any string of digits, preceded by a single algebraic sign, is called a numeric constant. The general rules for forming numeric type constants are given below:
(a) The decimal digits 0,1,2,3,….,9 are used.
(b) The minus sign must precede a negative constant; a plus sigh is optional and an unsigned constant is considered positive.
(c) No commas are to be included in a string of digits.
(d) The number of digits of a constant is limited by either a maximum number of digits or a maximum magnitude.
(e) A space within a numeric constant is not allowed.

Numeric constants are subdivided into (i) Integer and (ii) Real constants.
(i) Integer constant (also called fixed point constant) is a constant, which does not include a decimal point.
Thus
25 0 -7 +15274
are valid integer constants, whereas the following are not for the reasons indicated beside:
Incorrect
Reasons
Correct
18.0
-284.
10,235
3-
– – 7
Contains a decimal point
Contains a decimal point
Contains a comma.
Minus sign cannot be after the number.
Two minus signs cannot precede a number
18
-284
10235
– 3
– 7

(ii) Real constants (also called floating point constant) is a constant with a decimal point and may have a fractional part.
Thus
18.3 – 163.0 42. + .0125 + 0.256
are valid real constants, whereas the following are not valid real constants,
Incorrect
Reasons
Correct
1,465.3
Contains a comma.
1465.3
– 56
Contains no decimal point
– 56.

Scientific notation
Very small and very large numbers are expressed conveniently by scientific notation.
Standard decimal form
Scientific notation
BASIC notation
0.0000567
0.0000000679
20000000000
– 0.000000076
5.67 x 10 –5
6.79 x 10 –8
2.0 x 1010
–7.6 x 10–8
5.67E–5
6.79E–8
2.0E+10
–7.6E-8

So the general form of a scientific notation is
(co-efficient) x 10 (integer) = (co-efficient) E (integer)
The part appearing before E is called mantissa and the part following the E is called exponent.

2. Character constant
Any sequence of acceptable characters in the BASIC character set, enclosed within double quote (“ “), is called a character constant or string constant.
The number of characters in a character constant is the length of the constant.
If an apostrophe sign is to be one of the characters of the constants, it must be entered as a single quote.
Examples:
Character / String constant
Length of the string
Remarks
“ UNIX–PC ”
7
‘–‘ is a character
“ I.P.T.”
6
‘ .’ dots are also characters
“Don’t”
5
‘ single quote is a character
“More Pages”
10
‘ ‘ space is also a character

VARIABLES
A variable is a name used to identify the data stored in a memory location whose content may change during program execution. The rules for naming a variable are given below:
1. A variable name can contain letters A to Z (i.e. alphabet characters) and digits 0 to 9 (i.e. numerical characters) but no special characters such as +, $, *, etc.
2. The first character of a variable must be a letter (A to Z).
3. Maximum length of a variable name may be 40 character long.
4. No space is allowed in the variable name.
The following are valid variable names
A2X3 ITEM PAY_DAY123
The following are not valid variable names
Variable Name
Reasons
2AB
The first character is not a letter
BETA–3
(–) is a special character
X18.2
( . ) is a special character
ABCDEFGHIJKLMNOPQRSTUVWZYZ12
More than 40 characters
§ Certain words called reserve words such as PRINT, INPUT, CLS etc. are part of the BASIC language and hence are not valid variable names.

TYPE SPECIFICATION STATEMENTS
A variable can store a certain type of data after specifying the type of that variable. Following are the type specification statement of variables:
Type specification
Basic Keywords
Remarks
Data type suffix
Integer
INTEGER
A 16-bit signed integer variable.
%
Long-integer
LONG
A 32-bit signed integer variable.
&
Single-precision
SINGLE
A single-precision 32-bit floating-point variable.
!
Double-precision
DOUBLE
A double-precision 64-bit floating-point variable.
#
String
STRING * n%
A fixed-length string variable n% bytes long.

String
STRING
A variable-length string variable.
$

Type specification statements may be declared explicitly in the following syntax
DIM variablename AS variabletype
e.g.
DIM NUM AS INTEGER
DIM NUM1 AS INTEGER, NUM2 AS LONG, NUM3 AS SINGLE, NUM4 AS DOUBLE
DIM FNAME AS STRING*15 –FNAME is a variable that contains maximum length of 15 characters
DIM LNAME AS STRING – LNAME contains any length of characters.

A variable can also be declared implicitly by within the program by Variablename suffix For example
NUM%
NUM1%
NUM2&
NUM3!
NUM4#
FNAME$
LNAME$
Limits of QBASIC environment

Maximum limit
Minimum limit
Variable name length
40 characters
1 character
String length
32,767 characters
0 characters
Integers
32,767
– 32,767
Long Integers
2,147,483,647
-2,147,483,648
Single-precision numbers
Positive number
Negative number

3.402823E+38
-2.802597E-45

2.802597E-45
-3.402823E+38
Double-precision numbers
Positive number
Negative number

1.79769313486231D+308
-4.940656458412465D-324

4.940656458412465D-324
-1.79769313486231D+308

Undeclared variables
If a variable is not explicitly declared by DIM variablename AS variabletype then QBASIC automatically assigns the variable as Single precision.

ARRAY VARIABLES
An array is a collection of values, or data, that are related in some way. The data may be stored in consecutive memory locations, each of which can be accessed directly. Such a collection of values, or data, is called an array and the data item its element.
e.g. The test scores of 100 students are S1, S2, S3, ….., S100. Where S is the single group name for all the student’s score and the subscript (1,2,3, .. .100) identifies the test scores of a specific student. Thus S1 is the scores of the first student, S10 is the score of the 10th student and so on.
In BASIC language the above data set is represented as follows: S(1), S(2), S(3),…., S(100).
The number of subscripts in a pair of parentheses may be more than one.
e.g. S(23,34,2)

The name and the range of subscript of an array may be declared in a DIM statement of the form:
DIM variable (m1 TO n1, m2 TO n2, .. , mk TO nk) AS type
§ Each pair of integer constants, usually called parameters.
m1 is the lower bound of the array, minimum value is 0.
n1 is the upper bound of the array.
§ AS type Declares the data type of the array or variable (INTEGER, LONG, SINGLE, DOUBLE, STRING)

e.g. DIM PUPIL (0 TO 10) AS INTEGER, MARKS (10 TO 50) AS LONG

ARITHMETIC OPERATIONS
The numeric data can be manipulated using arithmetic operations. A combination of variables and constants together with operation symbols, we use the phrase expression.
For numeric data there are six arithmetic operations:

Operation
Arithmetic Operator
BASIC operator
Examples
Addition
+
+ (plus sign)
P + Q
Subtraction

– (minus sign)
P – Q
Multiplication
x
* (asterisk sign)
P * Q
Division
¸
/ (slash sign)
P / Q
Exponentiation
PQ
^ (caret sign)
P ^ Q
Negation
– P
– P
– P

Rules
1. Single mode operation
When two constants or variables of the same type are combined, using one of the four arithmetic operations (+, –, *, / ) the result will be same type as the constants or the variables.
e.g. 8 + 2 = 10 8 – 2 = 6 8 * 2 = 16 8 / 2 = 4
e.g. 8. + 2. = 10. 8.0 – 2. = 6.0 8. * 2. = 16. 8. / 2. = 4.

If both the constants are integers and after division a fraction appears then the result will be the integer portion of the fraction
e.g. 5 / 2 = 2 (not 2.5 ) 1 / 2 = 0 (not 0.5 )
If both the constants are real (i.e. either single or double) and after division a fraction appears then the result will be the real.
e.g. 5./2. = 2.5 1. / 2. = 0.5
2. Mixed mode operation
When one integer and another real constants or variables are involved in an operation then the integer is automatically converted to its real equivalent, and the result is a real type.
e.g. 5 / 2.0 = 2.5
3. Exponentiation
(a) If the exponent is an integer value the QBASIC performs the operation by repeated multiplication.
e.g. – 4 ^ 2 = (–4) * (–4) = 16
(b) If the exponent is a real (either single or double) then QBASIC performs the operation by logarithmic method. E.g. – 4.0 ^ 2.0 = exp (2.0 log (–4.0)) = Undefined
Because logarithm of negative value is undefined.
N.B.
(–4.0)2.0 = x (let) Taking logarithm on both sides yields: 2.0 log (–4.0) = log x
Therefore, x = 102.0 log (–4.0). log (–4.0) is undefined.
PRECEDENCE RULE
Precedence rule states that the operations will be performed in the following descending order of precedence:
§ When the order of precedence of operators is same, such as in multiplication and division, the operations will be performed in order from left to right.
e.g.
§ If exponentiation operator appears multiple times in succession, the operation is performed from right to left.
PARENTHESES RULE
Parentheses rule states that operation will be performed in the innermost set of parentheses and then in the next outer set, etc., until all the operations inside the parentheses have been performed.
LOGICAL OPERATIONS OR BOOLEAN OPERATIONS
Boolean operators perform bit manipulations, Boolean operations, or tests on multiple relations. They return a true (nonzero) or false (zero) value to be used in making a decision.

Boolean operators
Description
Syntax
Examples

NOT
Used to perform logical negation on an expression.
result = Not expression

Dim A, B, C, D, MyCheck
A = 10: B = 8: C=5
MyCheck = Not (A > B) ' Returns False.

AND
Used to perform a logical conjunction on two expressions.
result = expression1 And expression2

Dim A, B, C, D, MyCheck
A = 10: B = 8: C=5
MyCheck = A > B And B > C ' Returns True

OR
Used to perform a logical disjunction on two expressions.
result = expression1 Or expression2

Dim A, B, C, D, MyCheck
A = 10: B = 8 : C=5
MyCheck = A > B Or B > C ' Returns True.
Other logical operators are XOR (exclusive or), EQV (equivalence) and IMP (impliactions).

LIBRARY FUNCTIONS
There are a number of computational processes that require multiple steps to obtain the desired result and are used again and again by different programmers. Examples include computing the square root of a given number, determining the absolute value of an expression, finding the largest value from a set of numbers and so on.
Instead of programming them again and again QBASIC provides a number of built-in functions. This buit-in functions are called intrinsic functions.

Function
Description
Syntax
Example
ABS
Returns the absolute value of a number.
ABS(numeric-expression)
PRINT ABS(45.5 – 100.0)
'Output is: 54.5
CINT
rounds a numeric expression to an integer

CINT(numeric-expression)
PRINT CINT(12.49), CINT(12.51)
'Output is: 12 13
CLNG
rounds a numeric expression to a long (4-byte) integer

CLNG(numeric-expression)
PRINT CLNG(338457.8) 'Output is: 338458

CSNG
converts a numeric expression to a single-precision value
CSNG(numeric-expression)
CSNG(975.3421515)
'Output is: 975.3422


CDBL
converts a numeric expression to a double-precision value

CDBL(numeric-expression)
CDBL(1 / 3)
'Output is: .3333333333333333
FIX
truncates a floating-point expression to its integer portion

FIX(numeric-expression)
PRINT FIX(12.49), FIX(12.54)
'Output is: 12 12
INT
returns the largest integer less than or equal to a numeric expression

INT(numeric-expression)

PRINT INT(12.54), INT(-99.4) 'Output is: 12 -100
ATN
returns the arctangent of a specified numeric expression
ATN(numeric-expression)
CONST PI=3.141592654
PRINT ATN(TAN(PI/4.0)), PI/4.0
'Output is: .7853981635 .7853981635
PRINT (COS(180 * (PI / 180)))
'Output is: -1

N.B.
To convert from degrees to radians, multiply degrees by (PI / 180).
SIN
return the sine of a specified angle in radian
SIN(angle)

COS
return the cosine of a specified angle in radian
COS(angle)
TAN
return the tangent of a specified angle in radian
TAN(angle)



Function
Description
Syntax
Example
EXP
returns e raised to a specified power, where e is the base of natural
EXP(numeric-expression)
N.B. For EXP, the numeric expression is a number less than or equal to 88.02969.
PRINT EXP(0), EXP(1) 'Output is: 1 2.718282
LOG
returns the natural logarithm of a numeric expression
LOG(numeric-expression)
N.B. For LOG, any positive numeric expression.
PRINT LOG(1), LOG(EXP(1)) 'Output is: 0 1

MOD
Divides one number by another and returns the remainder.
numeric-expression1 MOD numeric-expression2
numeric-expression1, numeric-expression2 – Any numeric expressions. Real numbers are
rounded to integers.
PRINT 19 MOD 6.7
'QBasic rounds 6.7 to 7, then divides.
'Output is: 5

SQR
Returns the square root of a numeric expression.
SQR(numeric-expression)
numeric-expression – A value greater than or equal to zero.
PRINT SQR(25), SQR(2)
'Output is: 5 1.414214

STRING MANIPULATIONS

Function
Description
Syntax
Example
INSTR
Returns the position of the first occurrence of a string in another string
INSTR([start%,]stringexpression1$,stringexpression2$)
N.B.
start% – Sets the character position where the search begins. If start% is omitted, INSTR starts at position 1.
stringexpression1$ – The string to search
stringexpression2$ –The string to look for.
a$ = "Microsoft QBasic"
PRINT INSTR(1, a$, "QBasic")
‘Output is 11

LEFT$
RIGHT$
Return a specified number of leftmost or rightmost characters in a string.
LEFT$(stringexpression$,n%)
RIGHT$(stringexpression$,n%)
N.B.
stringexpression$ – Any string expression.
n% – The number of characters to return, beginning with the leftmost or rightmost string character.
a$ = "Microsoft QBasic"
PRINT LEFT$(a$, 5)
'Output is: Micro

PRINT RIGHT$(a$, 5)
'Output is: Basic
MID$
The MID$ function returns part of a string (a substring).
The MID$ statement replaces part of a string variable with another string
MID$ ( stringexpression$, start% [,length%] )

MID$ ( stringvariable$, start% [,length%] )=stringexpression$

N.B.
stringexpression$ – The string from which the MID$ function returns substring, or the replacement string used by the MID$ statement. It can be any string expression.
start% – The position of the first character in the substring being returned or replaced.
length% – The number of characters in the substring. If the length is omitted, MID$ returns or replaces all characters to the right of the start position.
stringvariable$ – The string variable being modified by the MID$ statement.

a$ = "Where is Paris?"
PRINT MID$(a$, 10, 5)
'Output is: Paris

Text$ = "Paris, France"
PRINT Text$
'Output is: Paris, France

MID$(Text$, 8) = "Texas "
PRINT Text$
'Output is: Paris, Texas

LEN
Returns the number of characters in a string or the number of bytes
required to store a variable.
LEN(stringexpression$)
LEN(variable)

N.B.
stringexpression$ – Any string expression.
Variable – Any nonstring variable.

a$ = "Microsoft QBasic"
PRINT LEN(a$)
‘Ouput is 16

Exercise
Write the QBASIC equivalent of the following expressions:

Arithmetic Expression
QBASIC Expression
Arithmetic Expression
QBASIC Expression
a + b / c – d
A + B/C – D
X = SQR (Y)
(A + B ) / (C + D )
x = y – a
X = ABS (Y)
a2 – b2.
A ^ 2 – B ^ 2
x = ey + a
X = EXP(Y + A)

A*B / (C^2 – D )
log10 (2a – 3b)2.
LOG((2*A – 3*B)^2)/LOG (10)

Do the following exercises
Convert the following arithmetic expressions to QBASIC expressions
(a) az – 2.0
(b)
(c) (an)m + anam.
(d)
(e)
(f)
(g) loge (x + 3y )3.
(h) cos(2x – y) + x2 + y2 + exy.
(i)
(j)



Convert the following QBASIC expressions to arithmetic expressions
(a) SQR (3*A^2 + * B ^ 2)
(b) SQR (X ^ 2 / ( Y + Z) )
(c) COS (2*X–Y) + ABS (X^2 + Y^2 ) + EXP (X * Y)
(d) LOG ( ( 2 * A – 3 * B ) ^ 2
(e) A + B / ABS (M – N)
(f) EXP (ABS (Z) ) – Y ^ 3 / ABS (X)
(g) COS (LOG ( 3 * X + Y ) )
(h) SQR ( ABS (SIN (A – ABS (B) ) ) ) )
(i) ABS (SQR ( A – B ^ 2 ) – C ^ 2 / SIN (X + Y) )

Exercise
Calculate the values of each of the following expressions as per precedence rules:

(a) 9 – 6 + 3
(b) 3 ^ 2 + 4 / 5
(c) 2.0 / 4
(d) 3 + 2 ^ 3
(e) (3 + 2) ^ 3
(f) – 4.0 ^ 4
(g) – (4.0 ^ 4)
(h) 12.0 / 3.0 * 2.0
(i) 12 / (3.0 * 2.0)
(j) (2 ^ 3) ^ 2
(k) 2 ^ 3 ^ 2
(l) (( 3 + 2 ) ^ 3 ) / 5
(m) (3 + 2 ^ 3 ) / 5
(n) (3. + 2 ^ 3) / 5


The values are calculated as follows:
(a) 9 – 6 + 3 = 3 + 3 = 6
(b) 3 ^ 2 + 4 / 5 = 9 + 4/5 = 9 + 0 = 9
(c) 2.0 / 4 = .5
(d) 3 + 2 ^ 3 = 3 + 8 = 11
(e) (3 + 2) ^ 3 = 5 ^ 3 = 125
(f) – 4.0 ^ 4 = – (4.0)^4 = – (4.0) (4.0) (4.0) (4.0) = – (256.0) = -256.0
(g) – (4.0 ^ 4) = – (4.0 * 4.0 * 4.0 *4.0) = – (256.0) = – 256.0
Rest of the problems do yourself.



DESIGN OF PROGRAMS
Although computer program differ greatly in purpose and processing, they can all be organized into the following functional modules:
1. Initialization establishes initial values for some variables, prints headings, messages etc.
2. Input performs input of data required by the program.
3. Validation performs validation of input data to detect errors, omissions etc.
4. Processing performs computation, or data manipulation.
5. Output performs output of data to be provided by the program like printing the output.
6. Closing procedure performs procedure to end the execution of the program.

STRUCTURE OF PROGRAM
All computer program can be coded by using only three logical structures (patterns) given below, or a combination of these structures:
1. Sequential structure: It consists of one action followed by another. In other words performs operation A and then operation B and so on.
2. Selective structure: It consists for a test for a condition followed by two or more alternative paths for the program to follow The program selects one of the paths depending on the test of the condition.
3. Repetitive or Iterative structure: Here an operation, or a set of operations, is repeated as long as some condition satisfied.

SEQUENTIAL STRUCTURE
1. Assignment statement
The assignment statement is used to assign values to variables and has the form of variable = expression.
Where expression may be a
(a) constant (b) a variable to which a value has been assigned
(c) a formula which the computer can evaluate.
e.g. e.g.




Examples of Rules of assignments
1. In the left-hand side of the ‘=’ symbol a variable should be there.
2. No value, constant, or expression can be on the left-hand side of the ‘=’ symbol.
3. In the right-hand side any value, constant or valid expression can be placed.
4. The type of variable and the expression or constants in both the sides should be same.
e.g X = 2 Y = (N + 4) / 3 K = 1. / 3. + 1./3 IPT = “Institute”

Hence, the following are not valid QBASIC assignment statements for the reasons indicated:
15 = N
Variable N is on the right instead on the left of the = sign.
X + 4.3 = 3.14
Numeric expression should not appear to the left-hand side of = sign.
STRING = 4 & 7
4 & 7 is not a legal expression.
A = B = 1
B = 1 is not a valid expression
DIM A AS INTEGER
A = “abcd”
When A is declared as integer in the first line a string constant “abcd” cannot be assigned to A.

2. Input and output statements
The computer can manipulate the data only it is available to its memory. Data can be input in the memory either within the program or by the user during program execution.
Data can be input in the memory by the following statements and the result is shown beside:
Sl. No.
Syntax
Example
Displayed result during program execution
1
INPUT variablename
INPUT ipt
_

2
INPUT “prompt”; variablename
INPUT “First Data:”; X
First Data: ?_

3
INPUT “prompt”, variablename
INPUT “First Data:”, X
First Data: _

Sl. No.
Syntax
Example
Displayed result during program execution
4
INPUT variable1, variable2
INPUT X, Y
?12 , 20 Press enter
5
INPUT “prompt”, variable1, variable2
INPUT “X=”,X, Y
X= 12, 20
6
INPUT “prompt”; variable1, variable2
INPUT “X=”;X, Y
X= ? 12, 20

Data can be output from the memory by the following statements and the result is shown beside:
PRINT writes data to the screen or to a file.
LPRINT prints data on the printer LPT1.
The general syntax is
PRINT [expressionlist] [{; ,}]
LPRINT [expressionlist] [{; ,}]
_ expressionlist A list of one or more numeric or string expressions to print.
_ {; ,} Determines where the next output begins:; means print immediately after the last value.,
means print at the start of the next print zone. Print zones are 14 characters wide.


Sl. No.
Syntax
Example
Displayed result during program execution
1
PRINT variable1, variable2
PRINT A, X, Z
25 10 12
2
PRINT variable1; variable2
PRINT A; X; Z
25 10 12
3
PRINT string constant
PRINT “Institute”
Institute
4
PRINT string1 + string2
PRINT “ab” + “cd”
Abcd
5
PRINT string, variable
PRINT “X=”,X
X= 20
6
PRINT string; variable
PRINT “X=”;X
X= 20

STOP
The STOP statement is used to halt the program execution. But the program execution will not terminate here and the control will not be given to the user. Press F5 to continue.

END
This statement terminates a program and give the control to the user.
END must be the last statement of a program.

Example: Write the algorithm, flow chart and the program code.
1. Write a program that accepts input temperature in degree Farenheit, converts it to degree Centigrade, and outputs the temperature in degree Centigrade.
2. Write a program that reads values for the three sides of a triangle, calculates its perimeter and its area, and outputs these values.
3. Write a program that inputs the radius of a circle, computes and outputs (a) the area of the circle, (b) the area of the largest square contained within the circle, and (c) the ratio of (a) to that of (b). Use the constant, PI, the value 3.1416.
4. Write a program to find the surface area (SUR), volume (VOL) of a box with dimensions a,b,c where
SUR = 2(ab +bc + ca) and VOL =abc.
5. Assuming a1b2 – a2b1 ¹ 0, the solution of the linear equations a1x + b1y = c1 and a2x + b2y = c2 is given by

Write a program that reads values of a1, b1, c1, a2, b2, c2 and calculates and prints the solution pair x and y.
6. Suppose that a car starts from rest and has constant acceleration for t seconds. The final velocity v and the distance traveled d by the car is given by the formulae:
v = ft and d = ½ ft2.
Write a program that reads f and t, and prints t, d, and v.
7. Write a program to convert a given measurement in feet to an equivalent one in (i) yards, (ii) inches, (iii) centimeters, (iv) meters. (1yard = 3 ft, 1 foot = 12 inches, 1 inch = 2.54 centimeter, 1 meter = 100 centimeter)


2. SELECTIVE STRUCTURE
The selective structure consists of a test for a condition followed by alternative paths which the program can follow. Selection among alternative paths is programmed with the IF statements.

Relational expression
If A and B represents two numeric or string constants they can be combined by the following six relational operators:

Relational operator
Application in QBASIC
Meaning
<
A < B
A is less than B
< =
A < = B
A is less than or equal to B
=
A = B
A is equal to B
< >
A < > B
A is not equal to B
>
A > B
A is greater than B
> =
A > = B
A is greater than or equal to B

§ The relational operator can be used to compare numerical values. If a relational operator is present in between two numerical expression then the numerical expression will be executed first.
e.g. Assuming the two single values X = 20.0, Y = 4.0 and the integer variable M = 5 then
X + 5 > M * Y ® 20.0 + 5 > 5*4.0 ® 25.0 > 20.0 ® Answer is “True”
§ The relational operators can be used to compare two string expressions.
e.g. Let us assume three string variables X = “a”, Y = “b”, Z = “A”
X > Y ® True because ASCII code of “a” is 97 and “b” is 98
X > Z ® True because ASCII code of “a” is 97and “A” is 65

Logical expression
Logical operators used in QBASIC
Symbol
Expression
Meaning
Logical value
NOT
NOT A
Negation
True if and only if A is False.
AND
A AND B
Conjunction
True if and only if, A and B are both True.
OR
A OR B
Disjunction
True if and only if, at least one is True.
The following two truth tables summarizes the logical values for these expressions:
Truth Table - I
A
NOT A
T
F
F
T
Truth Table -II
A
B
A AND B
A OR B
T
T
F
F
T
F
T
F
T
F
F
F
T
T
T
F

Precedence of relational and logical operations
Precedence 1: Parentheses rules
Precedence 2: Numeric expressions
Precedence 3: Relational operations
Precedence 4: Logical operations
If more than one logical operations are in one expression then the execution will start from the left hand side.
e.g. Say A =3, B =4 and C =12 and the expression will be executed as follows:
A > B AND ( A <> C) OR NOT (A = B)
Þ A > B AND ( True OR B < > C) OR NOT (A = B)
Þ A > B AND ( True OR True) OR NOT (A = B)
Þ A > B AND True OR NOT (A = B)
Þ A > B AND True OR NOT ( False )
Þ A > B AND True OR True
Þ False AND True OR True
Þ False OR True
Þ True
GOTO statement
The GOTO statement is used to jump from one line to a another line.
Syntax: GOTO line
_ line The label or number of the line to execute next.

Precaution: Indiscriminate use of GOTO statement may make a program difficult to debug and understand therefore, GOTO should be used with caution.
10 LARGER = V2
SMALLER = V3
GOTO 10e.g.




IF Statements
Executes a statement or statement block depending on specified conditions.
Syntax I (Logical IF Statement)
IF condition THEN statements [ELSE statements]

Syntax II (Block IF statement)
IF condition1 THEN
[statementblock-1]
[ELSEIF condition2 THEN
[statementblock-2]]...
[ELSE
[statementblock-n]]
END IF

condition1 Any expression that can be evaluated as
condition2 true (nonzero) or false (zero).
statementblock-1 One or more statements or one or more lines.
statementblock-2
statementblock-n
statements One or more statements, separated by colons.

Example of Logical IF : IF (num < 100 ) PRINT num
IF (num < 100 ) PRINT num ELSE PRINT “No number”
Example of block IF: DIM v1 AS INTEGER, v2 AS INTEGER, larger AS INTEGER
INPUT v1,v2
IF (v1 > v2) THEN
larger = v1
ELSE
larger = v2
END IF
PRINT larger
END
Example of nested block IF:
DIM v1 AS INTEGER, v2 AS INTEGER
INPUT v1, v2
IF (v1 > v2) THEN
PRINT “Semester 1”
ELSE
IF (v2 > v1) THEN
PRINT “Semester 2” IF Block
ELSE Nest
PRINT “Semester 3”
END IF
END IF
END

Exercises
Problem1. Write a program to find the largest of the three numbers.
Solution:
Fig. Flow chart
Program coding:
REM Program to find the largest of 3 numbers
DIM x1 AS SINGLE, x2 AS SINGLE, x3 AS SINGLE, largest AS SINGLE
INPUT x1, x2, x3
IF (x1 > x2) THEN
IF (x1 > x3) THEN
largest = x1
ELSE
largest = x3
END IF
ELSE
IF (x2 > x3) THEN
largest = x2
ELSE
largest = x3
END IF
END IF
PRINT largest
END


Problem 2: Write a program that reads the coefficients a, b and c (a ¹ 0) of the quadratic equation
ax2 +bx + c = 0 and computes its real roots. The roots of the quadratic equation are given by
N.B. When the discriminant (b2 – 4ac) > 0 Þ two real roots are present
When the discriminant (b2 – 4ac) = 0 Þ two identical real roots are present and are equal to (– b /2a)
When the discriminant (b2 – 4ac) < 0 Þ no real roots are present

Algorithm
1. Begin : Declare a, b, c, discrim, root, root1, root2 as SINGLE
2. Read a, b and c.
3. If a = 0 then give the message “Not a quadratic equation” and GOTO statement 2 to read data again.
4. Calculate discriminant from the formula discrim = b^2 – 4*a*c
5. If discrim < 0 then give the message “ No real roots” and END the program here.
6. If discrim = 0 then give the message “Two identical roots” and give the value by using root = – b /(2*a) and end the program here.
7. If discrim > 0 then give the message “Two distinct roots”
root1 = (– b + SQR(discrim) ) / (2*a) root2 = (– b – SQR(discrim) ) / (2*a)
end the program here.

Program coding

DIM a AS SINGLE, b AS SINGLE, c AS SINGLE
DIM discrim AS SINGLE, root AS SINGLE, root1 AS SINGLE, root2 AS SINGLE

10 INPUT a, b, c

REM Checking for A <> 0
IF ( a = 0 ) THEN
PRINT “Not a quadratic equation and A cannot be zero. Input data again.”
GOTO 10 ‘ Line 10 is the input line.
END IF
REM Calculate the discriminant
discrim = b^2 – 4*a*c
REM Discriminant is less than zero
IF discrim < 0 THEN
PRINT “No real root. The program ends here”
GOTO 99 ‘ Line 99 is the last line of this program.
END IF
REM Discriminant is equal to zero
IF discrim = 0 THEN
root = – b / (2 * a)
PRINT “Two identical roots”, root
GOTO 99 ‘ Line 99 is the last line of this program.
END IF
REM Discriminant is greater than zero
IF discrim > 0 THEN
root1= (– b + SQR (discrim) ) / (2 * a)
root2= (– b – SQR (discrim) ) / (2 * a)
PRINT “Two distinct roots :”, “Root 1=”; root1, “Root2=”; root2
END IF
99 END


EXERCISES
1. If I = 2, J = 3, K = 6 then what values do the following expressions have?
(a) I > J AND I * J <= K
(b) I * J >= K AND I > J
(c) NOT I > J AND I * J >= K
(d) I > J OR I * J >= K
(e) I > J AND ( I <= K OR I * J <= K)

2. If I = 1 and J = –1 then what values the following logical expression have?
(a) ( (I > 0 ) AND ( J <> 0 ) AND NOT ( J < 0 )
(b) ( ( I > 0 ) AND NOT ( J <> 0 ) AND ( J < 0 )

3. Write a logical IF statement that prints YES if FOOD is between –1 and 1 (i.e. –1 £ FOOD £ 1 )

4. What will be the final value of NERD at the end of the each program fragment?
(a) NERD = 5 : JOCK = 10
IF (3*NERD < JOCK ) THEN NERD = NERD + 2
NERD = NERD + 3

(b) NERD = 5 : JOCK = 10
IF (2* JOCK <= 3* NERD) THEN GOTO 10
NERD = NERD +1
GOTO 20
10 NERD = JOCK
20 NERF = NERD + JOCK

8. Write a program to convert to Fahrenheit if the temperature input is in Centigrade and convert to Centigrade if the input temperature is in Fahrenheit scale.

9. Write a program to calculate the parts of alcohols (one of higher and other of lower strength) required to prepare a alcohol of desired strength. (Hints. Use aligation method.)

10. Write a program that calculates the Reynold’s number from diameter (DIA), velocity (VEL), density of liquid (DENSITY) and viscosity of the liquid (VISCOSITY) from the formula
REYNUM = (DIA*VEL*DENSITY)/VISCOSITY
If REYNUM £ 2000 give output
“The liquid is flowing in a stream line flow. Reynold’s number is:” REYNUM
If REYNUM ³ 4000 give output
“The liquid is flowing in a turbulent flow. Reynold’s number is:” REYNUM
If 2000 £ REYNUM £ 4000 give output
“The liquid is flowing in transition state. Reynold’s number is:” REYNUM


3. REPETITIVE STRUCTURE

In a repetitive structure a set of statements are executed many times, but that set of statements will appear only once in the program.
Any repetitive structure contains an entry point, a repetition or loop body, and an exit point, as illustrate in the figure:
The number of repetitions in a repetitive structure can be either condition-controlled or counter-controlled.

IF Loop
In the IF loop the number of repetition can be condition controlled.
Program fragment
100 INPUT A, B
SUM = A + B
PRINT A, B, SUM
GOTO 100
STOP
END
In the above program fragment there is no termination point. So this program will iterate (repeat) for an infinite time. To stop this loop a condition may be provided.
For example if the A = 9999 then the program will stop. This A = 9999 is a termination condition.
The same program fragment can be written in the following manner:
100 INPUT A, B
IF ( A = 9999) THEN GOTO 99
SUM = A + B
PRINT A, B, SUM
GOTO 100
99 STOP
END

Counter controlled by IF loop
To control the number of times a loop is executed, a counter may be used whose value is changed by 1 every time then loop is executed. When the counter attains a predetermined value, the loop execution is terminated.
Example:


DIM ICOUNT AS INTEGER
DIM A AS INTEGER, B AS INTEGER
DIM SUM AS INTEGER

100 IF ( ICOUNT > 5 ) THEN GOTO 99
INPUT A, B
SUM = A + B
PRINT A, B, SUM

ICOUNT = ICOUNT + 1
GOTO 100

99 END






Counter controlled by FOR … NEXT loop
QBASIC can handle counter-controlled repetitive structures, or program loops, by a single statement called the FOR .. NEXT loop. It repeats a block of statements a specified number of times.
Syntax
FOR counter = start TO end [STEP increment]
[statementblock]
NEXT [counter [,counter]...]

_ counter A numeric variable used as the loop counter.
_ start and end The initial and final values of the counter.
_ increment The amount the counter is changed each time through the loop.

In the first loop the loop will be executed 15 times.
In the second loop the loop will be executed thrice
1st loop: i = 2
2nd loop: i = 2 + (–3) = – 1
3rd loop: I = – 1 + (– 3) = – 4
Example:
FOR i = 1 TO 15
PRINT i
NEXT i
FOR i = 2 to – 6 STEP – 3
PRINT i
NEXT i
§ Where no increment value is given the increment is taken as 1. [(i.e. FOR i = 1 TO 15 STEP 1 ]
§ When the value of ‘i’ will exceed the end value the loop will stop.
§ Start, end, increment values may be integer or real value or variables.

Nested FOR … NEXT loop
Several FOR .. NEXT loop may be used within the same program. They may follow one another, or they may be nested, i.e. one loop may remain within another.
The inner most loop will be executed first, then the next outer loop, and so on.
Example
DIM PRIN AS INTEGER, RATE AS INTEGER, YEAR AS INTEGER
DIM INTEREST AS SINGLE
FOR PRIN = 1000 TO 10000 STEP 1000
FOR RATE = 10 TO 15
FOR YEAR = 1 TO 3
INTEREST = PRIN * (RATE / 100.) * YEAR Inner Middle Outer most
PRINT PRIN, RATE, YEAR, INTEREST loop loop loop
NEXT YEAR
NEXT RATE
NEXT PRIN
END

EXERCISES

11. Write a program that reads an integer N and prints the sum of the following:
(a) 1 + 2 + 3 + …..+ N
(b) 12 + 22 + 32 + … + N2.
(c) 13 + 23 + 33 + … + N3.
(d) 2 + 4 + 6 + 8 + … + 2N
(e) 1 + 3 + 5 + 7 + … + (2N+1)

Solution of (a)
DIM N AS INTEGER, SUM AS INTEGER
INPUT N
SUM = 0
FOR I = 1 TO N
SUM = SUM + I
NEXT I
PRINT SUM
END


12. Write a program that reads an integer N and computes factorial of N (i.e. N!)
[Hints: PROD = 1*2*3*….* N]
13. To fit a straight line Y = mX + C through a set of n points (x1,y1) (x2, y2), (x3, y3),…,(xn, yn), the following formulae are used to determine the values of m and c :


Write a program that first reads the number of points n, and then reads n pairs of values (xi,yi), and compares m and c.

FUNCTIONS AND SUBROUTINES
The functions and subroutines subprograms with which a program can be broken up into parts.
Function Subprogram
The function subprogram is the method by which a programmer can define function whose value cannot be specified by a single expression. The function will be called from the main program. Only one value will be returned by the function.
Syntax

FUNCTION name [(parameterlist)]
[statementblock
name = expression
[statementblock]
END FUNCTION

_ name The name of the function and the data type it returns, specified by a data-type suffix (%, &, !, #, or $).
_ parameterlist One or more variables that specify parameters to be passed to the function when it is called

Example
Main program PROG25.BAS
Function PROG25.BAS:MAX
DIM X1 AS SINGLE, X2 AS SINGLE
DIM X3 AS SINGLE
INPUT X1, X2, X3
PRINT MAX(X1, X2, X3)
END
FUNCTION MAX (A AS SINGLE, B AS SINGLE, C AS SINGLE)
DIM LARGE AS SINGLE
LARGE = A
IF B > LARGE THEN LARGE = B
IF C > LARGE THEN LARGE = C
MAX = LARGE
END FUNCTION

Subroutine
When more than one value is required to be returned to the main program then subroutine statement is writte.
Syntax
SUB name[(parameterlist)]
[statementblock]
END SUB
_ name The name of the SUB procedure, up to 40 characters long, with no data type suffix.
_ parameterlist One or more variables that specify parameters to be passed to the SUB procedure when it is called:
Main program PROG25.BAS
Function PROG25.BAS:LARGEST
DIM X1 AS SINGLE, X2 AS SINGLE
DIM X3 AS SINGLE, MAX AS SINGLE
INPUT X1, X2, X3
CALL LARGEST (X1, X2, X3, MAX)
PRINT MAX
END
SUB LARGEST (A AS SINGLE, B AS SINGLE, C AS SINGLE, LARGE AS SINGLE)
LARGE = A
IF B > LARGE THEN LARGE = B
IF C > LARGE THEN LARGE = C
END SUB



Example
Write a program that reads a temperature and a letter “C” or “F” and convert it to the other scale.

The main program
REM This program reads a temperature and an alphabet “C” or “F” and
REM then it converts the temperature to the other scale
DIM TEMP AS SINGLE, UNIT AS STRING
DIM CENTIGRADE AS SINGLE, FAHRENHEIT AS SINGLE
INPUT “Give the temperature: ”; TEMP
INPUT “Write C if it is Centigrade or F if it is in Fahrenheit:”; UNIT
IF UNIT = “C” THEN
PRINT TEMP,”deg C is equivalent to “, FAHRENHEIT(TEMP), “deg F”
ENDIF
IF UNIT = “F” THEN
PRINT TEMP “deg F is equivalent to “, CENTIGRADE(TEMP), “deg C”
ENDIF
END

The functions
FUNCTION FAHRENHEIT (X AS SINGLE)
FAHRENHEIT = X*9/5 + 32
END FUNCTION

FUNCTION CENTIGRADE (X AS SINGLE)
CENTIGRADE = (X – 32)*5/9
END FUNCTION

Same program but by using subroutine
The main program************
REM This program reads a temperature and an alphabet “C” or “F” and
REM then it converts the temperature to the other scale
DIM TEMP AS SINGLE, UNIT AS STRING
DIM CENTIGRADE AS SINGLE, FAHRENHEIT AS SINGLE
INPUT “Give the temperature: ”; TEMP
INPUT “Write C if it is in Centigrade or F if it is in Fahrenheit:”; UNIT

CALL CONVERT (TEMP, UNIT, VALUE)

IF UNIT = “C” THEN
PRINT VALUE, “deg F”
ELSE
PRINT VALUE, “deg C”
END IF
END

The sub-routine program*********
SUB CONVERT (TEMP AS SINGLE, UNIT AS STRING, VALUE AS SINGLE)
IF UNIT = “C’ THEN
VALUE = X*9/5 + 32
ELSE
VALUE = (X – 32)*5/9
ENDIF
END SUB
FORMATTING
LOCATE
By using LOCATE statement the display of text can be started from a predefined position on the screen.
LOCATE moves the cursor to a specified position on the screen
LOCATE [row%] [,[column%]
– row% is the row number (maximum 25 rows)
– column% is column number (maximum 80 columns)

Example
CLS
LOCATE 2, 10
PRINT "Enter X value"
LOCATE 2, 25
INPUT "", j
LOCATE 3, 10
PRINT "The value you have entered is "; j

GRAPHICS

SCREEN statement
SCREEN statement sets the screen mode and other characteristics of the screen. The type of graphical display depends on the screen mode. Here we will use screen mode 9. The characteristics of screen mode 9 are as follows:
SCREEN 9:
640 x 350 graphics
80 x 25 text format
16 colors assigned to 4 attributes (64K adapter memory), or
64 colors assigned to 16 attributes (more than 64K adapter memory)

COLOR statement
COLOR sets the screen display colors. It can set the foreground color (i.e. the color of the characters and lines) and (or) the color of background.
Example
SCREEN 9
COLOR 4, 7 ‘ This will set the foreground color in red and background color in white
PRINT "Institute" ‘ Institute will be written in red color against a white background
COLOR 1,7 ‘ This will set the foreground color in blue and background color in white
PRINT "Institute" ‘ Institute will be written in blue color against a white background
COLOR 1,0 'This will set the foreground color in blue and background color in black
PRINT "Institute" ‘ Institute will be written in blue color against a black background

QBASIC color values
Name of the color
QBASIC color values
Name of the color
0
Black
8
Gray
1
Blue
9
Light Blue
2
Green
10
Light Green
3
Cyan
11
Light Cyan
4
Red
12
Light Red
5
Magenta
13
Light Magenta
6
Brown
14
Yellow
7
White
15
High-intensity white

LINE statement
LINE statement draws a line or rectangle on the screen.
Syntax
LINE [(x1!,y1!)] – (x2!,y2!) [,[color%] [,[B BF] [,style%]]]

STEP Specifies that coordinates are relative to the current graphics cursor position.
(x1!,y1!) The screen coordinates of the start of the line and of (x2!,y2!) the end of the line.
color% A color attribute that sets the color of the line or rectangle. The available color attributes
depend on graphics adapter and the screen mode set by the SCREEN statement.
B Draws a rectangle instead of a line.
BF Draws a filled box.
style% A 16-bit value whose bits set whether or not pixels are drawn. Use to draw dashed or dotted
lines.
Examples

Sl
Statement
Comment
Display
1
SCREEN 9
Text mode 25 rows and 80 cols.
Graphics 640 x 350

2
LINE (10, 10)-(100, 100)
‘Draws a line from (10,10) to (100,100)

3
LINE (10, 10)-(100, 100),4
‘Draws a line from (10,10) to (100,100) with red color.
4
LINE (10, 10)-(100, 100),4, B
‘Draws a box from (10,10) to (100,100) with red color.




5
LINE (10, 10)-(100, 100),4, BF
‘Draws a box from (10,10) to (100,100) filled with red color.




6
LINE (10, 10)-(100, 100), , BF
‘Draws a box from (10,10) to (100,100) filled with default color.
7
LINE (10, 10)-(100, 100), 4, B, 1
‘Draws a box from (10,10) to (100,100) with dotted border.




8
LINE (10, 10)-(100, 100), 4, B, 3
‘Draws a box from (10,10) to (100,100) with dashed border.




9
LINE – (150, 150), 4, B
‘Draws a box from previous point (i.e. 100,100) to (150,150).







CIRCLE
Draws a circle or ellipse on the screen.
CIRCLE (x!,y!), radius![,[color%] [,[start!] [,[end!] [,aspect!]]]

STEP Specifies that coordinates are relative to the current graphics cursor position.
(x!,y!) The coordinates for the center of the circle or ellipse.
radius! The radius of the circle or ellipse in the units of the current coordinate system, determined by
the most recent SCREEN, VIEW, and WINDOW statements.
color% A color attribute that sets the circle's color. The available color attributes depend on your
graphics adapter and the screen mode set by the most recent SCREEN statement.
start! The starting angle for the arc, in radians.
end! The ending angle for the arc, in radians.
aspect! The ratio of the length of the y axis to the length of the x axis, used to draw ellipses.

Examples

Sl
Statement
Comment
Display
1
SCREEN 9
Text mode 25 rows and 80 cols.
Graphics 640 x 350

2
CIRCLE (100, 100), 50
‘Draws a circle with center (100,100) and radius of 50 pixels.

3
CIRCLE (100, 100), 50, 4
‘Draws a red circle with center (100,100) and radius of 50 pixels.
4
CIRCLE (100, 100), 50, 4, 0, 1.57
‘Draws a red arc with center (100,100) and radius of 50 pixels starting from 0 radian to p/2 radian

5
CIRCLE (100, 100), 50, 4, , , 0.5
‘Draws a red ellipse with center (100,100) and y axis / x axis ratio of 0.5.


14. 500 mg of a drug is administered orally to a patient. Blood samples were taken from every hour. Concentration of the drug (c) in the plasma is plotted against time (t) to obtain a curve. Write a program to approximate the area under the curve from t =0 to t=24 hours, using the trapezoidal rule.
The area of a trapezium = ½ (a + b)h