# Simultaneous linear Equations

• 01-29-2008
dvd4alll
Simultaneous linear Equations
I need to make a Console Application which can calculate the values of the Constants " X " and " Y " for the 2 equations ( ax + by = c) and ( dx + ey = f ) .. Of Course the program will ask the user to enter the values for the variables ( a , b , c , d , e , f ). I've tried to make some code, but got stuck at the core of the code. Would anybody please help me?

my Code:

Code:

```using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _1st_Exercise {     class Program     {         static void Main(string[] args)         {             //Getting the constants             Console.WriteLine("\"dx + ey = f\" ,, \"ax +by = c\"\n");             Console.WriteLine("Please enter\n");             Console.Write("F= ");             int F;             F = Convert.ToInt32(Console.ReadLine());             Console.Write("\nE= ");             int E;             E = Convert.ToInt32(Console.ReadLine());             Console.Write("\nD= ");             int D;             D = Convert.ToInt32(Console.ReadLine());             Console.Write("\nC= ");             int C;             C = Convert.ToInt32(Console.ReadLine());             Console.Write("\nB= ");             int B;             B = Convert.ToInt32(Console.ReadLine());             Console.Write("\nA= ");             int A;             A = Convert.ToInt32(Console.ReadLine());                         //Code core             int x;             x = (A - D);                         int y;             y = (B - E);                                     //Output             Console.WriteLine("\n\nX = " + x );             Console.WriteLine("\nY = " + y );                                                             //Exit program             Console.WriteLine("\n\nPress any key to exit");             Console.ReadKey();                 }     } }```
• 01-29-2008
mike_g
You can brute force it in a nested for loop by running through, and checking, each combination of X and Y. Probably not the fastest way to do this though. To give you a rough idea:

Code:

```for(y=0; y<100; y++)     for(x=0; x<100; x++)         if((A*x + B*y) == C)             if((D*x + E*y) == F)                 break;```
• 01-29-2008
dvd4alll
Would some one Put the Whole code please?
• 01-29-2008
vart
And what about solving the equations manually and implement it?
D*A*x + D*B*y == D*C
A*D*x + A*E*y == A*F
(D*B-A*E)*y == D*C - A*F

so if (D*B-A*E) != 0
y = (D*C - A*F) / (D*B-A*E)

same goes for x

in the else case - you have 2 possibilities: lines are the same (infinite number of solutions)
or - 2 line have no common point - so no solution is available
• 01-29-2008
vart
Quote:

Originally Posted by dvd4alll
Would some one Put the Whole code please?

Just you. WE can help to fix errors...
• 01-29-2008
dvd4alll
That's my new Code .. is it ok ?

Code:

```using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _1st_Exercise {     class Program     {         static void Main(string[] args)         {             //Getting the constants             Console.WriteLine("\"dx + ey = f\" ,, \"ax +by = c\"\n");             Console.WriteLine("Please enter\n");             Console.Write("F= ");             int F;             F = Convert.ToInt32(Console.ReadLine());             Console.Write("\nE= ");             int E;             E = Convert.ToInt32(Console.ReadLine());             Console.Write("\nD= ");             int D;             D = Convert.ToInt32(Console.ReadLine());             Console.Write("\nC= ");             int C;             C = Convert.ToInt32(Console.ReadLine());             Console.Write("\nB= ");             int B;             B = Convert.ToInt32(Console.ReadLine());             Console.Write("\nA= ");             int A;             A = Convert.ToInt32(Console.ReadLine());                         //Code core             for (int y = 0; y < 100; y++)             {                 for (int x = 0; x < 100; x++)                 {                     if ((A * x + B * y) == C)                     {                         if ((D * x + E * y) == F)                         {                             //Output                             Console.WriteLine("\n\nX = " + x);                             Console.WriteLine("\nY = " + y);                         }                     }                 }             }                                                             //Exit program             Console.WriteLine("\n\nPress any key to exit");             Console.ReadKey();                 }     } }```
• 02-04-2008
NeonBlack
I wrote something like this a few years ago in regular C that would solve a system of n equations with n unknowns using Cramer's rule. (Requires a little bit of linear algebra, but it's not too bad).

But if you're always going to have 2 equations and 2 unknowns, it is much simpler and you should be able to hard-code the solution as vart suggested. I don't suggest brute-forcing for a number of reasons including inefficiency and the cases where x and y are not integers.
• 02-05-2008
Livijn
Well, you can test the code if it works, and if it works, it's working. Not so hard?
• 02-08-2008
AloneInTheDark
Quote:

Originally Posted by Livijn
Well, you can test the code if it works, and if it works, it's working. Not so hard?

haha, that was one fun/good thing you said there. I will use it in the future.