You are on page 1of 22

DEPARTMENT OF COMPUTER SCIENCE

SCHOOL OF APPLIED SCIENCE


THE FEDERAL POLYTECHNIC ILARO
OGUN STATE
NIGERIA
ICT 102 (QBasic) Lecture Note

Introduction
 Programming is define as an art of writing codes.  
 It simply implies the ability of a programmer to communicate with the computer system in a
particular programming language.
 We can also see as a way by which the computer/programmer instructs the computer system to
carry out a particular task.

 PROGRAMMER: someone that indulges in writing computer codes. His major responsible is
to write programs that performs a given or specified function.
 PROGRAMMER someone that is an expert in writing computer codes or instruction to the
computer to perform a given task.
 PROGRAM: It is instruction, which the computer takes in and act on it.
 PROGRAMMING LANGUAGE: the language of choice for which the programmer
communicates with the computer machine. In other words, this points to the language with
which he codes.

LANGUAGES
 HIGH LEVEL LANGUAGE (HLL): This language is close to normal human English
Language. It is not a language the computer understands, rather it needs the help of an
interpreter, or a compiler before computer could understand it. Examples here includes QBasic
(Quick – Beginners’ All Purpose Symbolic and Instruction Code), Fortran (Formula translator),
Pascal, Java, C, C++, Python, Ada, Eiffel etc.
 LOW LEVEL LANGAUGE (LLL): This language is very much close to machine language. It
needs little translator to real machine language. It is regard as SYMBOLIC Language. A good
and common example is the ASSEMBLY Language.
 MACHINE LANGUAGE (ML): This is the very language that the computer understands. It is
called the language of 0s and 1s.
 For the purpose of this course, High Level Language shall be the language of usage and QBasic
specifically the programming language of choice.

The Structure of QBasic


QBasic is a procedural language. Unlike object-oriented language, it has no standard structure
according to programming style rule. Howbeit, it does have structure for programmers to
follow.
Guide to QBasic Structure
i. Line Number: Programs in QBasic are easily to write, with line numbers for readability
purpose.
ii. CLS: This means Clear Screen
iii. REM: Implies Remark – Comment.
iv. END: To signify the end of a program

For example, the program below does nothing but shows the guide to QBasic Structure:
5 CLS
10 REM
15 END
OR
2 CLS
4 REM
6 END

To feed/supply data unto the computer system using QBasic, we make use of any of the following two
commands:
i. INPUT statement
ii. READ and DATA statement
To fetch/retrieve stored or inputted data from the computer system using QBasic, we make use of
PRINT statement.

OUTPUT STATEMENTS
These are statements that are used for fetch/retrieve/show to screen, inputted or stored data from the
computer system.
• The major/basic output statement used in QBasic is call PRINT. Others will be treated under
FILE.
OUTPUT STATEMENTS – Usage & Format
PRINT can be used in two ways:
i. For outputting literals e.g. PRINT “I will surely pass this course ”
ii. For outputting variables (values of an identifier) e.g.
PRINT radius
(Where radius is a variable having a particular value that has previously been inputted
/captured / stored)
ILLUSTRATION OUTPUT STATEMENTS

5 CLS
10 REM
15 PRINT “I must pass this course”
20 END
Press F5 to RUN
OR
Click on RUN and click on START
RUN well? Good! Save your work now!! Click on FILE, then click on SAVE. Remove the
highlighting and save accordingly. Please ensure you preserve the .bas, it is the filename extension for
QBasic, just as .doc is to MS-Word.
See the output below:

RUN well? Good! Press any KEY to return to the Coding Screen. Save your work now!! Click on
FILE, then click on SAVE. Remove the highlighting and save accordingly. Please ensure you preserve
the .bas, it is the filename extension for QBasic, just as .doc is to MS-Word.

Example

5 CLS
10 REM
15 INPUT meter
20 PRINT meter
25 PRINT meter * 5
30 END
Press F5 to RUN
OR
Click on RUN and click on START.

You will first see something like this, prompting you to input/supply value for radius.
Input your radius to be 5 and press enter you will get something like

Class Exercise 1

Code in QBasic to output the literals:


BIO-DATA Quiz
HE was born and brought up in Lagos.
He hails from Kwara.
He settles down in Ondo.
Who is He?

Solution I

5 CLS
10 REM
15 Print “BIO-DATA Quiz”
20 Print “_____________”
25 Print “He was born and brought up in Lagos.”
30 Print “He hails from Kwara.”
35 Print “He settles down in Ondo.”
40 Print
45 Print “Who is He?”
50 END

Something is missing

Class Exercise II

Given two variables: birthyear and currentyear, input values for the two variables and compute your
AGE by finding the difference of the two variables.

Solution II
5 CLS
10 REM
15 INPUT birthyear
20 INPUT currentyear
25 PRINT currentyear-birthyear
30 END

INPUT STATEMENTS

These are statements that are used for feed/supply data unto the computer system.
• Two Types:
• Input Method
• Read & Data Method

INPUT Method
This is used where the program given is a closed problem (e.g. values not specified).
e.g. INPUT A
OR
INPUT “Enter value for A: ”, A
Here the value of A is expected to be inputted.

Variables
A variable, simply defined, is a name, which can contain a value. Programming involves giving values
to these names and presenting them in some form to the user. A variable has a type, which is define by
the kind of value it holds. If the variable holds a number, it may be of integer, floating decimal, long
integer, or imaginary. If the variable holds symbols or text, it may be a character variable or a string
variable. You will become accustomed as you continue programming to these terms.

Here are some examples of values a variable might contain:

STRING "hello, this is a string"


INTEGER 5
LONG 92883
SINGLE 39.2932
DOUBLE 983288.18

The first is a string. Strings contain text. The last four are number types. Nevertheless, the computer
does not know what kind of value you are trying to give a variable unless you tell it! There are two
methods of telling the computer what kind of variable you are using:

1. Explicitly declare the variable AS a type. This is done by using the DIM statement. Say you wanted
to make a variable called number, which would contain an integer (whole number, no digits after the
decimal point). You would do it like this:

DIM number AS INTEGER


Then you would use that variable as an integer. The word DIM actually originates from the word
Dimension, but you will not see why until we discuss the topic of arrays.

2. Put a symbol after the variable name, which is defined as representing that type. QBasic has a set of
symbols, which represent each variable type:
$ String
% Integer
& Long
! Single
# Double
Appending one of these symbols to the name of a variable when you use it in the program tells the
computer that you are using it as that type. This topic of data types is actually a difficult concept to
grasp for newcomers to programming. The most common error in QBasic is the infamous Type
Mismatch, which you will see a lot. This means that you are trying to put a value into a variable of the
wrong type. You might be trying to put the letters "hi there" into an integer variable. If you do not
define the type of the variable, then QBasic assumes it is of the Single type, which can often yield
unexpected results. I personally prefer to use the type symbols after variable names, but some
explicitly declare them usually at the head of their programs.

STRING VARIABLES
These are alphanumeric storage locations. In other words, they are storage locations that hold
alphanumeric data. Alphanumeric data implies data that consist of a combination of both alphabetic
and numeric data. For instance, consider a variable called name, holding data like john NAJIMU – this
implies purely an ALPHABETIC DATA/VARIABLE. Another instance is a variable called score,
holding data like 100 – this is refer to as NUMERIC DATA/VARIABLE. Now, when you have a
variable called address, containing data like 12, ikosi Street or user name like samrt100 (i.e. a
combination of both alphabetic and numeric data) – this is called ALPHANUMERIC (Alpha-Numeric:
Alphabetic and Numeric) DATA/VARIABLE. It should be mentioned however that all ALPHABETIC
VARIABLES are also taken as ALPHANUMERIC VARIABLES; e.g. course title – ‘INTRO TO
COMPUTER PROGRAMMING’.However for numeric variable, it has to be explicitly stated such
variables have to be declared as string for the Qbasic interpreter to recognise/take them as String
Variables. For instance the variable Age can be a numeric variable if containing data such as 23, 56, 78
– purely numeric. But this can be taken as string variable if so declared such that it now contain data
such as 23years, 56years OR even 23, 45 – purely numeric but with a clause that arithmetic
computation cannot be carried out on it unlike in the case of numeric variable/data.

DECLARING STRING VARIABLES

• To declare a variable to be a string variable, two things must be done:


• Declare the variable as STRING using the DIM statement
• E.g. DIM name AS String
• Accept data unto the string variable by appending the dollar sign ($) unto the
string variable name.
e.g.
5 CLS
10 REM
15 DIM surname AS String
20 INPUT surname$
25 END

INPUT AND OUTPUT STRING VARIABLES


INPUTTING STRING VARIABLES
i. This has already been slightly demonstrated in the previous slide.
ii. To input data/values unto string variable, simply append ($) at the end of the string variable
name.
iii. The inputting process is then performed as in inputting of numeric variables.
iv. The only caution/prerequisite to this is just that the variable concerned must have been
previously declared (DIM) as string.

ILLUSTRATION – Write a Qbasic program that captures your bio-data (name, age, sex,
address, occupation, and hobby)
5 CLS
10 REM
15 DIM sname AS String
20 DIM age AS String
25 DIM sex AS String
30 DIM address AS String
35 DIM occupation AS String
40 DIM hobby AS String
45 INPUT “Enter your name: ”, sname$
50 INPUT “Enter your age: ”, age$
55 INPUT “Enter your gender: ”, sex$
60 INPUT “Enter your home address: ”, address$
65 INPUT “Enter your profession: ”, occupation$
70 INPUT “Enter your hobby: ”, hobby$
75 END

Output
OUTPUTTING STRING VARIABLES
• Like in NUMERIC processing, we make use of the PRINT Statement for outputting STRING
Variables’ data/values.
• E.g. PRINT sname$
• OR Print “My name is: ”, sname$
• Let’s move to the next slide and modify/extend the earlier INPUT program on STRING
Variable (remember, the program stops/ends at Line No 75).
• Note that END will no more occupy Line 75 but to create a demarcation between the input
screen and the output statement, we insert the CLS statement.

• ILLUSTRATION – Write a Qbasic program that captures your bio-data (name, age, sex,
address, occupation, and hobby)
5 CLS
10 REM
15 DIM sname AS String
20 DIM age AS String
25 DIM sex AS String
30 DIM address AS String
35 DIM occupation AS String
40 DIM hobby AS String
45 INPUT “Enter your name: ”, sname$
50 INPUT “Enter your age: ”, age$
55 INPUT “Enter your gender: ”, sex$
60 INPUT “Enter your home address: ”, address$
65 INPUT “Enter your profession: ”, occupation$
70 INPUT “Enter your hobby: ”, hobby$
75 CLS
80 PRINT “SURNAME: ”, sname$
85 PRINT “AGE: ”, age$
90 PRINT “GENDER: ”, sex$
95 PRINT “ADDRESS: ”, address$
100 PRINT “PROFESSION: ”, occupation$
105 PRINT “HOBBY: ”, hobby$
110 END
After responding to the input prompts and pressing any key to continue…here comes the output…

To carry out computations on string variables, the following commands are used:
i. Left$: This is used to filter and output the leftmost part of a string depending on the specified
number of character, starting from the LEFT.
ii. Right$: This is used to filter and output the rightmost part of a string depending on the
specified number of character, starting from the RIGHT.
iii. Mid$: This is used to filter and output the middle part of a string depending on the specified
number of character and starting pint/location.
iv. Concat: This is used to concatenate/merge two or more string variables together.

(i) Left$
Format
Left$(the string variable, start point)
Meaning
Take the specified string variable, start counting from the LEFT position of the
string/string variable and output based on the number of character specified.
Example
Let mymy = “Dosage”
Specifying Left$(mymy, 3) will output “Dos”
(ii) Right$
Format
Right$(the string variable, start point)
Meaning
Take the specified string variable, start counting from the RIGHT position of the
string/string variable and output based on the number of character specified.
Example
Let mymy = “Dosage”
Specifying Right$(mymy, 3) will output “age”

(iii) Mid$
Format
Mid$(the string variable, start point, end point)
Meaning
Take the specified string variable, start counting from the specified STARTING
POINT, use the number specified in the END POINT to determine the number of
character to output.
Example
Let mymy = “Dosage”
Specifying Mid$(mymy, 3, 4) will place the counting string on‘s’ and counted four (4)
characters, producing/outputting “sage”.

i. Left$
Programmatically, we can have it as:
5 CLS
10 REM
15 Let mymy$ = “Dosage”
20 Print Left$(mymy$, 3)
25 Print Right$(mymy$, 3)
30 Print Mid$(mymy$, 3, 4)
25 END

…and the output is:

• MORE ILLUSTRATION…
Given:
name1$ = “Ajasin”
name2$ = “Adekunle”
Compute:
• Left$(3) of name1$
• Right$(3) of name1$
• Concat(name2$ and name1$)
• Mid$(name1$,1,3) concat Mid$(name2$,4,5)

• ILLUSTRATION
Output:
• Left$(3) of name1$
Will give
Aja
• Right$(3) of name1$
Will give
sin
• Concat(name2$ and name1$)
• Mid$(name1$,1,3) concat Mid$(name2$,4,5)
What will these two give? Turn to the codes:

• CODE ILLUSTRATION
5 CLS
10 REM
15 LET name1$ = "Ajasin"
20 LET name2$ = "Adekunle"
25 PRINT "Name 1 is: ", name1$
30 PRINT "Name 2 is: ", name2$
35 PRINT "Left to 3xters is: ", LEFT$(name1$, 3)
40 PRINT "Right to 3xters is: ", RIGHT$(name1$, 3)
45 PRINT "Merging of Name2 and Name1: ", name2$ + name1$
50 REM To leave space in between
55 PRINT "To leave space in between Name1 and Name2"
60 PRINT "Merging of Name2 and Name1 with space: ", name2$ + " " + name1$
65 PRINT "Middle of Name1(1,3) and Middle of Name2(4,5): ", MID$(name1$, 1, 3) + MID$
(name2$, 4, 5)
70 END

The output looks thus:

OTHER COMPUTATIONS ON STRING VARIABLES

• Others include:
i. Len : This is used to compute the number of characters that a string/string variable is made up
of. LEN(String/String Variable).
ii. UCase$: This is used to convert lower case string to upper. UCASE$(String/String Variable).
iii. LCase$: This is used to convert an upper case string to lower. LCASE$(String/String Variable).

• CODE ILLUSTRATION
5 CLS
10 REM
15 LET name1$ = “ajasin"
20 LET name2$ = “ADEKUNLE"
25 PRINT “Name 1 is: ", name1$
30 PRINT “Name 2 is: ", name2$
35 PRINT “Length of Name1 is: ”, len(name1$)
40 PRINT “Length of Name2 is: ”, len(name2$)
45 PRINT “Conversion of Name1 to Upper Case: ”, Ucase$(name1$)
50 PRINT “Conversion of Name2 to Lower Case: ”, Lcase$(name2$)
55 END
• The output looks thus:

The IF and THEN commands

The IF and THEN commands are used to compare an expression and then perform some task based on
that expression.

x = 5
IF x = 5 THEN PRINT "x equals 5"
Since X does equal 5 in this case, the program outputs:
x equals 5

Expression signs
You can also enter the following statements, instead of the equals sign:
x < 5  (x is less than 5)

x > 5  (x is greater than 5)

Run the following:

x = 16
IF (x > 5) THEN PRINT "x is greater than 5"
Output:
x is greater than 5

You can also combine the signs like this:


x <= 5  (x is less than or equal to 5)
x >= 5  (x is greater than or equal to 5)
x <> 5  (x does not equal 5)

Run the following example:


CLS
x = 5
IF (x >= 5) THEN PRINT "x is greater than or equal to 5"
IF (x <= 5) THEN PRINT "x is less than or equal to 5"
IF (x <> 5) THEN PRINT "x does not equal 5"
Output:
x is greater than or equal to 5
x is less than or equal to 5

ELSE
Using the ELSE command, you can have the program perform a different action if the statement is
false.
x = 3
IF x = 5 THEN PRINT "Yes" ELSE PRINT "No"
Since X doesn't equal 5, the output is:
No

END IF

END IF allows you to have multiple commands after the IF...THEN statement, but they must start on
the line after the IF statement. END IF should appear right after the list of commands.
x = 5
IF (x = 5) THEN
INPUT a$
PRINT a$

END IF

The following program uses ELSE with the END IF command:


x = 16
IF (x = 5) THEN
INPUT a$ TIP: There is a way to have multiple commands after IF...THEN
PRINT a$ without using END IF. To do so, place a colon between each
ELSE command.
PRINT x * 2
IF (x = 5) THEN INPUT a$: PRINT a$
END IF
Output:
32

ELSEIF
The ELSEIF command allows you to perform a secondary action if the first expression was false.
Unlike ELSE, this task is only performed if a specified statement is true.
x = 6
IF (x = 5) THEN
PRINT "Statement 1 is true"

ELSEIF (x = 6) THEN
PRINT "Statement 2 is true"
END IF
Output:
Statement 2 is true

You can have multiple ELSEIF commands, along with ELSE.


x = 8
IF (x = 5) THEN
PRINT "Statement 1 is true"

ELSEIF (x = 6) THEN
PRINT "Statement 2 is true"

ELSEIF (x = 7) THEN
PRINT "Statement 3 is true"

ELSE
PRINT "No above statements are true"

END IF
Output:
No above statements are true

Multiple expressions
You can have more than one expression in IF...THEN by using either the OR operator or the AND
operator.

The OR operator only requires one expression to be true in order to print "Yes" in the following
program:
x = 20
IF (x = 5 OR x = 20) THEN PRINT "Yes"
Output:
Yes

The AND operator requires both expressions to be true.


x = 7
IF (x > 5 AND x < 10) THEN PRINT "True"
Output:
True

This is a slightly more complex example:


x = 16
y = 3
IF ((x > 5 AND x < 10) OR y = 3) THEN PRINT "Correct"
Output (since Y is 3):
Correct

Strings in IF...THEN

So far in this chapter, we've only been dealing with numbers, but you can also use strings with the
IF...THEN command.
x$ = "Hello"

IF (x$ = "Hello" OR x$ = "World") THEN PRINT x$
Output:
Hello
You can also compare two variable strings:
x$ = "Hello"
y$ = "World"
IF (x$ <> y$) THEN PRINT x$; " "; y$
Output:
Hello World

Labels and the GOTO and GOSUB commands

The GOTO and GOSUB commands enables you to jump to certain positions in your program. Labels
are used to specify what point in the program to continue execution.

GOTO

To use GOTO, place a label somewhere in your program, and then enter.
GOTO <label>

Run the following example program:


TIP: TheLabel can be placed on the same line as
PRINT "1" PRINT "3"
GOTO TheLabel
PRINT "2" TheLabel: PRINT "3"
TheLabel:
PRINT "3"
Output (notice how PRINT "2" is skipped):
1
3

GOSUB
The GOSUB command is the same as GOTO, except when it encounters a RETURN statement, the
program "returns" back to the GOSUB command. In other words, RETURN continues program
execution immediately after the previous GOSUB statement.

PRINT "1" Since the program returns to the GOSUB


GOSUB TheLabel command, the number 2 is printed this time.
PRINT "2" 1
END 3
2
TheLabel:
PRINT "3"
RETURN
(Note: The END command exits the program.)
Line numbers

"Line numbers" can be used as labels.


PRINT "1"
GOTO 10
PRINT "2"
10 PRINT "3"  (Notice the line number)

You can also write the program like this:


10 PRINT "1"
20 GOTO 40
30 PRINT "2"
40 PRINT "3"

The line numbers don't even have to be in sequence.


17 PRINT "1"
2 GOTO 160
701 PRINT "2"
160 PRINT "3"

Each of these programs output:


1
3

Guessing game

The following is a simple guessing game:


CLS
start:
PRINT "Guess a number between 1 and 10: ";
INPUT num
IF (num < 1 OR num > 10) THEN
PRINT "That is not between 1 and 10"
GOTO start
END IF
IF (num = 6) THEN
PRINT "Correct!!!"
ELSE
PRINT "Try again"
PRINT
GOTO start
END IF
Output (may be slightly different):
Guess a number between 1 and 10: ? 2
Try again TIP: Notice the second PRINT statement
under PRINT "Try again". It adds a blank
line under Try again when the program is
Guess a number between 1 and 10: ? 7 executed.
Try again
Guess a number between 1 and 10: ? 6
Correct!!!

Loops
"Loops" make it easier to do an action multiple times. There are at least four types of loops:
IF...GOTO, WHILE...WEND, DO...LOOP, and FOR...NEXT.
IF...GOTO
This program uses IF...GOTO to create a loop:

x = 10 Output:
10
start: 11
PRINT x 12
13
x = x + 1  (This adds 1 to x) 14

IF x < 15 THEN GOTO start

WHILE...WEND

The WHILE...WEND commands continue a loop until a specified expression is false.

To use WHILE...WEND:

1. Place an expression after WHILE


2. Enter a list of commands
3. Place WEND at the end

Run the following:

x = 10 Output (same as in previous example):


WHILE x < 15 10
PRINT x 11
x = x + 1 12
WEND 13
14

DO...LOOP

DO...LOOP is exactly the same as WHILE...WEND, except it has at least two slight advantages. With
DO...LOOP you can:

1. Loop until an expression is true

2. Loop at least one time regardless of whether the expression is true or not.
To use DO...LOOP:

1. Specify whether the loop continues "while" the expression is true or "until" the expression is
true, using the WHILE and UNTIL statements, respectively.

2. Place an expression after WHILE/UNTIL

3. Enter a list of commands

4. Place LOOP at the end

The following uses the WHILE statement:


x = 10

DO WHILE x < 15

PRINT x

x = x + 1

LOOP

This program uses the UNTIL statement:


x = 10

DO UNTIL x = 15

PRINT x

x = x + 1

LOOP

They both output:


10
11
12
13
14

If you place the expression at the end of the loop instead, the program goes through the loop at least
once.
x = 32

DO

PRINT x

x = x + 1
LOOP WHILE x < 5
This is the output because the loop was only gone through one time:
32

FOR...NEXT

FOR...NEXT provides an easier way to create a loop.

FOR x = 1 TO 5
PRINT x
NEXT x
Output:
TIP: The X after NEXT is optional (unless you have a
1 loop within a loop).
2
3
4
5
Also, you can use the STEP attribute to specify how much X will be increased each time through the
loop.
FOR x = 1 TO 5 STEP 2

PRINT x

NEXT x
Output:
1
3
5
STOPPING LOOPS
To stop a loop prematurely, use the EXIT command, followed by either FOR or DO.
FOR x = 1 TO 5

PRINT x

IF (x = 3) THEN EXIT FOR

NEXT x
Output:
1
2
3
(NOTE: This command only works with the DO...LOOP and FOR...NEXT commands, not with
WHILE...WEND or IF...GOTO.)
ARRAY

• DEFINITION
• DIVISIONS
• DECLARATIONS
• DEMONSTRATIONS

DEFINITIONS

An array is a collection of data element, captured with the same name (storage location name)
and having the same data type.
The nature of data stored in an array depicts the type of array been created (i.e. numeric array,
string array etc.)

PICTORIAL REPRESENTATION

NUMERIC / INTEGER ARRAY

STRING ARRAY

DIVISIONS
We have two divisions of array:
 One Dimensional Array OR Linear Array
 Multidimensional Array OR Matrix

An array that consist of a one-columnar set of data.

FORMAT
To declare a linear array, we have
DIM array_name(size of the array) as INTEGER
e.g.
DIM A(10) as INTEGER

Illustration: A Program to Capture 10 elements into an integer array,


named A
5 CLS
10 REM
15 DIM A(10) AS INTEGER
20 FOR I = 1 TO 10
25 PRINT “Enter element ”; I; “of the array: ”;
30 INPUT A(I)
35 NEXT I
40 END

Illustration II: Consider a company with the sales data for the year given as
shown below (data in millions):

{4, 1, 3, 5, 3, 7, 8, 2, 7, 4, 5, 6}

Code in QBasic to
(i) capture the sales data
(ii) sum up the sales for the year

Solution:
5 CLS
10 REM
15 DIM sales(12) as INTEGER
20 sumsales = 0
25 FOR I = 1 TO 12
30 PRINT “Sales ”; I;
35 INPUT sales(I)
40 sumsales = sumsales + sales(I)
45 NEXT I
50 Print “The total sales is: ”; sumsales
55 END

Illustration III: I have two companies with the sales data for the year given
as shown below (data in millions):

COY A= {4, 1, 3, 5, 3, 7, 8, 2, 7, 4, 5, 6}
COY B= {7, 6, 4, 2, 1, 8, 9, 4, 6, 5, 6, 7}

Code in QBasic to
(i) capture the sales data for each company
(ii) sum up the sales for the year for each company
(iii) determine which company has the highest sales and report
accordingly.

Solution:

5 CLS
10 REM
15 DIM salescoy1(12) as INTEGER
20 DIM salescoy2(12) as INTEGER
25 sumsalescoy1 = 0
30 sumsalescoy2 = 0
35 PRINT “Processing for Company 1”
40 FOR I = 1 TO 12
45 PRINT “Sales ”; I;
50 INPUT salescoy1(I)
55 sumsalescoy1 = sumsalescoy1 + salescoy1(I)
60 NEXT I
65 PRINT “Processing for Company 2”
70 FOR I = 1 TO 12
75 PRINT “Sales ”; I;
80 INPUT salescoy2(I)
85 sumsalescoy2 = sumsalescoy2 + salescoy2(I)
90 NEXT I
95 Print “The total sales for COY1 is: ”; sumsalescoy1
100 Print “The total sales for COY2 is: ”; sumsalescoy2
105 If sumsalescoy1>sumsalescoy2 Then PRINT “Company1 has the
highest sales”
Else PRINT “Company2 has the highest sales”
110 END

HOME TAKE 4

1 Discuss the concept of ‘Array’ in Programming.


2 Given myArray having the elements:
{4, 6, 2, 1, 3, 7, 5, 3, 0, 3}
Code in QBasic to:
 captures the elements of the array
 computes the sum of the array elements
 computes the sum-products of the array elements

3 Otedola Group has two subsidiaries: OteWealth and OteFame. The monthly sales data
for the two subsidiaries for the year ended 2016 are as shown below (data in millions):
OteWealth = {4, 7, 4, 2, 1, 7, 8, 9, 6, 7, 8, 7}
OteFame = {5, 6, 4, 6, 5, 8, 8, 9, 8, 9, 8, 6}
Code in QBasic to:
 capture the sales data for each subsidiary
 sum up the dales for the year for each subsidiary
 determine which subsidiary has the highest sales and report accordingly

Multidimensional Array OR Matrix

You might also like