Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# 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);
    }
}
 
Comment

PREVIOUS NEXT
Code Example
Csharp :: fix autofill issue asp.net mvc 
Csharp :: erewt 
Csharp :: function to accept interger 
Csharp :: linq query to fetch parent child data from same table in c# 
Csharp :: insert button in c# 
Csharp :: constant interpolated string 
Csharp :: c# iterate xml 
Csharp :: How to return a list to view after foreach in c# 
Csharp :: unity repeat coroutine 
Csharp :: save and query mongodb collection as dynamic ExpandoObject 
Csharp :: c# wpf control to windw size 
Csharp :: save string to file c# 
Csharp :: psobject get service name 
Csharp :: add RowDefinition from cs xamarin 
Csharp :: classe padre figlio c# 
Csharp :: c# skip debug attribute 
Csharp :: générer un nombre aléatoire en c# 
Csharp :: remove multiple element on list from index a to b C# 
Csharp :: c# Lucene search - build index 
Csharp :: c# object to xmldocument 
Csharp :: Filter list contents with predicate (anonymous method) 
Csharp :: c# how to use or operator on integers in while loop 
Csharp :: c# Jarray tryparse 
Csharp :: unity eventtrigger blocks scrollview 
Csharp :: c# zeitverzögerung 
Csharp :: how to do multiplication with button c# 
Csharp :: when creating a new boolean column in an existing table how to set the default value as true in c# models code first 
Csharp :: f# list map function 
Csharp :: accord.io read .mat file 
Csharp :: using randomly chars to build a string 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =