variable re-initialization not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • YouPoP
    New Member
    • Jun 2007
    • 17

    #1

    variable re-initialization not working

    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:

    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;
    
            }
    
    
        }
    }
    Many thanks in advance,

    Yolaine
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    I couldnot get what you really meant.

    But lets see if i could understand the problem my way.

    when you pass values to the Cramer class the values should be calculated - in your case, this is not happening and when it returns to the parent (ie the object which called it) no change appers!

    If thats the case ---- (i hope it is)
    It is so becauswe you are not apssing the variables as reference.
    use the ref keyword while passing parameters to your methods and the correct value (as calculated by the cramer class) should reflect

    Comment

    • YouPoP
      New Member
      • Jun 2007
      • 17

      #3
      the variable is fine from one classe to another, the problem is when i do the calculation, the matTemp does not return to the initial value of a after having B replacing the n column. at the end of the loop, a AND matTemp are filled with the B values only...

      Comment

      • Shashi Sadasivan
        Recognized Expert Top Contributor
        • Aug 2007
        • 1435

        #4
        Gotcha,

        when you do the following code

        matTemp = new int[n, n];
        matTemp = a;

        matTemp is a reference to a.

        You would have to clone a to mat temp every time

        a is object 1
        matTemp = new int[n, n]; //this creates a new object (object2) and matTemp = object 2

        matTemp = a; // this make matTemp = object1

        So so thats why everytime matTemp and a gets filled up with values of B

        Comment

        • YouPoP
          New Member
          • Jun 2007
          • 17

          #5
          this is already what I have no? Or am I missing something? because this way even a gets filled with the B values ...

          Code:
          private static int[] CalculerXi(int[,]a, int[,]b, int n)
          {
                    .......
          
                      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;
          }

          Comment

          • Shashi Sadasivan
            Recognized Expert Top Contributor
            • Aug 2007
            • 1435

            #6
            hi, what you need to do instead of this is

            matTemp = new int[n, n];
            matTemp = a;


            [CODE=cpp]matTemp = new int[n,n];
            for(int p=0,p<n;p++)
            {
            for(int pp=0,pp<n;pp++)
            {
            matTemp[p,pp] = a[p,pp];
            }
            }
            //matTemp = a; this line should be commented[/CODE]

            Comment

            • YouPoP
              New Member
              • Jun 2007
              • 17

              #7
              Found out:
              Instead of doing it in the same function
              matTemp = a
              I created a new function with your code which fills and returns the initial matrix "a" and replaces matTemp = a with matTemp = InitializeMatri x(a);

              thanks so much for your help!!!

              Yolaine

              Comment

              • Shashi Sadasivan
                Recognized Expert Top Contributor
                • Aug 2007
                • 1435

                #8
                Yes, that should have worked.

                The reason behind why a was returning as B, is because, you were changing a indirectly even though u assigned it to the temporary variable.

                To make this clearer
                see the following

                [CODE=cpp]class DataObject
                {
                public int x;
                }

                class Program()
                {
                static void Main(string[] args)
                {
                DataObject a = new DataObject();
                a.x = 10;
                DataObject b = new DataObject();
                b = a;
                Console.WriteLi ne("before changing a, value of b : " + b.x.toString()) ;
                Console.WriteLi ne("before changing a, value of a : " + a.x.toString()) ;

                a = 20;
                Console.WriteLi ne("after changing a, value of b : " + b.x.toString()) ;
                Console.WriteLi ne("after changing a, value of a : " + a.x.toString()) ;
                }
                }[/CODE]

                Output:
                before changing a, value of b : 10
                before changing a, value of a : 10
                after changing a, value of b : 20
                after changing a, value of a : 20

                hope this clears it a bit

                Comment

                Working...