Bug: An elusive creature living in a program that makes it incorrect. The activity of "debugging", or removing bugs from a program, ends when people get tired of doing it, not when the bugs are removed.

I'm working as Project Lead in Oracle. I'm mainly into Microsoft technologies but quite interested in Java and C. I'm not a professional blogger, whatever I find interesting I read it and write a blog post just to share my understanding.
View all posts by Sunil Kumar

Published

One thought on “Diagonal Difference code in C#”

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {

static void Main(String[] args) {
// converts n to a usable integer (n dictates how many rows & columns there will be in the matrix)
int n = Convert.ToInt32(Console.ReadLine());

// Matrix 2d array creator (int[,] creates a multidimensional array)
//new int[n,n] <- calls a new 2d array which is n "rows" by n "columns"
int[,] matrix = new int[n, n];

// Simple counter loop increments to n – the number of rows
for(int numRows = 0; numRows < n; ++numRows)
{
// reads case and delineates it by spaces
string[] strArr = Console.ReadLine().Split(' ');

// counter loop increments to n – number of columns
for (int numCols = 0; numCols 0)
{
// While it decrements n, it selects @ x,y = 2,2 / 1,1 / 0,0
leftDiagTotal += matrix[left, left];
// While it decrements n, it selects @ x,y = 0,2 / 1,1 / 2,0
rightDiagTotal += matrix[left++, right–];
}
// Math.Abs
int ans = (Math.Abs(leftDiagTotal – rightDiagTotal));
Console.WriteLine(ans);
}
}

// My own breakdown of the code above with research into every little thing

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

class Solution {

static void Main(String[] args) {

// converts n to a usable integer (n dictates how many rows & columns there will be in the matrix)

int n = Convert.ToInt32(Console.ReadLine());

// Matrix 2d array creator (int[,] creates a multidimensional array)

//new int[n,n] <- calls a new 2d array which is n "rows" by n "columns"

int[,] matrix = new int[n, n];

// Simple counter loop increments to n – the number of rows

for(int numRows = 0; numRows < n; ++numRows)

{

// reads case and delineates it by spaces

string[] strArr = Console.ReadLine().Split(' ');

// counter loop increments to n – number of columns

for (int numCols = 0; numCols 0)

{

// While it decrements n, it selects @ x,y = 2,2 / 1,1 / 0,0

leftDiagTotal += matrix[left, left];

// While it decrements n, it selects @ x,y = 0,2 / 1,1 / 2,0

rightDiagTotal += matrix[left++, right–];

}

// Math.Abs

int ans = (Math.Abs(leftDiagTotal – rightDiagTotal));

Console.WriteLine(ans);

}

}

// My own breakdown of the code above with research into every little thing

LikeLike