Arrays
Arrays
Definition
•Array is a collection of variables of same type all of which are referred by a common name.
•Specific element of an array is accessed by index.
•A reference to array element in a program often include one or more subscript depending upon
what is the dimension of an array.
•The elements of an array may be primitive type or user defined type,but all of must be of same
type.
•The most common form of array is :One dimensional and two dimensional .
•The name for one dimensional array is vector
•Two dimensional array are known as matrix.
One dimensional Array
1-D Array can be declared as
int arr[5];
We are declaring an array named as arr and which can store 5 integer elements.
Size is specified in [ ] square brackets.Size must be given when array is declared
Each integer element takes 2 bytes so this array declaration reserves 10 bytes of memory for the array
when array elements are initialized.
All elements of an array are stored in contiguous location with the first element accessed as
arrayname[0],second as arrayname[1] and so on.In this case it will be arr[0],arr[2] upto arr[4].The first
element of an array start at index 0 and last element at index arraysize-1.
In fortan Lower bound is 1 and upper bound is 5 in this example.There may be other values of upper
bound and lower bound.
1 –Dimensional Array
Number of elements in array is given by the following formula:-
N=UB-LB+1
For example in C int arr[10],number of elements in array will be
N=9-0+1=10
Specific elements of the array are referenced by means of a atwo level syntactic mechanism
,where the first part is the name of array and second part consisiting of one or more items
known as indexing subscripts.For example in C arr[4] refers the 5th element of array named arr.
Short program of array in C
printf("\nenter a[%d] element :",i);
scanf("%d",&a[i]);
#include<stdio.h>
}
#include<conio.h>
#define s 5 printf("array elements are \n");
main() for(i=0;i<s;i++)
{ printf("a[%d]=%d\n",i,a[i]);
int a[5]; getch();
int i;
}
clrscr();
for(i=0;i<s;i++)
{
Initializing a 1-D Array
The elements of array can be initialized as:-
int a[ ]={34,56,78,89,67}
We initialize the array within opening and closing braces{ } and separate the element by
comma.The size of array in subscript operator is optional.The size of array can be calculated as:-
const int 5=sizeof(a)/sizeof(int)
Size of array a will be 10 as total 5 elements are there in array and array is of int type and size of
int is 2 in turbo C so S=10/2=5
Array boundary is not checked while accessing an element of using subscript notation i.e if our
array is declared as int [5] and we try to access an element beyond 0 and 4 then it will not check
either at compile time or run time.This sometimes may result in crashing the program.
Traversing Linear Array
Let A be a collection of data elements stored in the memory of computer.Suppose we want to
print the contents of each element of A or suppose we want to count the number of elements
with a given property.This can be accomplished by traversing A.
Algorithm for traversing a Linear array
Here LA is a linear array with lower bound LB and upper bound UB.This algorithm traverses LA
applying an operation PROCESS to each element of LA. LA
(1) [Initialize counter] Set K :=LB.
23 0
(2) Repeat Steps 3 and 4 while K<=UB.
34 1
(3) [visit element.]Apply PROCESS to LA[K].
2
(4) [Increase counter.]Set K:=k+1. 12
{
for (c = n - 1; c >= position - 1; c--)
return 0;
printf("Enter the location where you wish to insert an element\n");
scanf("%d", &position); }
C Program for deleting an element in
array
#include <stdio.h> scanf("%d", &position);
int main() if (position >= n+1)
{ printf("Deletion not possible.\n");
int array[100], position, c, n; else
{
printf("Enter number of elements in array\n"); for (c = position - 1; c < n - 1; c++)
scanf("%d", &n); array[c] = array[c+1];
Array of an element of an array say “A[ I ]” is calculated using the following formula:
Address of A [ I ] = B + W * ( I – LB )
Where,
B = Base address
W = Storage Size of one element stored in the array (in byte)
I = Subscript of element whose address is to be found
LB = Lower limit / Lower Bound of subscript, if not specified assume 0 (zero)
Numerical based on 1-d Array
Example:
Given the base address of an array B[1300…..1900] as 1020 and size of each element
is 2 bytes in the memory. Find the address of B[1700].
Solution:
The given values are: B = 1020, LB = 1300, W = 2, I = 1700
Address of A [ I ] = B + W * ( I – LB )
=1020+2*(1700–1300)
=1020+2*400
=1020+800
= 1820 [Ans]
Numerical:-
Q)Consider the linear arrays AAA(5:50),BBB(-5:10) and CCC(18).
(a)Find the number of elements in each array.
(b)Suppose Base(AAA)=300 and w=4 words per memory cell for AAA.Find the address of
AAA[15],AAA[35] and AAA[55].
(2,1) Column 1
(3,1)
(1,2)
(2,2) Column 2
Address of A [ I ][ J ] Column Major
Wise = B + W * [( I – Lr ) + M * ( J – (3,2)
Lc )]
(1,3)
(2,3) Column 3
(3,3)
(1,4)
(2,4) Column 4
(3,4)
Row Major Order
(1,1)
Address of A [ I ][ J ] = B + W * [
(1,2)
N * ( I – Lr ) + ( J – Lc ) ]
B = Base address (1,3) Row 1
I = Row subscript of element whose (1,4)
address is to be found
J = Column subscript of element (2,1)
whose address is to be found (2,2)
W = Storage Size of one element (2,3) Row 2
stored in the array (in byte)
Lr = Lower limit of row/start row index (2,4)
of matrix (3,1)
Lc = Lower limit of column/start
column index of matrix (3,2) Row 3
M = Number of row of the given matrix (3,3)
N = Number of column of the given
matrix
(3,4)
Q ) Consider the 25*4 matrix array SCORE.Suppose Base (SCORE) =200 and there are w=4
words per memory cell.Furthermore,Suppose the programming language stores two
dimensional arrays using row major –order.Then the address of SCORE[12,3],the third test of
twelth student ,follows:
LOC(SCORE[12,3])=200+4[4(12-1)+(3-1)]=200+4[46]=384
L.r and Lc =1
Important : Usually number of rows and columns of a matrix are given ( like A[20][30] or A[40][60] )
but if it is given as A[Lr- – – – – Ur, Lc- – – – – Uc].
In this case number of rows and columns are calculated using the following methods:
Number of rows (M) will be calculated as = (Ur – Lr) + 1
Number of columns (N) will be calculated as = (Uc – Lc) + 1
And rest of the process will remain same as per requirement (Row Major Wise or Column Major Wise).
Q). An array X [-15……….10, 15……………40] requires one byte of storage. If beginning
location is 1500 determine the location of X [15][20].
Solution:
As you see here the number of rows and columns are not given in the question. So they are
calculated as:
Number or rows say M = (Ur – Lr) + 1 = [10 – (- 15)] +1 = 26
Number or columns say N = (Uc – Lc) + 1 = [40 – 15)] +1 = 26
(i) Column Major Wise Calculation of above equation
The given values are: B = 1500, W = 1 byte, I = 15, J = 20, Lr = -15, Lc = 15, M = 26
Address of A [ I ][ J ] = B + W * [ ( I – Lr ) + M * ( J – Lc ) ]
= 1500 + 1 * [(15 – (-15)) + 26 * (20 – 15)] = 1500 + 1 * [30 + 26 * 5] = 1500 + 1 * [160] =
1660 [Ans]
(ii) Row Major Wise Calculation of above equation
The given values are: B = 1500, W = 1 byte, I = 15, J = 20, Lr = -15, Lc = 15, N = 26
Address of A [ I ][ J ] = B + W * [ N * ( I – Lr ) + ( J – Lc ) ]
= 1500 + 1* [26 * (15 – (-15))) + (20 – 15)] = 1500 + 1 * [26 * 30 + 5] = 1500 + 1 * [780 + 5] = 1500
+ 785
= 2285 [Ans]
In case array start at index 0:-
(i)For row major order
Address (arr[i][j])=arr+ S(N*i+j)
(ii)For column major order
Address(arr[i][j])=arr+ S(M*j+i)
In case array start at index 1
(i)For row major order
Address(arr[i][j])=arr+S(N*(i-1)+(j-1))
(ii) For column major order
Address(arr[i][j]=arr+S(M*(j-1)+(i-1))
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[3][4]={1,2,3,5,4,3,6,7,2,3,4,0};
int i,j;
printf("Array elements in row major order\n");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
printf("%d",arr[i][j]);
printf("\n");
}
printf("Array elements in column major order\n");
for(j=0;j<4;j++)
{
for(i=0;i<3;i++)
printf("%d",arr[i][j]);
printf("\n");
}
getch();
}
Output:-
Array elements in row major order
1235
4367
2340
Array elements in column major order
142
233
364
570
Multidimensional Array
A 2 –D array is a collection of 1-D array,similarly a 3-D array is collection of 2-D array,a 4-D array
is a collection of 3-D array and so on:-
A 3-D array in C can be declared as:
Int arr[2][3][4];
The array can be initialized as :-
int a[2][3][4]={{{1,2},{2,3},{3,4}},{{3,2},{2,5},{7,2}}
for(i=0;i<2;i++)
{
printf(“2D Array %d\n”,i+1);
for(j=0;j<3;j++)
{
for(k=0;k<2;k++)
printf(“%d”,a[i][j][k]);
printf(“\n”);
}
An n dimensional m1*m2*…….mn
Array B is a collection of m1.m2…..mn data elements in which each element is specified by a list
of n integers-such as K1,K2,….Kn called subscripts,with the property that
1≤k1≤m1, 1≤k2≤m2,……,1≤kn≤mn
The element of B with subscripts k1,k2,…….,kn will be denoted by
Bk1,k2,…..,kn Or B[K1,K2,……..,KN]
Suppose B is a three –dimensional 2*4*3 array.Then B contains 2*3*4=24 elements.
Li=Upper bound-Lower Bound+1
Ei=Ki-Lower Bound
Then the address LOC(C[K1,K2,….,KN] of an arbitrary element of C can be obtained from the
formula
Column Major:-Base(C)+w[(((…(ENLN-1+EN-1)LN-2)+….+E3)L2+E2)L1+E1]
Or from the formula
Row Major:-Base(C)+w[(((…(E1L2+E2)L3+E3)L4+….+EN-1)LN+EN]
Whether C is stored in column major or row major .
Base(C) denotes the address of the first element of C
W denotes the number of words per memory location.
Suppose a three dimensional array MAZE is declared using
MAZE(2:8,-4:1,6:10)
The address of an element of MAZE for example,MAZE[5,-1,8] is obtained as follows.Effective indices of the subscripts are
E1=5-2=3,E2=-1-(-4)=3 ,E3=8-6=2
E1L2=3.6=18
E1L2+E2=18+13=21
(E1L2+E2)L3=21.5=105
(E1L2+E2)L3+E3=105+2=107
Therefore,LOC(MAZE[5,-1,8]=200+4(107)=200+428=628
Q)Suppose multidimensional arrays A and B are declared using
A(-2:2,2:22) and B(1:8,-5:5,-10:5)
(a) Find the length of each dimension and the number of elements in A and B.
Length Li of dimensions of A are:
Length=Upper bound-Lower bound+1
L1=2-(-2)+1=5 and L2=22-2+1=21
Accordingly ,A has 5.21=105 elements.
Length of Li of dimension B are:
L1=8-1+1=8,L2=5-(-5)+1=11,L3=5-(-10)+1=16
B has 8.11.16=1408 elements
(b) Consider the element B[3,3,3] in B.Find the effective indices E1,E2,E3 and the address of
the element,assuming Base (B)=400 and there are w=4 words per memory location.
The effective index Ei is obtained from Ei=ki-LB,where ki is the given index and LB is the lower
bound.Hence
E1=3-1=2, E2=3-(-5)=8,E3=3-(-10)=13
E3L2=13.11=143
E3L2+E2=143+8=151
(E3L2+E2)L1=151.8=1208
(E3L2+E2)L1+E1=1208+2=1210
Therefore LOC(B[3,3,3])=400+4(1210)=400+4840=5240
Difference between Vector and array
1. Vectors contain contiguous element stored as an array.
Actually array is used to store similar data type but vector is used to store object data type and
object can be of string of any class.
2.Vector is also an array but the size of vector can change dynamically where in array it is fixed.
Q) A multidimensional array is declared as z[3:30;-5:15;10:20].Base[z]=1000 and there are 4
words per memory location?
(i) Find the length of each dimension and number of elements in z.
L1=UB1-LB1+1=30-3+1=28
L2=UB2-LB2+1=15-(-5)+1=21
L3=UB3-LB3+1=20-10+1=11
Total number of elements in z=L1.L2.L3=28.21.11=6468
(ii) Find the address of z(5,10,15) assume z is stored in row major
L1=28 L2=21 L3=11
E1=5-3=2 E2=10-(-5)=15 E3=15-10=5
Effective address ((E1.L2+E2)L3 +E3)
A[5,10,15]=B.A+w((E1.L2+E2)L3 +E3)
=1000+4(2.21+15)11+5)=1000+4(632)=3528
Q)Consider the following multidimensional array Y[3:10;1:15;10:20]
(a) Find the number of elements in Y
L1=UB1-LB1+1=10-3+1=8
L2=UB2-LB2+1=15-1+1=15
L3=UB3-LB3+1=20-10+1=11
Total number of elements in Y=L1*L2*L3=8*15*11=1320
(b)Suppose base address for Y=400 and size of each element in the array is 2.Find the effective
address of Y[5,10,15] assuming Y is stored in row major order?
Base Address(B.A.)=400
Size(width,w)=2
L1=18 L2=15 L3=11
E1=k1-LB1=5-3=2 E2=K2-LB2=10-1=9 E3=K3-LB3=15-10=5
As array Y is stored in row major order
Y[5,10,15]=B.A.+w((E1.L2+E2)L3+E3)
= 400+2((2.15+9)11+5)
=400+2(434)=1268
Q) In an array SPACE(-1:10;5:14).Find
L1=UB1-LB1+1=10-(-1)+1=12
L2=UB2-LB2+1=14-5+1=10
L1*L2=12*10=120
(iii) If the starting address of the array is START and size of each element is SIZE.What is the address of the element
SPACE(5,8)?
SPACE(5,8)=START+SIZE(L2[5-1]+[8-1])
=START+SIZE(47)
SPACE(5,8)=START+SIZE(L1[8-1]+[5-1])
=START+SIZE(88)
Q) A 4-D array a[3][4][5][6] is stored using column major order.What will be the address of
a[2][3][4][5].If the base address of array is α and β is the size of each element ?Assume that
array indices starts from zero?
L1=3 L2=4 L3=5 L4=6
E1=2 E2=3 E3=4 E4=5
As array is stored column wise ,hence the effective address is
(((E4.L3+E3)L2+E2)L1+E1
(((5.5+4)4+3)3 +2 )=359
Hence the address is
α[2][3][4][5]=BA+w(E.A)=α+β(359)=α+359β
Sparse Matrices
A sparse matrix is a matrix where majority of its elements are zero and very few non-zero
element exist.
0 2 3 0 0 - 2 3 - -
0 2 1 0 0 or - 2 1 - -
0 0 0 0 5 - - - - 5
0 3 0 0 0 - 3 - - -
Representation of Sparse Matrix
(1) Representation of sparse matrix using three column form:
In this representation, we consider only non-zero values along with their row and column index
values. In this representation, the 0th row stores the total number of rows, total number of
columns and the total number of non-zero values in the sparse matrix.
In example matrix, there are only 6 non-zero elements ( those are 9, 8, 4, 2, 5 & 2) and matrix size
is 5 X 6. We represent this matrix as shown in the above image. Here the first row in the right side
table is filled with values 5, 6 & 6 which indicates that it is a sparse matrix with 5 rows, 6 columns &
6 non-zero values. The second row is filled with 0, 4, & 9 which indicates the non-zero value 9 is at
the 0th-row 4th column in the Sparse matrix. In the same way, the remaining non-zero values also
follow a similar pattern.
2 0 0 3 0 0
0 6 8 0 0 0
5 0 0 2 0 0
0 1 0 4 0 4
Array Representation
4 6 9
0 0 2
0 3 3
1 1 6
1 2 8
2 0 5
2 3 2
3 1 1
3 3 4
3 5 4
Using Linked Lists
In linked list, each node has four fields. These four fields are defined as:
•Row: Index of row, where non-zero element is located
•Column: Index of column, where non-zero element is located
•Value: Value of the non zero element located at index – (row,column)
•Next node: Address of the next node
Why to use Sparse Matrix instead of simple matrix ?
Storage: There are lesser non-zero elements than zeros and thus lesser memory can
be used to store only those non-zero elements.
Computing time:Computing time can be saved by logically designing a data structure
traversing only non-zero elements..
Type of Sparse Matrix
1. Lower Triangular matrix:A square matrix is said to be lower triangular when all non-zero
elements resides at and below the main diagonal and all elements above main diagonal are
zero.
ꭓ 0 0 0
ꭓ ꭓ 0 0
ꭓ ꭓ ꭓ 0
ꭓ ꭓ ꭓ ꭓ
2)Upper triangular matrix:-
A square matrix is said to be upper triangular when all non zero elements of matrix resides at
and above the main diagonal and all elements below the main diagonal are zero.
ꭓ ꭓ ꭓ ꭓ
0 ꭓ ꭓ ꭓ
0 0 ꭓ ꭓ
0 0 0 ꭓ
3.Diagonal matrix
A square matrix is said to be diagonal when all non –zero elements of matrix resides at the main
diagonal.For example the matrices shown below
ꭓ 0 0
0 ꭓ 0
0 0 ꭓ
4.Tridiogonal matrix
A square matrix is said to be tridiagonal when all non-zero elements of matrix resides at the
main diagonal and just above and just below the main diagonal and all other elements are zero.
ꭓ ꭓ 0 0
ꭓ ꭓ ꭓ 0
0 ꭓ ꭓ ꭓ
0 0 ꭓ ꭓ
Q) For the given sparse matrix obtain the array or three column representation
15 0 0 22 0 -15
0 11 3 0 0 0
0 0 0 -6 0 0
0 0 0 0 0 0
91 0 0 0 0 0
0 0 28 0 0 0
Sol.
6 6 8
0 0 15
0 3 22
0 5 -15
1 1 11
1 2 3
2 3 -6
4 0 91
5 2 28
Operation on sparse matrix
•Add
•Transpose and
•Multiply
Given two sparse matrices, perform the operations such as add, multiply or transpose of
the matrices in their sparse form itself.
The result should consist of three sparse matrices, one obtained by adding the two input
matrices, one by multiplying the two matrices and one obtained by transpose of the first
matrix.
Example: Note that other entries of matrices will be zero as matrices are sparse.
Operation on sparse matrix
https://www.happycompiler.com/memory-addresses-arrays/
Follow only Array numerical from the above link