Created
October 30, 2019 00:31
-
-
Save letsjustfixit/3362f1a0b8824bcf08900882b9cb8d12 to your computer and use it in GitHub Desktop.
Clipped MagicSquare.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace ConsoleApp1 | |
{ | |
class Program | |
{ | |
//rows contains the row sums, cols contains the col sums | |
static void sumsGeneration(int[,] m, int[] rows,int[] cols) | |
{ | |
for(int i=0; i < rows.Length; i++) | |
{ | |
for(int j=0; j < cols.Length; j++) | |
{ | |
rows[i] += m[i, j]; | |
cols[j] += m[i, j]; | |
} | |
} | |
}//*** | |
static string print2d(int[,] matrix) | |
{ | |
string s = ""; | |
for(int i=0; i< matrix.GetLength(0); i++) | |
{ | |
for(int j=0; j < matrix.GetLength(1); j++) | |
{ | |
s += matrix[i, j].ToString().PadLeft(8); | |
} | |
s += "\n"; | |
} | |
return s; | |
}//*** | |
static string print1d(int[] vector) | |
{ | |
string s = ""; | |
for (int i = 0; i < vector.Length; i++) | |
s += vector[i] + " "; | |
return s; | |
}//*** | |
static bool allEqual(int[] t)//retruns true iff all elements of t are equal | |
{ | |
int x = t[0]; | |
foreach(int a in t) | |
{ | |
if (x != a) return false; | |
} | |
return true; | |
}//*** | |
static int diag1(int[,] matrix)//computes main diag sum | |
{ | |
int sum = 0; | |
for(int i=0; i< matrix.GetLength(0); i++) | |
{ | |
sum += matrix[i, i]; | |
} | |
return sum; | |
}//*** | |
static int diag2(int[,] matrix)//computes skew diag sum | |
{ | |
int sum = 0; | |
int L = matrix.GetLength(0); | |
for (int i = 0; i < L; i++) | |
{ | |
sum += matrix[i, L - 1 - i]; | |
} | |
return sum; | |
}//*** | |
static bool isMagicSquare(int[,] A) | |
{ | |
int[] rows = new int[A.GetLength(0)]; | |
int[] cols = new int[A.GetLength(1)]; | |
sumsGeneration(A, rows, cols); | |
if (allEqual(rows) == false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment