Search
 
SCRIPT & CODE EXAMPLE
 

CPP

Prefix Sum of Matrix (Or 2D Array)

// C++ Program to find prefix sum of 2d array
#include <bits/stdc++.h>
using namespace std;
 
#define R 4
#define C 5
 
// calculating new array
void prefixSum2D(int a[][C])
{
    int psa[R][C];
    psa[0][0] = a[0][0];
 
    // Filling first row and first column
    for (int i = 1; i < C; i++)
        psa[0][i] = psa[0][i - 1] + a[0][i];
    for (int i = 1; i < R; i++)
        psa[i][0] = psa[i - 1][0] + a[i][0];
 
    // updating the values in the cells
    // as per the general formula
    for (int i = 1; i < R; i++) {
        for (int j = 1; j < C; j++)
 
            // values in the cells of new
            // array are updated
            psa[i][j] = psa[i - 1][j] + psa[i][j - 1]
                        - psa[i - 1][j - 1] + a[i][j];
    }
 
    // displaying the values of the new array
    for (int i = 0; i < R; i++) {
        for (int j = 0; j < C; j++)
            cout << psa[i][j] << " ";
        cout << "
";
    }
}
 
// driver code
int main()
{
    int a[R][C] = { { 1, 1, 1, 1, 1 },
                    { 1, 1, 1, 1, 1 },
                    { 1, 1, 1, 1, 1 },
                    { 1, 1, 1, 1, 1 } };
 
    prefixSum2D(a);
 
    return 0;
}
Comment

Prefix Sum of Matrix (Or 2D Array)

// Java program to find prefix sum of 2D array
import java.util.*;
 
class GFG {
 
    // calculating new array
    public static void prefixSum2D(int a[][])
    {
        int R = a.length;
        int C = a[0].length;
 
        int psa[][] = new int[R][C];
 
        psa[0][0] = a[0][0];
 
        // Filling first row and first column
        for (int i = 1; i < C; i++)
            psa[0][i] = psa[0][i - 1] + a[0][i];
        for (int i = 1; i < R; i++)
            psa[i][0] = psa[i - 1][0] + a[i][0];
 
        // updating the values in the
        // cells as per the general formula.
        for (int i = 1; i < R; i++)
            for (int j = 1; j < C; j++)
 
                // values in the cells of new array
                // are updated
                psa[i][j] = psa[i - 1][j] + psa[i][j - 1]
                            - psa[i - 1][j - 1] + a[i][j];
 
        for (int i = 0; i < R; i++) {
            for (int j = 0; j < C; j++)
                System.out.print(psa[i][j] + " ");
            System.out.println();
        }
    }
 
    // driver code
    public static void main(String[] args)
    {
        int a[][] = { { 1, 1, 1, 1, 1 },
                      { 1, 1, 1, 1, 1 },
                      { 1, 1, 1, 1, 1 },
                      { 1, 1, 1, 1, 1 } };
        prefixSum2D(a);
    }
}
Comment

Prefix Sum of Matrix (Or 2D Array)

# Python Program to find
# prefix sum of 2d array
R = 4
C = 5
 
# calculating new array
def prefixSum2D(a) :
    global C, R
    psa = [[0 for x in range(C)]
              for y in range(R)]
    psa[0][0] = a[0][0]
 
    # Filling first row
    # and first column
    for i in range(1, C) :
        psa[0][i] = (psa[0][i - 1] +
                       a[0][i])
    for i in range(0, R) :
        psa[i][0] = (psa[i - 1][0] +
                       a[i][0])
 
    # updating the values in
    # the cells as per the
    # general formula
    for i in range(1, R) :
        for j in range(1, C) :
 
            # values in the cells of
            # new array are updated
            psa[i][j] = (psa[i - 1][j] +
                         psa[i][j - 1] -
                         psa[i - 1][j - 1] +
                           a[i][j])
 
    # displaying the values
    # of the new array
    for i in range(0, R) :
        for j in range(0, C) :
            print (psa[i][j],
                   end = " ")
        print ()
 
# Driver Code
a = [[ 1, 1, 1, 1, 1 ],
     [ 1, 1, 1, 1, 1 ],
     [ 1, 1, 1, 1, 1 ],
     [ 1, 1, 1, 1, 1 ]]
 
prefixSum2D(a)
 
# This code is contributed by
# Manish Shaw(manishshaw1)
Comment

Prefix Sum of Matrix (Or 2D Array)

// C# program to find prefix
// sum of 2D array
using System;
 
class GFG
{
 
    // calculating new array
    static void prefixSum2D(int [,]a)
    {
        int R = a.GetLength(0);
        int C = a.GetLength(1);
 
        int [,]psa = new int[R, C];
 
        psa[0, 0] = a[0, 0];
 
        // Filling first row
        // and first column
        for (int i = 1; i < C; i++)
            psa[0, i] = psa[0, i - 1] +
                               a[0, i];
        for (int i = 1; i < R; i++)
            psa[i, 0] = psa[i - 1, 0] +
                               a[i, 0];
  
        // updating the values in the
        // cells as per the general formula.
        for (int i = 1; i < R; i++)
            for (int j = 1; j < C; j++)
 
                // values in the cells of
                // new array are updated
                psa[i, j] = psa[i - 1, j] +
                            psa[i, j - 1] -
                            psa[i - 1, j - 1] +
                            a[i, j];
 
        for (int i = 0; i < R; i++)
        {
            for (int j = 0; j < C; j++)
                Console.Write(psa[i, j] + " ");
            Console.WriteLine();
        }
    }
 
    // Driver Code
    static void Main()
    {
        int [,]a = new int[,]{{1, 1, 1, 1, 1},
                              {1, 1, 1, 1, 1},
                              {1, 1, 1, 1, 1},
                              {1, 1, 1, 1, 1}};
        prefixSum2D(a);
    }
}
 
// This code is contributed by manishshaw1
Comment

Prefix Sum of Matrix (Or 2D Array)

<?php
// PHP Program to find
// prefix sum of 2d array
$R = 4;
$C = 5;
 
// calculating new array
function prefixSum2D($a)
{
    global $C, $R;
    $psa = array();
    $psa[0][0] = $a[0][0];
 
    // Filling first row
    // and first column
    for ($i = 1; $i < $C; $i++)
        $psa[0][$i] = $psa[0][$i - 1] +
                             $a[0][$i];
    for ($i = 0; $i < $R; $i++)
        $psa[$i][0] = $psa[$i - 1][0] +
                             $a[$i][0];
 
    // updating the values in
    // the cells as per the
    // general formula
    for ($i = 1; $i < $R; $i++)
    {
        for ($j = 1; $j < $C; $j++)
 
            // values in the cells of
            // new array are updated
            $psa[$i][$j] = $psa[$i - 1][$j] +
                           $psa[$i][$j - 1] -
                           $psa[$i - 1][$j - 1] +
                           $a[$i][$j];
    }
 
    // displaying the values
    // of the new array
    for ($i = 0; $i < $R; $i++)
    {
        for ($j = 0; $j < $C; $j++)
            echo ($psa[$i][$j]. " ");
        echo ("
");
    }
}
 
// Driver Code
$a = array(array( 1, 1, 1, 1, 1 ),
           array( 1, 1, 1, 1, 1 ),
           array( 1, 1, 1, 1, 1 ),
           array( 1, 1, 1, 1, 1 ));
 
prefixSum2D($a);
 
// This code is contributed by
// Manish Shaw(manishshaw1)
?>
Comment

Prefix Sum of Matrix (Or 2D Array)

<script>
// Javascript program to find prefix sum of 2D array
 
// calculating new array
function prefixSum2D(a)
{
    let R = a.length;
        let C = a[0].length;
  
        let psa = new Array(R);
        for(let i = 0; i < R; i++)
        {
            psa[i] = new Array(C);
            for(let j = 0; j < C; j++)
                psa[i][j] = 0;
        }   
  
        psa[0][0] = a[0][0];
  
        // Filling first row and first column
        for (let i = 1; i < C; i++)
            psa[0][i] = psa[0][i - 1] + a[0][i];
        for (let i = 1; i < R; i++)
            psa[i][0] = psa[i - 1][0] + a[i][0];
  
        // updating the values in the
        // cells as per the general formula.
        for (let i = 1; i < R; i++)
            for (let j = 1; j < C; j++)
  
                // values in the cells of new array
                // are updated
                psa[i][j] = psa[i - 1][j] + psa[i][j - 1]
                            - psa[i - 1][j - 1] + a[i][j];
  
        for (let i = 0; i < R; i++) {
            for (let j = 0; j < C; j++)
                document.write(psa[i][j] + " ");
            document.write("<br>");
        }
}
 
// driver code
let a=[[ 1, 1, 1, 1, 1 ],
                      [ 1, 1, 1, 1, 1 ],
                      [ 1, 1, 1, 1, 1 ],
                      [ 1, 1, 1, 1, 1 ]];
prefixSum2D(a);
 
// This code is contributed by avanitrachhadiya2155
</script>
Comment

python Prefix Sum of Matrix (Or 2D Array)

# Python Program to find
# prefix sum of 2d array
R = 4
C = 5
 
# calculating new array
def prefixSum2D(a) :
    global C, R
    psa = [[0 for x in range(C)]
              for y in range(R)]
    psa[0][0] = a[0][0]
 
    # Filling first row
    # and first column
    for i in range(1, C) :
        psa[0][i] = (psa[0][i - 1] +
                       a[0][i])
    for i in range(0, R) :
        psa[i][0] = (psa[i - 1][0] +
                       a[i][0])
 
    # updating the values in
    # the cells as per the
    # general formula
    for i in range(1, R) :
        for j in range(1, C) :
 
            # values in the cells of
            # new array are updated
            psa[i][j] = (psa[i - 1][j] +
                         psa[i][j - 1] -
                         psa[i - 1][j - 1] +
                           a[i][j])
 
    # displaying the values
    # of the new array
    for i in range(0, R) :
        for j in range(0, C) :
            print (psa[i][j],
                   end = " ")
        print ()
 
# Driver Code
a = [[ 1, 1, 1, 1, 1 ],
     [ 1, 1, 1, 1, 1 ],
     [ 1, 1, 1, 1, 1 ],
     [ 1, 1, 1, 1, 1 ]]
 
prefixSum2D(a)
Comment

PREVIOUS NEXT
Code Example
Cpp :: coin change top-down 
Cpp :: KL/wweiok#L['.[- 
Cpp :: https://www.cplusplus.com/doc/tutorial/pointers/ 
Cpp :: why wont a function stop C++ 
Cpp :: overwrite windows mbr c++ 
Cpp :: C++ Multilevel Inheritance 
Cpp :: c++ terinary operator 
Cpp :: c++ check if cin got the wrong type 
Cpp :: ue4 c++ enum variable declaration 
Cpp :: c++ reverse bits 
Cpp :: Print value of data in c++ 
Cpp :: stack algorithm in c++ 
Cpp :: c++ system() from variable 
Cpp :: can you add a bool and an int 
Cpp :: Corong_ExerciseNo3(1) 
Cpp :: c++ tuple example 
Cpp :: how to find the sum of elements in a stack in cpp 
Cpp :: std remove example 
Cpp :: how to refresh multiple command lines in C++ stream 
Cpp :: Tricky Subset Problem 
Cpp :: initialize many variablles c++ 
Cpp :: ros pointcloud2 read_points c++ 
Cpp :: generate consecutive numbers at compile time 
Cpp :: private static c++ 
Cpp :: split the array there is an array val of n integers . A good subarray is defined as 
Cpp :: how to define a node in c++ 
Cpp :: map update field elixir 
Cpp :: constant qualifier c++ "error display" 
Cpp :: get array size 
Cpp :: dualSort 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =