0% found this document useful (0 votes)
43 views13 pages

Computer Termpaper

The document summarizes information about sparse matrices and multi-dimensional arrays in C programming. It defines sparse matrices as matrices with mostly zero elements and discusses different ways to store sparse matrices, including array and linked list representations. It also presents a program to check if a matrix is sparse or not. Furthermore, it covers key aspects of multi-dimensional arrays like declaring and accessing elements of 2D arrays and calculating their sizes. Some disadvantages of multi-dimensional arrays are fixed memory allocation and need to know array sizes in advance.

Uploaded by

sowe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views13 pages

Computer Termpaper

The document summarizes information about sparse matrices and multi-dimensional arrays in C programming. It defines sparse matrices as matrices with mostly zero elements and discusses different ways to store sparse matrices, including array and linked list representations. It also presents a program to check if a matrix is sparse or not. Furthermore, it covers key aspects of multi-dimensional arrays like declaring and accessing elements of 2D arrays and calculating their sizes. Some disadvantages of multi-dimensional arrays are fixed memory allocation and need to know array sizes in advance.

Uploaded by

sowe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Paper Name: PROBLEM SOLVING THROUGH C PROGRAMING Code : ESCs201

Term Paper/Micro-project Title: "sparse matrix and multi dimensional


arrays"

________________________________________________________________________________

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

Signature of Faculty with date________________________________________________

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.

We then discussed about multi dimensional arrays, their


declaration, calculating the size of multi dimensional arrays. We
also learnt about 2D array and also the disadvantages of multi
dimensional array. We did a program based on multi dimensional
array.

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:

 Those that support efficient modification, such as DOK (Dictionary of keys),


LIL (List of lists), or COO (Coordinate list). These are typically used to
construct the matrices.
 Those that support efficient access and matrix operations, such as CSR
(Compressed Sparse Row) or CSC (Compressed Sparse Column).

SPARSE MATRIX
REPRESENTATIONS

Representing a sparse matrix by a 2D array leads to wastage of lots of


memory as zeroes in the matrix are of no use in most of the cases. So,
instead of storing zeroes with non-zero elements, we only store non-zero
elements. This means storing non-zero elements with triples- (Row,
Column, value).

Sparse Matrix Representations can be done in many ways following are


two common 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)

LINKED LIST REPRESENTATION

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

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;

printf("Enter the order of the matrix \n");

scanf("%d %d", &m, &n);

printf("Enter the elements of the matix \n");

for (i = 0; i < m; ++i)

for (j = 0; j < n; ++j)

scanf("%d", &matrix[i][j]);

if (matrix[i][j] == 0)

++sparse_counter;

if (sparse_counter > ((m * n) / 2))

printf("The given matrix is Sparse Matrix !!! \n");


}

else

printf("The given matrix is not a Sparse Matrix \n");

printf("There are %d number of Zeros.", sparse_counter);

OUTPUT OF THE
PROGRAM
Enter the order of the matix 3 3

Enter the elements of the matix

123

400

000

The given matrix is Sparse Matrix !!!

There are 5 number of Zeros.


SIZE OF MULTI
DIMENSIONAL
ARRAYS
Total number of elements that can be stored in a multidimensional array
can be calculated by multiplying the size of all the dimensions.
For example:
The array int x[10][20] can store total (10*20) = 200 elements.
Similarly array int x[5][10][20] can store total (5*10*20) = 1000 elements

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

 Elements in two-dimensional arrays are commonly referred by


x[i][j] where i is the row number and ‘j’ is the column number.
 A two – dimensional array can be seen as a table with ‘x’ rows
and ‘y’ columns where the row number ranges from 0 to (x-1) and
column number ranges from 0 to (y-1). A two – dimensional array
‘x’ with 3 rows and 3 columns is shown below:

Initializing Two – Dimensional Arrays: There are two ways in which a


Two-Dimensional array can be initialized.
First Method:
int x[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}
The above array have 3 rows and 4 columns. The elements in the
braces from left to right are stored in the table also from left to right.
The elements will be filled in the array in the order, first 4 elements
from the left in first row, next 4 elements in second row and so on.
Better Method:
int x[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};
This type of initialization make use of nested braces. Each set of inner
braces represents one row. In the above example there are total three
rows so there are three sets of inner braces.
Accessing Elements of Two-Dimensional Arrays: Elements in Two-
Dimensional arrays are accessed using the row indexes and column
indexes.
Example:

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.

C programming language allows multidimensional arrays. Here is the general form of a


multidimensional array declaration −

type name[size1][size2]...[sizeN]; For example, the following declaration creates a


three dimensional integer array −

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;

     printf("Enter size of array: ");

    scanf("%d",&n);

     printf("Enter %d elements in the array : ", n);

    for(i=0;i<n;i++)

    {

        scanf("%d", &a[i]);

    }

    printf("\nElements in array are: ");

    for(i=0;i<n;i++)

    {

        printf("%d  ", a[i]);
    }

    return 0;

OUTPUT OF THE
PROGRAM
Enter size of array: 5

Enter 5 elements in the array: 1

Elements in array are: 1  2  3  4  5


CONCLUSION
So Sparse matrix is basically a type of matrix which has the majority
of elements as 0. Sparse matrix is stored in the memory in a specific
way and has two types of representations. On the other hand multi
dimensional arrays are the arrays of arrays.2d array is an example
of it. Multi dimensional arrays are very useful as they can contain a
lot of data under a single variable name. Multi Dimensional arrays
also have certain disadvantages.

REFERENCES
www.geeksforgeeks.org/multidimensional-arrays-c-cpp

www.geeksforgeeks.org/sparse-matrix-representation

en.wikipedia.org/wiki/Sparse_matrix

You might also like