📜  查找给定二维数组中元素总和的程序

📅  最后修改于: 2022-05-13 01:57:21.717000             🧑  作者: Mango

查找给定二维数组中元素总和的程序

给定一个 M * N 阶的二维数组,任务是找出矩阵元素的总和。

例子:

方法:

可以通过遍历矩阵并将元素相加来计算二维数组的每个元素的总和。

以下是实施上述方法 -

C++
// C++ program to find sum of
// elements in 2D array
#include 
using namespace std;
 
// Get the size m and n
#define M 4
#define N 4
 
// Function to calculate sum
// of elements in 2d array
int sum(int arr[M][N])
{
    int i, j;
    int sum = 0;
 
    // Finding the sum
    for (i = 0; i < M; ++i) {
        for (j = 0; j < N; ++j) {
            // Add the element
            sum = sum + arr[i][j];
        }
    }
    return sum;
}
 
// Driver code
int main()
{
    int i, j;
    int arr[M][N];
 
    // Get the matrix elements
    int x = 1;
    for (i = 0; i < M; i++)
        for (j = 0; j < N; j++)
            arr[i][j] = x++;
 
    // Get sum
    cout << sum(arr);
    return 0;
}


Java
// Java code for the above approach
import java.io.*;
 
class GFG {
  // Get the size m and n
  static int M = 4;
  static int N = 4;
 
  // Function to calculate sum
  // of elements in 2d array
  static int sum(int arr[][])
  {
    int i, j;
    int sum = 0;
 
    // Finding the sum
    for (i = 0; i < M; ++i) {
      for (j = 0; j < N; ++j) {
        // Add the element
        sum = sum + arr[i][j];
      }
    }
    return sum;
  }
 
  public static void main (String[] args)
  {
    int i, j;
    int arr[][]= new int[M][N];
 
    // Get the matrix elements
    int x = 1;
    for (i = 0; i < M; i++)
      for (j = 0; j < N; j++)
        arr[i][j] = x++;
 
    // Get sum
    System.out.println(sum(arr));
  }
}
 
// This code is contributed by Potta Lokesh


Python3
# phython program to find sum of
# elements in 2D array
 
# Get the size m and n
M = 4
N = 4
 
# Function to calculate sum
# of elements in 2d array
def sum(arr):
 
    sum = 0
 
    # Finding the sum
    for i in range(M):
        for j in range(N):
           
            # Add the element
            sum = sum + arr[i][j]
 
    return sum
 
# Driver code
arr = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
 
# Get the matrix elements
x = 1
for i in range(M):
    for j in range(N):
        arr[i][j] = x
        x += 1
 
# Get sum
print(sum(arr))
 
# This code is contributed by ninja_hattori


C#
using System;
 
class GFG
{
 
  // Get the size m and n
  static int M = 4;
  static int N = 4;
 
  // Function to calculate sum
  // of elements in 2d array
  static int sum(int [,]arr)
  {
    int i, j;
    int sum = 0;
 
    // Finding the sum
    for (i = 0; i < M; ++i)
    {
      for (j = 0; j < N; ++j)
      {
 
        // Add the element
        sum = sum + arr[i,j];
      }
    }
    return sum;
  }
 
  static public void Main (String[] args){
    int i, j;
    int [,]arr= new int[M,N];
 
    // Get the matrix elements
    int x = 1;
    for (i = 0; i < M; i++)
      for (j = 0; j < N; j++)
        arr[i,j] = x++;
 
    // Get sum
    Console.WriteLine(sum(arr));
    // Code
  }
}
// This code is contributed by Kritima Gupta


Javascript



输出
136

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