📜  第一个元素为奇数的二维矩阵的列总和

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

第一个元素为奇数的二维矩阵的列总和

给定一个矩阵mat[][] ,任务是打印第一个元素为奇数的列的总和

例子:

方法:初始化sum = 0并开始遍历第一行。如果第一行中的任何元素是奇数,则将该列的所有元素添加到总和中。最后打印总和

下面是上述方法的实现:

C++
// CPP implementation of the approach
#include
using namespace std;
 
// Function to return the sum of those columns
// of a given matrix that start with an odd element
int sumColumns(int arr[][4], int r, int c)
{
     
    // Initialize sum to 0
    int sum = 0;
     
    // Traverse through all the elements of the first row
    for (int j = 0; j < c; j++)
    {
        // If the element is odd
        if (arr[0][j] % 2 != 0)
        {
            // Add all the elements of that column
            for (int i = 0; i < r; i++)
                sum += arr[i][j];
        }
             
             
    }
 
    // Return the sum
    return sum;
 
}
     
// Driver code
int main()
{
int arr[3][4] = {{8, 2, 3, 5},{9, 8, 7, 6}, {1, 2, 5, 5}};
int r = sizeof(arr)/sizeof(arr[0]);
int c = sizeof(arr[0])/sizeof(int);
cout<<(sumColumns(arr, 3, 4));
}
 
// This code is contributed by
// Surendra_Gangwar


Java
// Java implementation of the approach
import java.io.*;
 
class GFG
{
     
// Function to return the sum of those columns
// of a given matrix that start with an odd element
static int sumColumns(int arr[][], int r, int c)
{
     
    // Initialize sum to 0
    int sum = 0;
     
    // Traverse through all the
    // elements of the first row
    for (int j = 0; j < c; j++)
    {
        // If the element is odd
        if (arr[0][j] % 2 != 0)
        {
            // Add all the elements of that column
            for (int i = 0; i < r; i++)
                sum += arr[i][j];
        }
    }
 
    // Return the sum
    return sum;
}
     
// Driver code
public static void main (String[] args)
{
    int arr[][] = {{8, 2, 3, 5},{9, 8, 7, 6}, {1, 2, 5, 5}};
    System.out.println(sumColumns(arr, 3, 4));
}
}
 
// This code is contributed by
// shs..


Python3
# Python3 implementation of the approach
 
# Function to return the sum of those columns
# of a given matrix that start with an odd element
def sumColumns(arr, r, c):
     
    # Initialize sum to 0
    sum = 0
     
    # Traverse through all the elements of the first row
    for j in range(c):
         
        # If the element is odd
        if (arr[0][j] % 2 != 0):
             
            # Add all the elements of that column
            for i in range(r):
                sum += arr[i][j]
 
    # Return the sum
    return sum
 
# Driver code
arr = [ [8, 2, 3, 5], [9, 8, 7, 6], [1, 2, 5, 5] ]
r = len(arr)
c = len(arr[0])
print(sumColumns(arr, r, c))


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
    // Function to return the sum of those columns
    // of a given matrix that start with an odd element
    static int sumColumns(int [,]arr, int r, int c)
    {
         
        // Initialize sum to 0
        int sum = 0;
         
        // Traverse through all the
        // elements of the first row
        for (int j = 0; j < c; j++)
        {
            // If the element is odd
            if (arr[0, j] % 2 != 0)
            {
                // Add all the elements of that column
                for (int i = 0; i < r; i++)
                    sum += arr[i, j];
            }
        }
     
        // Return the sum
        return sum;
    }
         
    // Driver code
    public static void Main()
    {
        int [,]arr= {{8, 2, 3, 5}, {9, 8, 7, 6}, {1, 2, 5, 5}};
        Console.WriteLine(sumColumns(arr, 3, 4));
    }
}
 
// This code is contributed by aishwarya.27


PHP


Javascript


输出:
31