Hi,
I am new to coding and to C# and I am experiencing this problem:
I have to use the Cramer's Rule to solve linear equation systems given by the users (I am using a windows form). Once the data is filles in the matrix, i send this matrix for the determinants calculations to solve the systems.
But whenever i try to calculate the determinants it does not work because the vector B is supposed to replace one column of A at a time and then calculate the determinant, but in my case the matrix A does not return back to its initial data even if I initialize it before or after attributing the B vector. (CalculerXi(... )).
Also, even if i try to do a switch with the a parameter passed to CalculerXi(int[,] a...)
matTemp = new int[n, n];
matTemp = a; // reinitialiser la matrice A
the matTemp variable got changed through the calculations because B replace every columns of it at the end of the calculation , but also the a parameter!!
I tried many different things up to now including global variables and private variables in both classes, pass it through different functions, switch variables and so on, I am kind of desperate here...
Here is my code:
Many thanks in advance,
Yolaine
I am new to coding and to C# and I am experiencing this problem:
I have to use the Cramer's Rule to solve linear equation systems given by the users (I am using a windows form). Once the data is filles in the matrix, i send this matrix for the determinants calculations to solve the systems.
But whenever i try to calculate the determinants it does not work because the vector B is supposed to replace one column of A at a time and then calculate the determinant, but in my case the matrix A does not return back to its initial data even if I initialize it before or after attributing the B vector. (CalculerXi(... )).
Also, even if i try to do a switch with the a parameter passed to CalculerXi(int[,] a...)
matTemp = new int[n, n];
matTemp = a; // reinitialiser la matrice A
the matTemp variable got changed through the calculations because B replace every columns of it at the end of the calculation , but also the a parameter!!
I tried many different things up to now including global variables and private variables in both classes, pass it through different functions, switch variables and so on, I am kind of desperate here...
Here is my code:
Code:
public partial class Form1 : Form
{
private void BTN_OK_MATRICE_Click(object sender, EventArgs e)
{
A = GenererMatrice(x, x); // where A and B are 2d int[,] given by the user
B = GenererVecteur(x, y);
Calculs(A, B);
}
private void Calculs(int[,] a, int[,] b)
{
int[,] matA = a;
int[,] vecB = b;
// Test si la matrice est inversible
if (MatriceCalculs.TestInversible(A) == true)
{
Cramer.SolutionCramer(matA, vecB, n);
//this.Hide();
}
else
{
MessageBox.Show("pas inversible");
}
}
}
namespace Devoir2
{
class Cramer
{
public static void SolutionCramer(int[,] a, int[,] b, int n)
{
int[] x;
int determinant;
int[] Ai;
int[,] A;
int[,] B;
A = a;
B = b;
int[,] K = Form1.A;
int[] valeurs = new int[n];
valeurs = CalculerXi(A, B, n);
}
private static int[] CalculerXi(int[,]a, int[,]b, int n)
{
int[,] matTemp = new int[n, n];
int detAi;
int[] Ai = new int[n];
int k = 0;
int detA;
detA = MatriceCalculs.Determinant(a);
for (int j = k; j < n; j++) // pour chaque colonne
{
matTemp = new int[n, n];
matTemp = a; // reinitialiser la matrice A
for (int i = 0; i < n; i++) // pour chaque ligne
{
matTemp[i, j] = b[i, 0]; // echanger les items de la colonne de A avec ceux de la colonne de B
}
detAi = (MatriceCalculs.Determinant(matTemp)); // Calculer le determinant pour Ai
Ai[j] = detAi / detA; // calculer xi
k++;
}
return Ai;
}
}
}
Yolaine
Comment