I can not do it, I tried to use the FOR loop and also the IF to store in the array before summing, but my knowledge in C # is scarce. and the matrix is a 2x2.
I can not do it, I tried to use the FOR loop and also the IF to store in the array before summing, but my knowledge in C # is scarce. and the matrix is a 2x2.
The result of a sum of a matrix of order 2 x 2 is the sum of the corresponding elements of their positions, for example:
Source:
To encode this in c # , it's very simple , the declaration of an array of 2 x 2 integers would be:
int[,] matrizA = new int[2, 2];
the assignment in each position:
matrizA[0, 0] = 1;
matrizA[0, 1] = 2;
matrizA[1, 0] = 3;
matrizA[1, 1] = 4;
and recovery:
int i0 = matrizA[0, 0];
int i1 = matrizA[0, 1];
int i2 = matrizA[1, 0];
int i3 = matrizA[1, 1];
A minimum example:
static void Main(string[] args)
{
int[,] matrizA = new int[2, 2];
int[,] matrizB = new int[2, 2];
matrizA[0, 0] = 1;
matrizA[0, 1] = 2;
matrizA[1, 0] = 3;
matrizA[1, 1] = 4;
matrizB[0, 0] = 1;
matrizB[0, 1] = 2;
matrizB[1, 0] = 3;
matrizB[1, 1] = 4;
int[,] matrizC = Sum(matrizA, matrizB);
View(matrizC);
System.Console.ReadKey();
}
public static int[,] Sum(int[,] a, int[,] b)
{
int[,] result = new int[a.Rank, a.Rank];
for (int i = 0; i < a.Rank; i++)
for (int j = 0; j < a.Rank; j++)
result[i, j] = a[i, j] + b[i, j];
return result;
}
public static void View(int[,] a)
{
for (int i = 0; i < a.Rank; i++)
{
for (int j = 0; j < a.Rank; j++)
System.Console.Write("{0} ", a[i, j]);
System.Console.WriteLine();
}
}
this example running ONLINE where the expected result would be:
2 4
6 8
In this code the static
sum
method is responsible for adding the corresponding elements where the first for
is the control of lines and the second for
o of columns and columns of the available matrix, and this method is executed in larger matrix only of the same amount of elements, eg > 3x3 , 4x4 , etc ., And the static
view
method will show the existing elements in an array. p>