Print a 2D Array or Matrix using single loop
Last Updated :
08 Jun, 2021
Given a matrix mat[][] of N * M dimensions, the task is to print the elements of the matrix using a single for loop.
Examples:
Input: mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
Output: 1 2 3 4 5 6 7 8 9
Input: mat[][] = {{7, 9}, {10, 34}, {12, 15}}
Output: 7 9 10 34 12 15
Approach: To traverse the given matrix using a single loop, observe that there are only N * M elements. Therefore, the idea is to use modulus and division to switch the rows and columns while iterating a single loop over the range [0, N * M]. Follow the steps below to solve the given problem:
- Iterate a loop over the range [0, N * M] using the variable i.
- At each iteration, find the index of the current row and column as row = i / M and column = i % M respectively.
- In the above steps, print the value of mat[row][column] to get the value of the matrix at that index.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
void print2DMatrix( int arr[][3],
int rows, int columns)
{
for ( int i = 0; i < rows * columns; i++)
{
int row = i / columns;
int col = i % columns;
cout << arr[row][col] << " " ;
}
}
int main()
{
int mat[][3] = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
int N = sizeof (mat) / sizeof (mat[0]);
int M = sizeof (mat[0]) / sizeof (mat[0][0]);
print2DMatrix(mat, N, M);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void print2DMatrix(
int arr[][], int rows, int columns)
{
for ( int i = 0 ;
i < rows * columns; i++) {
int row = i / columns;
int col = i % columns;
System.out.print(
arr[row][col] + " " );
}
}
public static void main(String[] args)
{
int [][] mat = { { 1 , 2 , 3 },
{ 4 , 5 , 6 },
{ 7 , 8 , 9 } };
int N = mat.length;
int M = mat[ 0 ].length;
print2DMatrix(mat, N, M);
}
}
|
Python3
def print2DMatrix(arr, rows, columns):
for i in range ( 0 , rows * columns):
row = i / / columns
col = i % columns
print (arr[row][col], end = ' ' )
if __name__ = = '__main__' :
mat = [ [ 1 , 2 , 3 ],
[ 4 , 5 , 6 ],
[ 7 , 8 , 9 ] ]
N = len (mat)
M = len (mat[ 0 ])
print2DMatrix(mat, N, M)
|
C#
using System;
class GFG{
public static void print2DMatrix( int [, ] arr, int rows,
int columns)
{
for ( int i = 0; i < rows * columns; i++)
{
int row = i / columns;
int col = i % columns;
Console.Write(arr[row, col] + " " );
}
}
public static void Main()
{
int [, ] mat = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
int N = mat.GetLength(0);
int M = mat.GetLength(1);
print2DMatrix(mat, N, M);
}
}
|
Javascript
<script>
function print2DMatrix(arr,rows,columns)
{
for (let i = 0; i < rows * columns; i++)
{
let row = parseInt(i / columns, 10);
let col = i % columns;
document.write(arr[row][col] + " " );
}
}
let mat = [ [ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ] ];
let N = mat.length;
let M = mat[0].length;
print2DMatrix(mat, N, M);
</script>
|
Output:
1 2 3 4 5 6 7 8 9
Time Complexity: O(N * M)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...