Breaking News
You are here: Home / Notes / SEM-1 / CPPM / Concepts Of Arrays

Concepts Of Arrays

An array is a list of variables of the same type

This permits you to represent the list of value in one variable name.

 

A particular quantity is indicated by writing subscript in parenthesis after the variable name. The complete set of quantities is called ARRAY.

 

An individual quantity is called ELEMENT.A subscript is called an INDEX.

 

There are two types of ARRAY:

 

  1. One dimensional ARRAY
  2. Two dimensional ARRAY

 

Explain single and double subscript array with example.

Answer:

Single Subscripted Variable:

A list of quantities can be given one variable name using only one subscript and such a variable is called a single-subscripted variable. A single subscripted variable is also known as a one-dimensional array or variable.

 

A one dimensional array is also known as SINGLE ARRAY or SINGLE SUBBSCRIPTED VARIABLE.

 

An ARRAY is a list of variables of the same type. ARRAYS are useful for organizing multiple variables. To create an ARRAY, use the DIM (DIMENSION) command.

 

 

SYNTAX:    DIM VARIABLE NAME (INTEGER NUMBER)

For Example:

This uses ARRAY called A, which contain 5 variable

 

DIM A(5)

A(1) = 19

A(2) = 17

A(3) = 11

A(4) = 3

A(5) = -5

 

For example, a set of five number, say(19, 17, 11,3,-5) can be described as follows:

 

Array name Cells Quantities
A(1) ß 19
A(2) ß 17
A = A(3) ß 11
A(4) ß   3
A(5) ß -­ 5

 

Example:

 

REM TO DISPLAY EVEN NUMBER

CLS

DIM A(5)

FOR I = 1 TO 5

A(I) = I * 2

NEXT I

FOR I = 1 TO 5

PRINT A(I);

NEXT I

END

 

OUTPUT:

2          4          6          8          10

 

You can also create an array of string.

For Example:

 

A$(1) = “Hello”

N$(2) = “India”

 

Example:

 

REM TO DISPLAY THE NAME OF EMPLOYEE

CLS

DIM A$(5)

FOR I = 1 TO 5

INPUT A$(I)

NEXT I

FOR I = 1 TO 5

PRINT A$(I);

NEXT I

END

 

Double Subscripted Variable:

A variable name in BASIC can have subscript up to three. The use of two subscripts has very wide application in practice, often in manipulation of table and matrices.

DIM D(rowsubscript,columnsubscript)

Consider a table of numbers as shown below:

2 4 8 10
1 3 5 7
3 7 13 17

These values can be stored in three single subscripted variables each representing a row. Instead, a single two-dimension variable could be used. If this is done, and the new variable is named D, then the variable takes the form

D( I , J )

The first subscript I denotes rows and the second subscript J denotes columns. Each value of the table can be described by the appropriate values of subscripts. The table is stored as shown below:

Cells of D Values
Col.1 Col.2 Col.3 Col.4 Col.1 Col.2 Col.3 Col.4
Row 1 1,1 1,2 1,3 1,4 Row 1 2 4 8 10
Row 2 2,1 2,2 2,3 2,4 Row 1 1 3 5 7
Row 3 3,1 3,2 3,3 3,4 Row 1 3 7 13 17

 

To create an array(Single Dimensional array) using DIM Statement for number.

CLS

DIM VAR(5)

VAR(1) = 2

VAR(2) = 4

VAR(3) = 6

VAR(4) = 8

VAR(5) = 10

PRINT VAR(1), VAR(2), VAR(3), VAR(4), VAR(5)

END

 

 

 

To create an array(Single Dimensional array) using DIM Statement for string.

Ans:

CLS

DIM VAR$(5)

VAR$(1) = “Apple”

VAR$(2) = “Mango”

VAR$(3) = “Banana”

VAR$(4) = “Orange”

VAR$(5) = “Graphs”

PRINT VAR$(1), VAR$(2), VAR$(3), VAR$(4), VAR$(5)

END

Download Paper Solutions from Below Link

About admin

Scroll To Top