Arrays
Pankaj Kumar Gupta
Head, BCA Department
Durga Prasad Baljeet Singh (PG) College,
Anoopshahr
District Bulandshahr (Uttar Pradesh)
Abstract:
This Paper
describes full discussion about arrays. All topics about array like: Definition
of an array, Declaration of array, Types of arrays (SDA, DDA, MDA), Memory
allocation, Subscripting, Initialization, Inputting, Outputting etc.
Keywords:
Array, Homogenous, Contiguous,
Linear, Primary, Lower Bound, Upper Bound, SDA, DDA, MDA, Declaration,
Allocation, Initialization, Subscripting, Indexing.
Meaning of array:
According to
Cambridge Dictionary is:
“A large group of
things or people, especially one that is attractive or causes administration or
has been position or has been positioned in a particular way.”
According to
Oxford Dictionary is:
“A group or collection of things or people, often one
that is large or impressive.”
Here in Computer Application we define
Array as: it is a Linear Data Structure. It
is also termed as a collection of homogenous data
elements. All the elements of an array share common name and stored on
contiguous locations. It is also categorized into derived data types.
Example:
1.
A collection of
roll numbers of 50 students (int).
2.
A collection of
marks of 50 students in 6 subjects for a class (float).
3.
A collection of
names of 50 students (char).
Categories of Arrays:
Arrays are categorized into 3 categories
that are defined as follows:
1. Single Dimensional
Arrays.
2. Two Dimensional
Arrays.
3. Multidimensional
Arrays.
1.
Single Dimensional Array (SDA) :
It is also known as
One Dimensional Array (1D). All the elements of this type of Array are stored
in one dimension only.
For example: A collection of Roll Numbers of 50 students.
Declaration of SDA:
Variables of primary data types are declared to use the same. And Arrays
are categorized into secondary data types, variables of array must also be
declared like primary data types. So followings are the syntaxes of array data type.
Syntax:
data_type array_name[size];
For example:
To declare Roll Numbers
of 5 Students (in integer form):
int rn[5];
To declare Marks of
4 Subjects (in float form):
float m[4];
To declare Name of
10 characters (in character form):
char n[10];
Memory Allocation
(SDA):
All the elements of Array (SDA) are stored at contiguous locations in
memory.
Indexing or Subscripting (SDA):
Since all the
elements of array share common name, to recognize the elements of an array has
become the problem. To overcome this problem all the elements uses the indexing
or subscripting. Indexing of SDA
always starts from 0 and goes up to n-1.
Where n is the size of array.
Initialization (SDA):
Boxes created in
the memory (i.e. memory allocation) must be filled with some values. Same
values may be filled with initialization or may be input form user. here
initialization is shown in following diagram:
Inputting (SDA):
If values of SDA
are required to input from user, we may input same from user by following
method:
Algorithm INPUTARRAY(A,N,LB,UB)
/* Here INPUTARRAY
is an algorithm which is used to input all the elements of Array A with the
Capacity of N elements. LB is Lower bound & UB is the Upper bound of Array.
*/
Step-1 Start
Step-2 set K=LB.
Step-3 Repeat
steps 4 and 5 while K<=UB
Step-4 Input
value of A[K].
Step-5 set
K=K+1.
Step-6 Exit.
Outputting:
If values of SDA
stored in the memory are required to show to user, we may output same to user
by following method:
Algorithm OUTPUTARR(A,N,LB,UB)
/* Here OUTPUTARR
is an algorithm which is used to output all the elements of Array A with the
Capacity of N elements. LB is Lower bound & UB is the Upper bound of Array.
*/
Step-1 Start
Step-2 set K=LB.
Step-3 Repeat
steps 4 and 5 while K<=UB
Step-4 print the
value of A[K].
Step-5 set
K=K+1.
Step-6 Exit.
2.
Two Dimensional Array (2DA/DDA):
It is also known as Double Dimensional
Array (DDA). All the elements of this type of Array are stored in two
dimensions (i.e. rows & columns) only.
For example: A collection of marks of 5 students in 3-3 subjects (float) etc.
Declaration of DDA:
Syntax:
datatype arrayname[row][col];
For example:
To declare Marks of 5 Students in 3-3
subjects (in floating point form):
float m[5][3];
Memory Allocation
(DDA):
All the elements of Array (DDA) are stored at contiguous locations in
memory.
float m[5][3];
Indexing or Subscripting (DDA):
Since all the
elements of array share common name, to recognize the elements of an array has
become the problem. To overcome this problem all the elements uses the indexing
or subscripting. Indexing always
starts from [0][0] and goes up to [r-1][c-1]. Where r is the no. of rows and c
is the no. of columns.
Initialization (DDA):
Boxes created in
the memory (i.e. memory allocation) must be filled with some values. Same
values may be filled with initialization or may be input form user. Here
initialization is shown in following diagram:
Inputting (DDA):
If values of DDA
are required to input from user, we may input same from user by following
method:
Algorithm INPUT2DARRAY(A,R,C,LR,UR,LC,UC)
/* Here INPUT2DARRAY
is an algorithm which is used to input all the elements of Array A with the
Capacity of R X C elements. LR is Lower bound of Row & UR is the Upper
bound Row. LC is Lower bound of Column & UC is the Upper bound Column. R is
the No. of Rows & C is the No. of Columns. */
Step-1 Start
Step-2 set K=LR.
Step-3 Repeat
steps 4 to 8 while K<=UR
Step-4 set J=LC.
Step-5 Repeat
steps 6 and 7 while J<=UC
Step-6 Input
value of A[K][J].
Step-7 set J=J+1.
Step-8 set
K=K+1.
Step-9 Exit.
Outputting (DDA):
If values of DDA
stored in the memory are required to show to user, we may output same to user
by following method:
Algorithm
OUTPUT2DARRAY(A,R,C,LR,UR,LC, UC)
/* Here OUTPUT2DARRAY
is an algorithm which is used to input all the elements of Array A with the
Capacity of R X C elements. LR is Lower bound of Row & UR is the Upper
bound Row. LC is Lower bound of Column & UC is the Upper bound Column. R is
the No. of Rows & C is the No. of Columns. */
Step-1 Start
Step-2 set K=LR.
Step-3 Repeat
steps 4 to 8 while K<=UR
Step-4 set J=LC.
Step-5 Repeat
steps 6 and 7 while J<=UC
Step-6 Output
the value of A[K][J].
Step-7 set J=J+1.
Step-8 set
K=K+1.
Step-9 Exit.
3.
Multidimensional Array (MDA):
It may be 3-D, 4-D … and n-D Array. All the
elements of this type of Array are stored in 3-Dimensions, 4-Dimensions, ……….. n-Dimensions. Here we will
consider 3-D Array for an example. Consider A collection of marks of 5 students
in 3-3 subjects for 2 Classes (float) etc.
Declaration of 3D Array:
Syntax:
datatype arrayname[room][row][col];
For example:
To declare Marks of 5 Students in 3-3
subjects for 2 Classes (in floating point form):
float m[2][5][3];
Memory Allocation (3DA):
All the elements of Array (3DA) are stored at contiguous locations in
memory.
Indexing or Subscripting (3DA):
Since all the
elements of array share common name, to recognize the elements of an array has
become the problem. To overcome this problem all the elements uses the indexing
or subscripting. Indexing always
starts from [0][0][0] and goes up to [x-1][y-1][z-1]. Where x is the no. of
rooms, y is the no. of rows and z is the no. of columns.
Initialization (3DA):
Boxes created in
the memory (i.e. memory allocation) must be filled with some values. Same
values may be filled with initialization or may be input form user. Here
initialization is shown in following diagram:
Inputting (3DA):
If values of 3DA
are required to input from user, we may input same from user by following
method:
Algorithm INPUT3DARRAY(A,B,R,C,LB,UB,LR,
UR,LC,UC)
/* Here INPUT3DARRAY
is an algorithm which is used to input all the elements of Array A with the
Capacity of B X R X C elements. LB is Lower bound of Room & UB is the Upper bound Room. LR is Lower bound of
Row & UR is the Upper bound Row. LC is Lower bound of Column & UC is
the Upper bound Column. B is the No. of Rooms, R is the No. of Rows & C is
the No. of Columns. */
Step-1 Start
Step-2 set I=LB.
Step-3 Repeat
steps 4 to 11 while I<=UB
Step-4 set J=LR.
Step-5 Repeat
steps 6 to 10 while J<=UR
Step-6 set K=LC.
Step-7 Repeat
steps 8 and 9 while K<=UC
Step-8 Input
value of A[I][J][K].
Step-9 set K=K+1.
Step-10 set J=J+1.
Step-11 set
K=K+1.
Step-12 Exit.
Outputting (3DA):
If values of 3DA
stored in the memory are required to show to user, we may output same to user
by following method:
Algorithm OUTPUT3DARRAY(A,B,R,C,LB,UB,
LR,UR,LC,UC)
/* Here OUTPUT3DARRAY
is an algorithm which is used to output all the elements of Array A with the
Capacity of B X R X C elements. LB is Lower bound of Room & UB is the Upper bound Room. LR is Lower bound of
Row & UR is the Upper bound Row. LC is Lower bound of Column & UC is
the Upper bound Column. B is the No. of Rooms, R is the No. of Rows & C is
the No. of Columns. */
Step-1 Start
Step-2 set I=LB.
Step-3 Repeat
steps 4 to 11 while I<=UB
Step-4 set J=LR.
Step-5 Repeat
steps 6 to 10 while J<=UR
Step-6 set K=LC.
Step-7 Repeat
steps 8 and 9 while K<=UC
Step-8 Output
the value of A[I][J][K].
Step-9 set K=K+1.
Step-10 set J=J+1.
Step-11 set
K=K+1.
Step-12 Exit.
Conclusion:
An array can also be termed
as a collection of homogeneous values. Anybody can treat an array as a single
object by referring to it through a variable. Variable name is succeeded by
square brackets (that is [ ]
symbols) But you can also treat the components of the
array as if they are themselves variables.
References:
https://dictionary.cambridge.org
https://www.oxfordlearnersdictionaries.com
No comments:
Post a Comment