3D Array
3D Array
Example:
int array[3][3][3];
Initialization of Three-Dimensional Array:
Initialization in a 3D array is the same as that of 2D arrays. The difference is as the number of dimensions increases
so the number of nested braces will also increase.
A 3D array in C can be initialized by using:
1. Initializer List
2. Loops
Multidimensional array:
Multidimensional arrays are one of the most powerful features of the C programming language. They allow you to
store data in a table-like format, where each row and column can be accessed using an index.
Sparse Matrix:
Sparse matrices are those matrices that have the majority of their elements equal to zero. In other words, the sparse
matrix can be defined as the matrix that has a greater number of zero elements than the non-zero elements.
There are the following benefits of using the sparse matrix -
Storage - We know that a sparse matrix contains lesser non-zero elements than zero, so less memory can be used to
store elements. It evaluates only the non-zero elements.
Computing time: In the case of searching in sparse matrix, we need to traverse only the non-zero elements rather
than traversing all the sparse matrix elements. It saves computing time by logically designing a data structure
traversing non-zero elements.
Row - It is the index of a row where a non-zero element is located in the matrix.
Column - It is the index of the column where a non-zero element is located in the matrix.
Value - It is the value of the non-zero element that is located at the index (row, column).
Example - Consider the sparse matrix -
In the above figure, we can observe a 5x4 sparse matrix containing 7 non-zero elements and 13 zero elements. The
above matrix occupies 5x4 = 20 memory space. Increasing the size of matrix will increase the wastage space.
The tabular representation of the above matrix is given below -
In the above figure, we can observe a 4x4 sparse matrix containing 5 non-zero elements and 11 zero elements.
Above matrix occupies 4x4 = 16 memory space. Increasing the size of matrix will increase the wastage space.
The linked list representation of the above matrix is given below -
Polynomial Manipulation:
Polynomial manipulations are one of the most important applications of linked lists. Polynomials are an important
part of mathematics not inherently supported as a data type by most languages. A polynomial is a collection of
different terms, each comprising coefficients, and exponents. It can be represented using a linked list. This
representation makes polynomial manipulation efficient.
While representing a polynomial using a linked list, each polynomial term represents a node in the linked list. To get
better efficiency in processing, we assume that the term of every polynomial is stored within the linked list in the
order of decreasing exponents. Also, no two terms have the same exponent, and no term has a zero coefficient and
without coefficients. The coefficient takes a value of 1.
Each node of a linked list representing polynomial constitute three parts:
The first part contains the value of the coefficient of the term.
The second part contains the value of the exponent.
The third part, LINK points to the next term (next node).
The structure of a node of a linked list that represents a polynomial is shown below:
Example Consider a polynomial P(x) = 7x2 + 15x3 - 2 x2 + 9. Here 7, 15, -2, and 9 are the coefficients, and 4,3,2,0
are the exponents of the terms in the polynomial. On representing this polynomial using a linked list, we have
Observe that the number of nodes equals the number of terms in the polynomial. So we have 4 nodes. Moreover, the
terms are stored to decrease exponents in the linked list. Such representation of polynomial using linked lists makes
the operations like subtraction, addition, multiplication, etc., on polynomial very easy.
Addition of Polynomials:
To add two polynomials, we traverse the list P and Q. We take corresponding terms of the list P and Q and compare
their exponents. If the two exponents are equal, the coefficients are added to create a new coefficient. If the new
coefficient is equal to 0, then the term is dropped, and if it is not zero, it is inserted at the end of the new linked list
containing the resulting polynomial. If one of the exponents is larger than the other, the corresponding term is
immediately placed into the new linked list, and the term with the smaller exponent is held to be compared with the
next term from the other list. If one list ends before the other, the rest of the terms of the longer list is inserted at the
end of the new linked list containing the resulting polynomial.
Example - Consider an example to show how the addition of two polynomials is performed,
P(x) = 3x4 + 2x3 - 4 x2 + 7
Q (x) = 5x3 + 4 x2 - 5
These polynomials are represented using a linked list in order of decreasing exponents as follows:
To generate a new linked list for the resulting polynomials that is formed on the addition of given polynomials P(x)
and Q(x), we perform the following steps,
Traverse the two lists P and Q and examine all the nodes.
We compare the exponents of the corresponding terms of two polynomials. The first term of polynomials P and Q
contain exponents 4 and 3, respectively. Since the exponent of the first term of the polynomial P is greater than the
other polynomial Q, the term having a larger exponent is inserted into the new list. The new list initially looks as
shown below:
We then compare the exponent of the next term of the list P with the exponents of the present term of list Q. Since
the two exponents are equal, so their coefficients are added and appended to the new list as follows:
Then we move to the next term of P and Q lists and compare their exponents. Since exponents of both these terms
are equal and after addition of their coefficients, we get 0, so the term is dropped, and no node is appended to the
new list after this,
Moving to the next term of the two lists, P and Q, we find that the corresponding terms have the same exponents
equal to 0. We add their coefficients and append them to the new list for the resulting polynomial as shown below:
For performing the addition of two long integers, the following steps need to be followed:
Traverse the two linked lists in parallel from left to right.
During traversal, corresponding digits and a carry from prior digits sum are added, then stored in the new node of
the resultant linked list.
The first positive long integer 543467 is represented using a linked list whose first node is pointed by NUM1
pointer. Similarly, the second positive long integer 48315 is represented using the second linked list whose first node
is pointed by NUM2 pointer. These two numbers are stored in the third linked list whose first node is pointed to by
the RESULT pointer.
Consider a polynomial P(x, y, z) = 10x2y2z + 17 x2y z2 - 5 xy2 z+ 21y4z2 + 7. On representing this polynomial using
linked list are:
Terms in such a polynomial are ordered accordingly to the decreasing degree in x. Those with the same degree in x
are ordered according to decreasing degree in y. Those with the same degree in x and y are ordered according to
decreasing degrees in z.