Computer Termpaper
Computer Termpaper
________________________________________________________________________________
Student Details
Name University Roll No.
Adhiraj Saha (11500119054)
Kuntal Roy (11500119045)
Dipayan Dasgupta (11500119002)
Nabajyoti Modak (11500119044)
(11500119122)
< Please do not write anything below the dotted line >
…………………………………………............................................................................................................................
Marks awarded
Marks Awarded
Total Marks
ABSTRACTION
In this project we are going to discuss about sparse matrix and
multi dimensional arrays. We are going to discuss about the
definition of sparse matrix, how they are stored in the memory and
how they are represented. We also did a program to check
whether a matrix is sparse or not.
INTRODUCTION
In numerical analysis and scientific computing, a sparse matrix or sparse
array is a matrix in which most of the elements are zero. By contrast, if most of
the elements are nonzero, then the matrix is considered dense. The number of
zero-valued elements divided by the total number of elements (e.g., m × n for
an m × n matrix) is called the sparsity of the matrix (which is equal to 1 minus
the density of the matrix). Using those definitions, a matrix will be sparse when
its sparsity is greater than 0.5.
Conceptually, sparsity corresponds to systems with few pairwise interactions.
Consider a line of balls connected by springs from one to the next: this is a
sparse system as only adjacent balls are coupled. By contrast, if the same line
of balls had springs connecting each ball to all other balls, the system would
correspond to a dense matrix. The concept of sparsity is useful
in combinatorics and application areas such as network theory, which have a
low density of significant data or connections.
In C/C++, we can define multidimensional arrays in simple words as array of
arrays. Data in multidimensional arrays are stored in tabular form (in row
major order)
Programming languages such C,C++,java etc support multi dimensional
arrays. They occupy a large amount of memory area but still they can be used
to store a large amount of data under a single variable name.
STORING A
SPARSE MATRIX
A matrix is typically stored as a two-dimensional array. Each entry in the array
represents an element ai,j of the matrix and is accessed by the two indices i and j.
Conventionally, i is the row index, numbered from top to bottom, and j is the
column index, numbered from left to right. For an m × n matrix, the amount of
memory required to store the matrix in this format is proportional
to m × n (disregarding the fact that the dimensions of the matrix also need to be
stored).
In the case of a sparse matrix, substantial memory requirement reductions can be
realized by storing only the non-zero entries. Depending on the number and
distribution of the non-zero entries, different data structures can be used and
yield huge savings in memory when compared to the basic approach. The trade-
off is that accessing the individual elements becomes more complex and
additional structures are needed to be able to recover the original matrix
unambiguously.
Formats can be divided into two groups:
SPARSE MATRIX
REPRESENTATIONS
Array representation
Linked list representation
ARRAY REPRESENTATION
2D array is used to represent a sparse matrix in which there are three rows
named 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)
In linked list, each node has four fields. These four fields are defined as:
PROGRAM TO CHECK
WHETHER A MATRIX
IS SPARSE OR NOT
#include <stdio.h>
void main ()
Int matrix[10][10];
int i, j, m, n;
int sparse_counter = 0;
scanf("%d", &matrix[i][j]);
if (matrix[i][j] == 0)
++sparse_counter;
else
OUTPUT OF THE
PROGRAM
Enter the order of the matix 3 3
123
400
000
2 DIMENSIONAL
ARRAYS
Two – dimensional array is the simplest form of a multidimensional
array. We can see a two – dimensional array as an array of one –
dimensional array for easier understanding.
The basic form of declaring a two-dimensional array of size x, y:
Syntax:
data_type array_name[x][y];
data_type: Type of data to be stored. Valid C/C++ data type.
We can declare a two dimensional integer array say ‘x’ of size
10,20 as:
int x[10][20];
int x[2][1];
The above example represents the element present in third row and
second column.
Note: In arrays if size of array is N. Its index will be from 0 to N-1.
Therefore, for row index 2 row number is 2+1 = 3.
To output all the elements of a Two-Dimensional array we can use
nested for loops. We will require two for loops. One to traverse the
rows and another to traverse columns.
DELARING A MULTI
DIMENSIONAL ARRAY
In C/C++, initialization of a multidimensional arrays can have left most dimension as
optional. Except the left most dimension, all other dimensions must be specified.
int threedim[5][10][4];
DISADVANTAGES OF
MULTI DIMENSIONAL
ARRAY
1. We must know in advance that how many elements are to be stored in multi
dimensional array.
2. Array is static structure. It means that array is of fixed size. The memory which is
allocated to array can not be increased or reduced.
3. Since multi dimensional arrays are of fixed size, if we allocate more memory than
requirement then the memory space will be wasted. And if we allocate less memory than
requirement, then it will create problem.
4. The elements of multi dimensional array are stored in consecutive memory locations.
So insertions and deletions are very difficult and time consuming.
MULTI DIMENSIONAL
ARRAY PROGRAM
#include <stdio.h>
int main()
int a[1000],i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
for(i=0;i<n;i++)
{
printf("%d ", a[i]);
}
return 0;
OUTPUT OF THE
PROGRAM
Enter size of array: 5
REFERENCES
www.geeksforgeeks.org/multidimensional-arrays-c-cpp
www.geeksforgeeks.org/sparse-matrix-representation
en.wikipedia.org/wiki/Sparse_matrix