📜  使用单循环打印二维数组或矩阵

📅  最后修改于: 2021-09-02 07:35:17             🧑  作者: Mango

给定一个N * M维的矩阵mat[][] ,任务是使用单个 for 循环打印矩阵的元素。

例子:

方法:要使用单个循环遍历给定矩阵,请注意只有N * M 个元素。因此,想法是使用模数和除法来切换行和列,同时在[0, N * M]范围内迭代单个循环。请按照以下步骤解决给定的问题:

  • 使用变量i在范围[0, N * M] 上迭代一个循环。
  • 在每次迭代中,分别找到当前行和列的索引为row = i / Mcolumn = i % M。
  • 在上述步骤中,打印mat[row][column]的值以获取该索引处的矩阵值。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to print the element
// of 2D matrix using single loop
void print2DMatrix(int arr[][3],
                   int rows, int columns)
{
     
    // Iterate over the range
    // [0, rows*columns]
 
    for(int i = 0; i < rows * columns; i++)
    {
         
        // Find row and column index
        int row = i / columns;
        int col = i % columns;
 
        // Print the element at
        // current index
        cout << arr[row][col] << " ";
    }
}
 
// Driver Code
int main()
{
     
    // Given matrix mat[][]
    int mat[][3] = { { 1, 2, 3 },
                     { 4, 5, 6 },
                     { 7, 8, 9 } };
 
    // Dimensions of the matrix
    int N = sizeof(mat) / sizeof(mat[0]);
    int M = sizeof(mat[0]) / sizeof(mat[0][0]);
 
    // Function Call
    print2DMatrix(mat, N, M);
    return 0;
}
 
// This code is contributed by akhilsaini


Java
// Java Program for the above approach
 
import java.io.*;
 
class GFG {
 
    // Function to print the element
    // of 2D matrix using single loop
    public static void print2DMatrix(
        int arr[][], int rows, int columns)
    {
 
        // Iterate over the range
        // [0, rows*columns]
        for (int i = 0;
             i < rows * columns; i++) {
 
            // Find row and column index
            int row = i / columns;
            int col = i % columns;
 
            // Print the element at
            // current index
            System.out.print(
                arr[row][col] + " ");
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given matrix mat[][]
        int[][] mat = { { 1, 2, 3 },
                        { 4, 5, 6 },
                        { 7, 8, 9 } };
 
        // Dimensions of the matrix
        int N = mat.length;
        int M = mat[0].length;
 
        // Function Call
        print2DMatrix(mat, N, M);
    }
}


Python3
# Python3 program for the above approach
 
# Function to print the element
# of 2D matrix using single loop
def print2DMatrix(arr, rows, columns):
   
  # Iterate over the range
  # [0, rows*columns]
  for i in range(0, rows * columns):
     
    # Find row and column index
    row = i // columns
    col = i % columns
 
    # Print the element at
    # current index
    print(arr[row][col], end = ' ')
     
# Driver Code
if __name__ == '__main__':
   
  # Given matrix mat[][]
  mat = [ [ 1, 2, 3 ],
          [ 4, 5, 6 ],
          [ 7, 8, 9 ] ]
   
  # Dimensions of the matrix
  N = len(mat)
  M = len(mat[0])
 
  # Function Call
  print2DMatrix(mat, N, M)
 
# This code is contributed by akhilsaini


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to print the element
// of 2D matrix using single loop
public static void print2DMatrix(int[, ] arr, int rows,
                                 int columns)
{
     
    // Iterate over the range
    // [0, rows*columns]
    for(int i = 0; i < rows * columns; i++)
    {
         
        // Find row and column index
        int row = i / columns;
        int col = i % columns;
 
        // Print the element at
        // current index
        Console.Write(arr[row, col] + " ");
    }
}
 
// Driver Code
public static void Main()
{
     
    // Given matrix mat[][]
    int[, ] mat = { { 1, 2, 3 },
                    { 4, 5, 6 },
                    { 7, 8, 9 } };
 
    // Dimensions of the matrix
    int N = mat.GetLength(0);
    int M = mat.GetLength(1);
 
    // Function Call
    print2DMatrix(mat, N, M);
}
}
 
// This code is contributed by akhilsaini


Javascript


输出:
1 2 3 4 5 6 7 8 9

时间复杂度: O(N * M)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live