Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php 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);
 

?>
Comment

PREVIOUS NEXT
Code Example
Php :: php Get location date format 
Php :: pass variable in translation larvel 
Php :: tina4 create route 
Php :: prevent undefined offset php 
Php :: How to get ID and other string in url 
Php :: how to make category for spesific post wordpress devv 
Php :: recaptcha v3 laravel 8 
Php :: install wget downloader php 
Php :: Only Show Specific Countries In Caldera Forms Phone Field 
Php :: laravel task scheduler error 
Php :: wordpress not recognizing function during plugin activation 
Php :: Calculate the remaining days on view Laravel, negative days if date has passed 
Php :: twig global 
Php :: shopware php get cookie 
Php :: laravel migration softdelete 
Php :: import csv in laravel 
Php :: php get docblock with reflection 
Php :: Display a variable containing html in laravel 
Php :: laravel convert array to string 
Php :: wordpress profile queries 
Php :: merge three array in php 
Php :: how to get last 10 digit from number in php 
Php :: php artisan spark not working in codeigniter 
Php :: PHP Validate POST value emoty & Set 
Php :: eager loading set limit to relationship 
Php :: php console lofarray values 
Php :: auth guard (admin) is not defined laravel 8 
Php :: create or update laravel 5.8 stackoverflow 
Php :: IlluminateValidationRulesRequiredIf 
Php :: laravel pdf generator 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =