Thread: Simultaneous linear Equations

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    3

    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();
            
            }
        }
    }

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    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;

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    Would some one Put the Whole code please?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by dvd4alll View Post
    Would some one Put the Whole code please?
    Just you. WE can help to fix errors...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    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();
            
            }
        }
    }

  7. #7
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    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.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  8. #8
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    Well, you can test the code if it works, and if it works, it's working. Not so hard?

  9. #9
    Registered User AloneInTheDark's Avatar
    Join Date
    Feb 2008
    Posts
    74
    Quote Originally Posted by Livijn View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read coefficients of linear equations from file into 2d array
    By omaralqady in forum C++ Programming
    Replies: 6
    Last Post: 06-20-2009, 07:39 AM
  2. LINEAR EQUATIONS please help
    By apple_ranger in forum C Programming
    Replies: 18
    Last Post: 09-08-2008, 05:49 AM
  3. LUP Decomposition (simultaneous equations)
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 08-24-2003, 02:08 AM
  4. simultaneous equations
    By nic_elmo_13 in forum C Programming
    Replies: 3
    Last Post: 04-14-2003, 08:11 AM
  5. Solving linear equations
    By PJYelton in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2002, 06:00 PM