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

3D Array

The document discusses three-dimensional arrays in C. It describes how to declare, initialize, and access elements of 3D arrays. It also discusses representations of sparse matrices using arrays and linked lists and provides examples.

Uploaded by

ansarisadique
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)
148 views

3D Array

The document discusses three-dimensional arrays in C. It describes how to declare, initialize, and access elements of 3D arrays. It also discusses representations of sparse matrices using arrays and linked lists and provides examples.

Uploaded by

ansarisadique
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/ 8

Three-Dimensional Array:

A Three Dimensional Array or 3D array in C is a collection of two-dimensional arrays. It can be visualized as


multiple 2D arrays stacked on top of each other.

Declaration of Three-Dimensional Array:


We can declare a 3D array with x 2D arrays each having y rows and z columns using the syntax shown below.
Syntax:
data_type array_name[x][y][z];
 data_type: Type of data to be stored in each element.
 array_name: name of the array
 x: Number of 2D arrays.
 y: Number of rows in each 2D array.
 z: Number of columns in each 2D 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

Initialization of 3D Array using Initializer List:


Method 1:
int x[2][3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23};
Method 2(Better):
int x[2][3][4] =
{
{ {0,1,2,3}, {4,5,6,7}, {8,9,10,11} },
{ {12,13,14,15}, {16,17,18,19}, {20,21,22,23} }
};

Initialization of 3D Array using Loops:


It is also similar to that of 2D array with one more nested loop for accessing one more dimension.
int x[2][3][4];
for (int i=0; i<2; i++) {
for (int j=0; j<3; j++) {
for (int k=0; k<4; k++) {
x[i][j][k] = (some_value);
}
}
}

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.

Syntax of Multidimensional Arrays:


To create a multidimensional array in C, you need to specify the number of dimensions and the size of each
dimension. The general syntax for declaring a multidimensional array is as follows:
type array_name[size1][size2]...[sizeN];
Here, type is the data type of the elements that will be stored in the array, array_name is the name of the array,
and size1, size2, ..., sizeN are the sizes of each dimension of the array.

Accessing Elements of Multidimensional Arrays:


To access an element of a multidimensional array, you need to specify the indices for each dimension. For example,
to access the element in the second row and third column of my_array, you would use the following syntax:
int element = my_array[1][2];
Note that the indices start at 0, so the first row is my_array[0], the second row is my_array[1], and so on. Similarly,
the first column of each row is my_array[i][0], and so on.

Initializing Multidimensional Arrays:


You can initialize a multidimensional array when you declare it by specifying the values for each element in the
array. For example, the following code declares and initializes a 2-dimensional array of integers with 2 rows and 3
columns:
int my_array[2][3] = {
{1, 2, 3},
{4, 5, 6}
};

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.

Representation of sparse matrix:


Now, let's see the representation of the sparse matrix. The non-zero elements in the sparse matrix can be stored
using triplets that are rows, columns, and values. There are two ways to represent the sparse matrix that are listed as
follows -
1. Array representation
2. Linked list representation

Array representation of the sparse matrix:


Representing a sparse matrix by a 2D array leads to the wastage of lots of memory. This is because zeroes in the
matrix are of no use, so storing zeroes with non-zero elements is wastage of memory. To avoid such wastage, we
can store only non-zero elements. If we store only non-zero elements, it reduces the traversal time and the storage
space.
In 2D array representation of sparse matrix, there are three fields used that are named as -

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 -

Linked List representation of the sparse matrix:


In a linked list representation, the linked list data structure is used to represent the sparse matrix. The advantage of
using a linked list to represent the sparse matrix is that the complexity of inserting or deleting a node in a linked list
is lesser than the array.
Unlike the array representation, a node in the linked list representation consists of four fields. The four fields of the
linked list are given as follows -
Row - It represents the index of the row where the non-zero element is located.
Column - It represents the index of the column where the non-zero element is located.
Value - It is the value of the non-zero element that is located at the index (row, column).
Next node - It stores the address of the next node.
The node structure of the linked list representation of the sparse matrix is shown in the below image -
Example - Consider the sparse matrix -

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 -

Application of Linked List:


1. Polynomial Manipulation representation
2. Addition of long positive integers
3. Representation of sparse matrices
4. Addition of long positive integers
5. Symbol table creation
6. Mailing list
7. Memory management
8. Linked allocation of files
9. Multiple precision arithmetic etc

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:

Addition of long positive integer using linked list:


Most programming languages allow restrictions on the maximum value of integers stored. The maximum value of
the largest integers is 32767, and the largest is 2147483647. Sometimes, applications such as security algorithms and
cryptography require storing and manipulating integers of unlimited size. So in such a situation, it is desirable to use
a linked list for storing and manipulating integers of arbitrary length.
Adding long positive integers can be performed effectively using a circular linked list. As we know that when we
add two long integers, the digits of the given numbers are individually traversed from right to left, and the
corresponding digits of the two numbers along with a carry from prior digits sum are added. So to accomplish
addition, we must need to know how the digits of a long integer are stored in a linked list.
The digits of a long integer must be stored from right to left in a linked list so that the first node on the list contains
the least significant digit, i.e., the rightmost digit, and the last node contains the most significant digit, i.e., leftmost
digit.
Example: An integer value 543467 can be represented using a linked list as

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.

Polynomial of Multiple Variables


We can represent a polynomial with more than one variable, i.e., it can be two or three variables. Below is a node
structure suitable for representing a polynomial with three variables X, Y, Z using a singly linked list.

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.

Other applications of linked list:


Memory Management: Memory management is one of the operating system's key features. It decides how to
allocate and reclaim storage for processes running on the system. We can use a linked list to keep track of portions
of memory available for allocation.
Mailing List: Linked lists have their use in email applications. Since it is difficult to predict multiple lists, maybe a
mailer builds a linked list of addresses before sending a message.
LISP: LISP is an acronym for LIST processor, an important programming language in artificial intelligence. This
language extensively uses linked lists in performing symbolic processing.
Linked allocation of files: A file of large size may not be stored in one place on a disk. So there must be some
mechanism to link all the scattered parts of the file together. The use of a linked list allows an efficient file
allocation method in which each block of a file contains a pointer to the file's text block. But this method is good
only for sequential access.
Virtual Memory: An interesting application of linked lists is found in the way systems support virtual memory.
Support for other data structures: Some other data structures like stacks, queues, hash tables, graphs can be
implemented using a linked list.

You might also like