473,385 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

variable re-initialization not working

17
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:

Expand|Select|Wrap|Line Numbers
  1. public partial class Form1 : Form
  2. {
  3.         private void BTN_OK_MATRICE_Click(object sender, EventArgs e)
  4.         {
  5.             A = GenererMatrice(x, x);  // where A and B are  2d int[,] given by the user
  6.             B = GenererVecteur(x, y);
  7.  
  8.             Calculs(A, B);
  9.         }
  10.  
  11.  
  12.         private void Calculs(int[,] a, int[,] b)
  13.         {
  14.             int[,] matA = a;
  15.             int[,] vecB = b;
  16.  
  17.             // Test si la matrice est inversible
  18.             if (MatriceCalculs.TestInversible(A) == true) 
  19.             {
  20.                 Cramer.SolutionCramer(matA, vecB, n);
  21.                 //this.Hide();
  22.             }
  23.             else
  24.             {
  25.                 MessageBox.Show("pas inversible");
  26.             }
  27.  
  28.         }
  29. }
  30.  
  31. namespace Devoir2
  32. {
  33.     class Cramer
  34.     {
  35.  
  36.         public static void SolutionCramer(int[,] a, int[,] b, int n)
  37.         {
  38.             int[] x;
  39.             int determinant;
  40.              int[] Ai;
  41.             int[,] A;
  42.             int[,] B;
  43.             A = a;
  44.             B = b;
  45.             int[,] K = Form1.A;
  46.             int[] valeurs = new int[n];
  47.             valeurs = CalculerXi(A, B, n);
  48.  
  49.         }
  50.  
  51.         private static int[] CalculerXi(int[,]a, int[,]b, int n)
  52.         {
  53.             int[,] matTemp = new int[n, n];
  54.             int detAi;
  55.             int[] Ai = new int[n];
  56.             int k = 0;
  57.             int detA;
  58.             detA = MatriceCalculs.Determinant(a);
  59.  
  60.             for (int j = k; j < n; j++) // pour chaque colonne
  61.             {
  62.                 matTemp = new int[n, n];
  63.                 matTemp = a; // reinitialiser la matrice A
  64.                 for (int i = 0; i < n; i++) // pour chaque ligne
  65.                 {
  66.                     matTemp[i, j] = b[i, 0]; // echanger les items de la colonne de A avec ceux de la colonne de B
  67.                 }
  68.                 detAi = (MatriceCalculs.Determinant(matTemp)); // Calculer le determinant pour Ai
  69.                 Ai[j] = detAi / detA; // calculer xi
  70.                 k++;
  71.             }
  72.             return Ai;
  73.  
  74.         }
  75.  
  76.  
  77.     }
  78. }
  79.  
Many thanks in advance,

Yolaine
Nov 30 '07 #1
7 982
Shashi Sadasivan
1,435 Expert 1GB
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
Nov 30 '07 #2
YouPoP
17
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...
Nov 30 '07 #3
Shashi Sadasivan
1,435 Expert 1GB
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
Nov 30 '07 #4
YouPoP
17
this is already what I have no? Or am I missing something? because this way even a gets filled with the B values ...

Expand|Select|Wrap|Line Numbers
  1. private static int[] CalculerXi(int[,]a, int[,]b, int n)
  2. {
  3.           .......
  4.  
  5.             for (int j = k; j < n; j++) // pour chaque colonne
  6.             {
  7.                 matTemp = new int[n, n];
  8.                 matTemp = a; // reinitialiser la matrice A
  9.                 for (int i = 0; i < n; i++) // pour chaque ligne
  10.                 {
  11.                     matTemp[i, j] = b[i, 0]; // echanger les items de la colonne de A avec ceux de la colonne de B
  12.                 }
  13.                 detAi = (MatriceCalculs.Determinant(matTemp)); // Calculer le determinant pour Ai
  14.                 Ai[j] = detAi / detA; // calculer xi
  15.                 k++;
  16.             }
  17.             return Ai;
  18. }
  19.  
Nov 30 '07 #5
Shashi Sadasivan
1,435 Expert 1GB
hi, what you need to do instead of this is

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


Expand|Select|Wrap|Line Numbers
  1. matTemp = new int[n,n];
  2. for(int p=0,p<n;p++)
  3. {
  4.    for(int pp=0,pp<n;pp++)
  5.    {
  6.       matTemp[p,pp] = a[p,pp];
  7.    }
  8. }
  9. //matTemp = a;  this line should be commented
Nov 30 '07 #6
YouPoP
17
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 = InitializeMatrix(a);

thanks so much for your help!!!

Yolaine
Nov 30 '07 #7
Shashi Sadasivan
1,435 Expert 1GB
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

Expand|Select|Wrap|Line Numbers
  1. class DataObject
  2. {
  3.    public int x;
  4. }
  5.  
  6. class Program()
  7. {
  8.    static void Main(string[] args)
  9.    {
  10.       DataObject a = new DataObject();
  11.       a.x = 10;
  12.       DataObject b = new DataObject();
  13.       b = a;
  14.       Console.WriteLine("before changing a, value of b : " + b.x.toString());
  15.       Console.WriteLine("before changing a, value of a : " + a.x.toString());
  16.  
  17.       a = 20;
  18.       Console.WriteLine("after changing a, value of b : " + b.x.toString());
  19.       Console.WriteLine("after changing a, value of a : " + a.x.toString());
  20.    }
  21. }
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
Nov 30 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Oplec | last post by:
Hi, I thought that I understood how C++ allows for the declaration and defining of variables within an if() statement and how the declared variable can be used until the end of the major if()...
2
by: Alan Hewat | last post by:
I have a relatively modest mySQL database with a PHP interface (70,000 entries). On a standard Dell Dimension 4400 (1.9 GHz with 512 Mbytes) running Suse 8.1 a typical query takes 0.66 sec with no...
1
by: Scott | last post by:
I have an XML Document in a format like: <Variable name="Bob">ABCDEFG</Variable> <Variable name="Steve">QWERTYUI</Variable> <Variable name="John">POIUYTR</Variable> <Variable...
7
by: bartek | last post by:
Hello, I've been pondering with this for quite some time now, and finally decided to ask here for suggestions. I'm kind of confused, actually... Maybe I'm thinking too much... Brain dump...
3
by: Bryan Parkoff | last post by:
.....I try to reduce un-necessary temporal variables so it can be optimized for best performance. I try to assign only one register storage so two variables can access to only one register storage...
3
by: Anders Borum | last post by:
Hello! I've come across a strange error that occurs, when you try to return a nodelist from a variable with a choose/where/otherwise statement. I'm not quite sure whether it's a bug or simply...
9
by: Pyenos | last post by:
Approach 1: class Class1: class Class2: def __init__(self):self.variable="variable" class Class3: def method():print Class1().Class2().variable #problem Approach 1.1:
0
MMcCarthy
by: MMcCarthy | last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For...
3
by: Richard K Miller | last post by:
Here's an OOP question that perplexes me. It seems PHP doesn't treat static variables correctly in child classes. <?php class ABC { public $regular_variable = "Regular variable in ABC\n";...
37
by: minkoo.seo | last post by:
Hi. I've got a question on the differences and how to define static and class variables. AFAIK, class methods are the ones which receives the class itself as an argument, while static methods...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.