Access elements of Matrix Data Structure
Like one-dimensional arrays, matrices can be accessed randomly by using their indices to access the individual elements. A cell has two indices, one for its row number, and the other for its column number. We can use arr[i][j] to access the element which is at the ith row and jth column of the matrix.
#include <iostream>
using namespace std;
int main()
{
// Initializing a 2-D array with values
int arr[3][3]
= { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
// Accessing elements of 2-D array
cout << "First element of first row: " << arr[0][0]
<< "\n";
cout << "Third element of second row: " << arr[1][2]
<< "\n";
cout << "Second element of third row: " << arr[2][1]
<< "\n";
return 0;
}
#include <stdio.h>
int main()
{
// Initializing a 2-D array with values
int arr[3][3]
= { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
// Accessing elements of 2-D array
printf("First element of first row: %d\n", arr[0][0]);
printf("Third element of second row: %d\n", arr[1][2]);
printf("Second element of third row: %d\n", arr[2][1]);
return 0;
}
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
// Initializing a 2-D array with values
int[][] arr
= { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
// Accessing elements of 2-D array
System.out.println("First element of first row: "
+ arr[0][0]);
System.out.println("Third element of second row: "
+ arr[1][2]);
System.out.println("Second element of third row: "
+ arr[2][1]);
}
}
# Initializing a 2-D array with values
arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# Accessing elements of 2-D array
print("First element of first row:", arr[0][0])
print("Third element of second row:", arr[1][2])
print("Second element of third row:", arr[2][1])
using System;
public class GFG {
static public void Main()
{
// Initializing a 2-D array with values
int[, ] arr
= { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
// Accessing elements of 2-D array
Console.WriteLine("First element of first row: "
+ arr[0, 0]);
Console.WriteLine("Third element of second row: "
+ arr[1, 2]);
Console.WriteLine("Second element of third row: "
+ arr[2, 1]);
}
}
// Initializing a 2-D array with values
let arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
// Accessing elements of 2-D array
console.log("First element of first row: " + arr[0][0]);
console.log("Third element of second row: " + arr[1][2]);
console.log("Second element of third row: " + arr[2][1]);
Introduction to Matrix or Grid Data Structure – Two Dimensional Array
Matrix or Grid is a two-dimensional array mostly used in mathematical and scientific calculations. It is also considered as an array of arrays, where array at each index has the same size. In this article, we will cover all the basics of Matrix, the Operations on Matrix, its implementation, advantages, disadvantages which will help you solve all the problems based on Matrix Data Structure.
Table of Content
- What is a Matrix Data Structure?
- Representation of Matrix Data Structure
- Declaration of Matrix Data Structure
- Initializing Matrix Data Structure
- Operations on Matrix Data Structure
- Access elements of Matrix Data Structure
- Traversal of a Matrix Data Structure
- Searching in a Matrix Data Structure
- Sorting Matrix Data Structure
- Applications of Matrix Data Structure
- Advantages of Matrix Data Structure
- Disadvantages of Matrix Data Structure
- More Practice problems on Matrix Data Structure