Traversal of a Matrix Data Structure
We can traverse all the elements of a matrix or two-dimensional array by using two for-loops.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int arr[3][4] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 } };
// Traversing over all the rows
for (int i = 0; i < 3; i++) {
// Traversing over all the columns of each row
for (int j = 0; j < 4; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
return 0;
}
#include <stdio.h>
int main()
{
int arr[3][4] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 } };
// Traversing over all the rows
for (int i = 0; i < 3; i++) {
// Traversing over all the columns of each row
for (int j = 0; j < 4; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main(String[] args)
{
int[][] arr = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 } };
// Traversing over all the rows
for (int i = 0; i < 3; i++) {
// Traversing over all the columns of each row
for (int j = 0; j < 4; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
// This code is contributed by lokesh
arr = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
# Traversing over all the rows
for i in range(0, 3):
# Traversing over all the columns of each row
for j in range(0, 4):
print(arr[i][j], end=" ")
print("")
using System;
public class GFG {
static public void Main()
{
int[, ] arr = new int[3, 4] { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 } };
// Traversing over all the rows
for (int i = 0; i < 3; i++) {
// Traversing over all the columns of each row
for (int j = 0; j < 4; j++) {
Console.Write(arr[i, j]);
Console.Write(" ");
}
Console.WriteLine(" ");
}
}
}
// This code is contributed by akashish__
// JS code for above approach
let arr = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12]];
// Traversing over all the rows
for (let i = 0; i < 3; i++) {
let s="";
// Traversing over all the columns of each row
for (let j = 0; j < 4; j++) {
s+=(arr[i][j]+" ");
}
console.log(s);
}
// This code is contributed by ishankhandelwals.
Output
1 2 3 4 5 6 7 8 9 10 11 12
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
Contact Us