Find the Sum of All Elements in a Matrix

You are given a matrix of size R*C containing integers. Write a program to find the sum of all elements present in the matrix.

Note: Make sure to read the input matrix correctly and find the sum of all elements in the matrix as specified in the question.

Boundary Condition(s):
2 <= R, C <= 50
-1000 <= Each integer value in the matrix <= 1000

Input Format:
The first line contains two space-separated integers R and C, representing the number of rows and columns in the matrix.
The next R lines contain C integers each, representing the elements of the matrix.

Output Format:
The program should output a single integer, which is the sum of all elements in the matrix.

Example Input/Output 1:
Input:
3 4
5 10 25 8
12 9 7 6
11 14 18 22

Output:
170

Explanation:
To find the sum of all elements in this matrix, we need to go through each element and add its value to the sum.

Example Input/Output 2:
Input:
4 3
30 40 5
16 28 35
55 90 15
10 25 60

Output:
384

Algorithm:

  1. Initialize a variable sum to store the sum of elements and set it to 0.
  2. Iterate through each row i from 0 to R-1 and for each row: a. Iterate through each column j from 0 to C-1 and for each column:
  3. Add the value of the current element matrix[i][j] to the sum.
  4. After the loop completes, the variable sum will hold the sum of all elements in the matrix.
  5. Return the value of sum.

Here’s the algorithm expressed in pseudocode:

function findSumOfElements(matrix, R, C):
    sum = 0
    for i from 0 to R-1:
        for j from 0 to C-1:
            sum += matrix[i][j]
    return sum
def find_sum_of_elements(matrix, R, C):
    # Initialize sum to store the sum of elements
    sum = 0

    # Iterate through each row
    for i in range(R):
        # Iterate through each element in the row
        for j in range(C):
            # Add the current element to the sum
            sum += matrix[i][j]

    return sum

if __name__ == "__main__":
    # Read the number of rows (R) and columns (C)
    R, C = map(int, input().split())

    # Read the matrix elements
    matrix = [list(map(int, input().split())) for _ in range(R)]

    # Call the function to find the sum of all elements in the matrix
    result = find_sum_of_elements(matrix, R, C)

    # Print the result (sum of all elements)
    print(result)
#include <stdio.h>

// Function to find the sum of all elements in a matrix
int find_sum_of_elements(int matrix[][50], int R, int C) {
    // Initialize sum to store the sum of elements
    int sum = 0;

    // Iterate through each row
    for (int i = 0; i < R; i++) {
        // Iterate through each element in the row
        for (int j = 0; j < C; j++) {
            // Add the current element to the sum
            sum += matrix[i][j];
        }
    }

    return sum;
}

int main() {
    int R, C;
    scanf("%d %d", &R, &C);

    // Declare a 2D array to store the matrix elements
    int matrix[50][50];

    // Read the matrix elements
    for (int i = 0; i < R; i++) {
        for (int j = 0; j < C; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }

    // Call the function to find the sum of all elements in the matrix
    int result = find_sum_of_elements(matrix, R, C);

    // Print the result (sum of all elements)
    printf("%dn", result);

    return 0;
}
#include <iostream>
using namespace std;

// Function to find the sum of all elements in a matrix
int find_sum_of_elements(int matrix[][50], int R, int C) {
    // Initialize sum to store the sum of elements
    int sum = 0;

    // Iterate through each row
    for (int i = 0; i < R; i++) {
        // Iterate through each element in the row
        for (int j = 0; j < C; j++) {
            // Add the current element to the sum
            sum += matrix[i][j];
        }
    }

    return sum;
}

int main() {
    int R, C;
    cin >> R >> C;

    // Declare a 2D array to store the matrix elements
    int matrix[50][50];

    // Read the matrix elements
    for (int i = 0; i < R; i++) {
        for (int j = 0; j < C; j++) {
            cin >> matrix[i][j];
        }
    }

    // Call the function to find the sum of all elements in the matrix
    int result = find_sum_of_elements(matrix, R, C);

    // Print the result (sum of all elements)
    cout << result << endl;

    return 0;
}
import java.util.Scanner;

public class Main {
    public static int findSumOfElements(int[][] matrix, int R, int C) {
        // Initialize sum to store the sum of elements
        int sum = 0;

        // Iterate through each row
        for (int i = 0; i < R; i++) {
            // Iterate through each element in the row
            for (int j = 0; j < C; j++) {
                // Add the current element to the sum
                sum += matrix[i][j];
            }
        }

        return sum;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int R = scanner.nextInt();
        int C = scanner.nextInt();

        // Declare a 2D array to store the matrix elements
        int[][] matrix = new int[R][C];

        // Read the matrix elements
        for (int i = 0; i < R; i++) {
            for (int j = 0; j < C; j++) {
                matrix[i][j] = scanner.nextInt();
            }
        }

        // Call the function to find the sum of all elements in the matrix
        int result = findSumOfElements(matrix, R, C);

        // Print the result (sum of all elements)
        System.out.println(result);
        scanner.close();
    }
}
using System;

class Program {
    static int FindSumOfElements(int[,] matrix, int R, int C) {
        // Initialize sum to store the sum of elements
        int sum = 0;

        // Iterate through each row
        for (int i = 0; i < R; i++) {
            // Iterate through each element in the row
            for (int j = 0; j < C; j++) {
                // Add the current element to the sum
                sum += matrix[i, j];
            }
        }

        return sum;
    }

    static void Main(string[] args) {
        // Read the number of rows (R) and columns (C)
        string[] input = Console.ReadLine().Split();
        int R = int.Parse(input[0]);
        int C = int.Parse(input[1]);

        // Declare a 2D array to store the matrix elements
        int[,] matrix = new int[R, C];

        // Read the matrix elements
        for (int i = 0; i < R; i++) {
            input = Console.ReadLine().Split();
            for (int j = 0; j < C; j++) {
                matrix[i, j] = int.Parse(input[j]);
            }
        }

        // Call the function to find the sum of all elements in the matrix
        int result = FindSumOfElements(matrix, R, C);

        // Print the result (sum of all elements)
        Console.WriteLine(result);
    }
}
Previous Article

Function printXAlphabetsCircularly – CTS PATTERN

Next Article

String Letters Frequency

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *