Data Structures Tutorial

Data Structures Tutorial Asymptotic Notation Structure and Union Array Data Structure Linked list Data Structure Type of Linked list Advantages and Disadvantages of linked list Queue Data Structure Implementation of Queue Stack Data Structure Implementation of Stack Sorting Insertion sort Quick sort Selection sort Heap sort Merge sort Bucket sort Count sort Radix sort Shell sort Tree Traversal of the binary tree Binary search tree Graph Spanning tree Linear Search Binary Search Hashing Collision Resolution Techniques

Misc Topic:

Priority Queue in Data Structure Deque in Data Structure Difference Between Linear And Non Linear Data Structures Queue Operations In Data Structure About Data Structures Data Structures Algorithms Types of Data Structures Big O Notations Introduction to Arrays Introduction to 1D-Arrays Operations on 1D-Arrays Introduction to 2D-Arrays Operations on 2D-Arrays Strings in Data Structures String Operations Application of 2D array Bubble Sort Insertion Sort Sorting Algorithms What is DFS Algorithm What Is Graph Data Structure What is the difference between Tree and Graph What is the difference between DFS and BFS Bucket Sort Dijkstra’s vs Bellman-Ford Algorithm Linear Queue Data Structure in C Stack Using Array Stack Using Linked List Recursion in Fibonacci Stack vs Array What is Skewed Binary Tree Primitive Data Structure in C Dynamic memory allocation of structure in C Application of Stack in Data Structures Binary Tree in Data Structures Heap Data Structure Recursion - Factorial and Fibonacci What is B tree what is B+ tree Huffman tree in Data Structures Insertion Sort vs Bubble Sort Adding one to the number represented an array of digits Bitwise Operators and their Important Tricks Blowfish algorithm Bubble Sort vs Selection Sort Hashing and its Applications Heap Sort vs Merge Sort Insertion Sort vs Selection Sort Merge Conflicts and ways to handle them Difference between Stack and Queue AVL tree in data structure c++ Bubble sort algorithm using Javascript Buffer overflow attack with examples Find out the area between two concentric circles Lowest common ancestor in a binary search tree Number of visible boxes putting one inside another Program to calculate the area of the circumcircle of an equilateral triangle Red-black Tree in Data Structures Strictly binary tree in Data Structures 2-3 Trees and Basic Operations on them Asynchronous advantage actor-critic (A3C) Algorithm Bubble Sort vs Heap Sort Digital Search Tree in Data Structures Minimum Spanning Tree Permutation Sort or Bogo Sort Quick Sort vs Merge Sort Boruvkas algorithm Bubble Sort vs Quick Sort Common Operations on various Data Structures Detect and Remove Loop in a Linked List How to Start Learning DSA Print kth least significant bit number Why is Binary Heap Preferred over BST for Priority Queue Bin Packing Problem Binary Tree Inorder Traversal Burning binary tree Equal Sum What is a Threaded Binary Tree? What is a full Binary Tree? Bubble Sort vs Merge Sort B+ Tree Program in Q language Deletion Operation from A B Tree Deletion Operation of the binary search tree in C++ language Does Overloading Work with Inheritance Balanced Binary Tree Binary tree deletion Binary tree insertion Cocktail Sort Comb Sort FIFO approach Operations of B Tree in C++ Language Recaman’s Sequence Tim Sort Understanding Data Processing Applications of trees in data structures Binary Tree Implementation Using Arrays Convert a Binary Tree into a Binary Search Tree Create a binary search tree Horizontal and Vertical Scaling Invert binary tree LCA of binary tree Linked List Representation of Binary Tree Optimal binary search tree in DSA Serialize and Deserialize a Binary Tree Tree terminology in Data structures Vertical Order Traversal of Binary Tree What is a Height-Balanced Tree in Data Structure Convert binary tree to a doubly linked list Fundamental of Algorithms Introduction and Implementation of Bloom Filter Optimal binary search tree using dynamic programming Right side view of binary tree Symmetric binary tree Trim a binary search tree What is a Sparse Matrix in Data Structure What is a Tree in Terms of a Graph What is the Use of Segment Trees in Data Structure What Should We Learn First Trees or Graphs in Data Structures All About Minimum Cost Spanning Trees in Data Structure Convert Binary Tree into a Threaded Binary Tree Difference between Structured and Object-Oriented Analysis FLEX (Fast Lexical Analyzer Generator) Object-Oriented Analysis and Design Sum of Nodes in a Binary Tree What are the types of Trees in Data Structure What is a 2-3 Tree in Data Structure What is a Spanning Tree in Data Structure What is an AVL Tree in Data Structure Given a Binary Tree, Check if it's balanced

Introduction to 2D-Arrays

Two Dimensional Array

Technical Definitions

An array of arrays is a common definition for a two-dimensional array. A matrix is another name for a two-dimensional array. A matrix looks like a table with rows and columns.

What is the syntax for declaring a two-dimensional array?

Defining a 2D array is fairly similar to declaring a 1D array in terms of syntax.

Syntax.

int arr[max_rows][max_columns];

As demonstrated below, it generates the following data structure:

Two Dimensional Array

How do you get data from a 2D array?

In the same way that data may be accessed using simply an index in a one-dimensional array, the indices of the cells can be used to access the cells individually in a two-dimensional array. A single cell has two indices: one is its row number, and the other is its column number.

The value recorded in any cell of an array is stored using the following syntax —

int x = a[i][j];

The row and column indices are I and j, respectively.

Creating a 2D array —

When declaring a one-dimensional array, we don't need to mention its size, but this isn't the case with a two-dimensional array. We must define at least the row size, or the second dimension, for a 2D array.

2D array declaration syntax —

int arr[2][2] = {1,2,3,4}

In a 2D array, the number of items is always equal to (number of rows * number of columns).

Consider the following scenario:

#include<stdio.h>
void main()
{
    int array[4][4],i,j;
    for (i=0; i<4; i++)
    {
        for (j=0; j<4; j++)
        {
            printf(“Enter value for array[%d][%d]: “ i,j);
            scanf(“%d”,&array[i][j]);
        }
    }
}

One-Dimensional (1D) Array vs. Two-Dimensional (2D) Array

Concept.One-Dimensional (1D) ArrayTwo-Dimensional (2D) Array
MeaningA one-dimensional array stores a single list of related data components.A list of lists or an array of arrays is kept in a two-dimensional array.  
SizeTotal Bytes = sizeof(datatype of array variable)* size of array is the size of a one-dimensional (1D) array.Total Bytes= sizeof(datatype of array variable)* size of first index* size of second index is the size of a two-dimensional (2D) array.
DimensionA one-dimensional array (sometimes known as a 1D array) has only one dimension.The dimension of a two-dimensional (2D) array is two.
Row column matrixIn a one-dimensional (1D) array, there is no row column matrix.In a two-dimensional (2D) array, there is a row and column matrix.

Addressing of 2-D Array

Row Major Order:

In an array, row major ordering is as follows:-

Row major ordering sends subsequent components to successive memory locations, travelling across the rows and then down the columns.

If the items of an array are stored in a Row-Wise form, in simple terms. The following diagram illustrates this mapping:

For a two-dimensional row-major ordered array, the formula to determine the Address (offset) is:

Two Dimensional Array
Two Dimensional Array

Address of A[I][J] = Base Address + W * ( C * I + j)

Where Base Address is the address of an array's first element.

  • W stands for data type's weight (size).
  • C stands for the total number of columns.
  • I stands for the row number
  • J stands for the column number of the element whose address has to be determined.

Column Major Ordering:

Column-major ordering in an array is when the elements of an array are stored in a Column Wise form. As you progressed through successive memory regions in row-major ordering, the rightmost index rose the fastest. The leftmost index advances the fastest in column-major ordering.

A column-major ordered array is seen in the diagram below.

Two Dimensional Array
Two Dimensional Array

Address of A[I][J] = Base Address + W * ( R * J + I)

Where Base Address is the address of an array's first element.

  • W stands for data type's weight (size).
  • R stands for the total number of rows.
  • I stands for the row number
  • J stands for the column number of the element whose address has to be determined.



ADVERTISEMENT
ADVERTISEMENT