0% found this document useful (0 votes)
30 views

Arrays

An array is a collection of variables of the same type accessed by a common name and index. The document explains one-dimensional and two-dimensional arrays, including their declaration, initialization, and operations such as insertion, deletion, and traversal. It also covers algorithms for these operations and provides examples in C programming, along with address calculation for array elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Arrays

An array is a collection of variables of the same type accessed by a common name and index. The document explains one-dimensional and two-dimensional arrays, including their declaration, initialization, and operations such as insertion, deletion, and traversal. It also covers algorithms for these operations and provides examples in C programming, along with address calculation for array elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 74

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

[End of Step 2 Loop.] 45 3


50
(5) Exit 4
Alternative Algorithm For traversing a
linear array
(Traversing a Linear array)This algorithm traverses a linear array LA with lower bound LB and
upper bound UB.
(1) Repeat for K=LB to UB:
Apply PROCESS to LA[K].
[End of Loop.]
(2) Exit.
Example of traversing a linear array
Consider the array AUTO which records the number of automobiles sold each year from 1932
through 1984.Each of the following modules,which carry out the given operation ,involves
traversing AUTO.
(A) Find the number NUM of years during which more than 300 automobiles were sold.
1.[Initalization step]Set NUM:=0.
2.Repeat for K=1932 to 1984:
If AUTO[K]>300,then : Set NUM:=NUM+1.
[End of Loop]
3.Return
(b)Print each year and the number of automobiles sold in that year
1. Repeat for K=1932 to 1984:
Write : K, AUTO[K].
[End of Loop.]
2.Return
Inserting an element in Array
Let A be a collection of data elements in the memory of computer.Inserting refers to the
operation of adding another element to collection A.
Inserting an element at the end of a linear array can be easily done at the end of array provided
the memory space allocated for the array is large enough to accommodate the additional
element.Suppose we want to insert an elelment in the middle of the array.Then on the average
half of the elements must be moved downward to new locations to accommodate the new
element and keep the order of other element.
Algorithm for inserting an element in
array
Inserting into a Linear Array)INSERT(LA,N,K,ITEM)
Here LA is a linear array with N elements and K is a positive integer K<=N. This algorithm insert an
element ITEM into the Kth position in LA.
(1) [Initialize counter] Set J:=N.
(2) Repeat Steps 3 and 4 while J>=K.
(3) [Move Jth element downward.]Set LA[J+1]:=LA[J]
(4) [Decrease Counter.]Set J:=J-1.
[End of Step 2 Loop]
(5) Set LA[K] := ITEM
(6)[Reset N.]Set N=N+1.
(7) Exit
Deleting an element in Linear Array
Deleting an element at the end of array present no difficulties,but deleting an element
somewhere in the middle of the array would require that each subsequent element be moved
one location upward in order to fill up the array.
Algorithm for deletion from array
(Deleting from a linear array)DELETE(LA,N,K,ITEM)
Here LA is a linear array with n elements and K is a positive integer such that K<=N.This
algorithm deletes the Kth elelment from LA.
(1) Set ITEM:=LA[K].
(2) Repeat for J=K to N-1:
[Move J+1st element upward ]Set LA[J]:=LA[J+1].
[End of Loop.]
(3) [Reset the number N of elements in LA.]Set N:=N-1.
(4) Exit
Example of Inserting and deleting an
array
Suppose NAME is an 8-elelment array, and suppose five names are in the array as in the Fig
(a)Observe that the names are listed alphabetically and suppose we want to keep the array
names to keep the array names alphabetical at all times .
Suppose Ford is added to the array.Then Jhonson ,Smith and Wagner must each be moved
downward one location ,as in fig(b).Next suppose Taylor is added to array,then wagner must be
moved as shown in fig (c).Last suppose David is removed from the array.Then five names
Ford,Jhonson,Smith,Taylor and wagner must be move upward one location as shown in fig
(d).Clearly such movement of data would be very expensive if thousand of names were in array.
Example of Inserting and deleting an
array
Name Name Name Name
Brown 1 Brown 1 Brown 1 Brown
1
Davis Davis Davis 2 Ford
2 2 2

3 Jhonson Ford 3 Ford 3 Jhonson


3

Jhonson 4 Jhonson 4 Smith


4 Smith 4
5 Smith 5 Taylor
5 Wagner 5 Smith
6 6 Taylor Wagner
Wagner 6
6
7 7 Wagner
7 7
8 8
8 8

Fig a Fig b Fig c Fig d


C Program for Inserting an element in array
#include <stdio.h> printf("Enter the value to insert\n");

int main() scanf("%d", &value);

{
for (c = n - 1; c >= position - 1; c--)

int array[100], position, c, n, value;


array[c+1] = array[c];
printf("Enter number of elements in array\n");
array[position-1] = value;
scanf("%d", &n);
printf("Resultant array is\n");
printf("Enter %d elements\n", n);
for (c = 0; c <= n; c++)
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
scanf("%d", &array[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];

printf("Enter %d elements\n", n); printf("Resultant array:\n");

for (c = 0; c < n; c++) for (c = 0; c < n - 1; c++)


scanf("%d", &array[c]); printf("%d\n", array[c]);
}
printf("Enter the location where you wish to delete element\n"); return 0; }
C program to delete an element in array
1-d Array address calculation

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].

(Sol at next slide)


Sol.
(a) The number of elements is equal to the length; hence use the
formula
Length=UB-LB+1
Accordingly, Length(AAA)=50-5+1=46
Length(BBB)=10-(-5)+1=16
Length(CCC)=18-1+1=18
Note that Length(CCC)=UB,since LB=1
(b)Use the formula
LOC(AAA[K])=Base(AAA)+w(K-LB)
Hence: LOC(AAA[15])=300+4(15-5)=340
LOC(AAA[35])=300+4(35-5)=420
AAA[55] is not an element of AAA,since 55 exceeds UB=50.
Q)Suppose the two arrays A1 and A2 are declared as :
A1(-3:3,3:33) and A2(1:9,-4:4,-10:5)
Find the length of each dimension and number of elements in A1 and A2.
Sol.(i)First dimension length L1 in A1=3-(-3)+1=7
Second dimension length L2 in A1=33-3+1=31
Number of elements in A1=L1*L2=7*31=217
(ii) First dimension L1 in A2=9-1+1=9
Second dimension length L2 in A2=4-(-4)+1=9
Third dimension length L3 in A2=5-(-10)+1=16
Number of elements in A2 =9*9*16=1296
Q) Suppose a company keeps a linear array YEAR(1920:1970)such that YEAR[K] contains the
number of employees born in year K.Write a module for each of the following tasks:
(a) To print each of the years in which no employee was born.
(1) Repeat for K=1920 to 1970:
If year[K]=0,then: write:K.
[End of Loop.]
(2) Return.
(b)To find the number NNN of years in which no employee was born
Sol. (1) Set NNN:=0.
(2) Repeat for K=1920 to 1970:
If YEAR[K] = 0 ,then : Set NNN:=NNN+1.
[End of Loop.]
(3) Return .
Two Dimensional Array
A two dimensional array can be thought of a matrix of rows and columns.
Declaration:int arr[3][4];

Col 1 Col 2 Col 3 Col 4


Row 1 arr[0][0] arr[0][1] arr[0][2] arr[0][3]
Row 2 arr[1][0] arr[1][1] arr[1][2] arr[1][3]
Row 3 arr[2][0] arr[2][1] arr[2][2] arr[2][3]

Number of elements=3*4=12 elements


2-D Array
Many programming languages (like visual Basic) allow integer ranges for rows and columns instead of
just integer.
ARR(10:15,-2:2)
Here 10 is the LB for the first dimension and 15 is the UB.
For second dimension -2 is LB and 2 is UB.
In this example we will have rows :10,11,12,13,14,15 and for each of the preceding rows we will have
column as: -2,-1,0,1,2
NR=UB-LB+1=15-10+1=6
NC=UB-LB+1=2-(-2)+1=5
Matrix arr is of size 6 by 5.
Number of elements=NR*NC=6*5=30
Initialization of 2-D Array
int arr[3][3]={{1,2,3},{4,3,6},{2,3,4}};
Another way of initializing a 2-D array is
int arr[][3]={1,2,3,4,3,6,2,3,4};
Row size can be left blank but column size cannot be left blank.
Storing 2-d array in memory
Let A be a two dimensional m*n array. A is a rectangular array of elements
with m rows and n columns,the array will be represented in memory by block
of m.n sequential memory locations.
The programming language will store the array A either
(1)Column by Column what is called column-major order
(2) Row by Row what is called row-major order
Row Major Order:- In this method the elements of the array that have first subscript,the lower
bound value of that subscript are stored first followed by the elements of the second value of
the first subscript and so on.In short in row order rows are listed on the basis of number of
columns.
Column major order: In column major order columns are listed on the number of rows.
1. Column Major ordering(memory
representation of 2-d array)
(1,1)

(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)

Then the length of the three dimension of MAZE are respectively,

L1=8-2+1=7 L2=1-(-4)+1=6 L3=10-6+1=5

Accordingly MAZE contains L1*L2*L3=7.6.5=210 elements.

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

For row major order=

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

(i) The length in each direction

L1=UB1-LB1+1=10-(-1)+1=12

L2=UB2-LB2+1=14-5+1=10

(ii) The number of elements that can be stored.

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)?

If an array is stored in row –major order,i.e.

SPACE(5,8)=START+SIZE(L2[5-1]+[8-1])

=START+SIZE(47)

If an array is stored in column –major order

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

You might also like