Thread: LINEAR EQUATIONS please help

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    10

    Angry LINEAR EQUATIONS please help

    i have been assigned a question asking me to compile a C program to solve any 2x2 linear equations. i'm very bad at c programming , so can anyone give me some pointers to how i should start my codes? thanks

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Well assuming 2x2 means, 2 variables (say x & y) and 2 equations...

    You can do it a few ways, either substitution or elimination -- google simultaneous equations. Or you can do it with a bit of matrix maths, which of course is good if you need to go to say a n x m system of equations.

    Do you have to parse the equations? If so, to what level? Otherwise you could just ask for equations in a certain form ie:

    ax + yb + c = d

    Enter a: ...
    Enter b: ...
    Enter c: ...
    Enter d: ...

    Same for equation 2

    Incase you didn't notice... start with planning then design, etc. But perhaps first work out what the actual requirements are, such as: do I need to parse math equations? Do I need to supply a menu? etc
    Last edited by zacs7; 09-02-2008 at 07:36 AM.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Are you saying that you are to input

    ax + by + c = d
    and
    ex + fy + g = h

    If so, you need not do too much since you can just solve via elimination (or at least if it were my assignment I would write the code that way). What do you need to do, and what have you tried so far? This isn't a homework for hire site. You can't afford most of us (I cost you your sanity.. and so do laserlight, Elysia, Salem, Prelude, and CornedBee).

    I know you learn all kinds of pesky vocabulary in math class. You learn it so that when you are describing a homework assignment on a programming forum we can all know what the hell it is that you are even speaking about. You say 2x2... and I think you are talking about a matrix of some sort. Yet you say linear equation... and I think of a linear equation...

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I think the OP means systems of equations in two variables.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    10
    okok let me explain this again , i have been asked to create a set of codes to solve linear equations " ax + by = c1" and " cx + dy = c2". some people suggested that i should use gaussian elimination , but as i'm only a newbie in computer class, i dont' think its that difficult, so i'm guessing it might be just using methods of elimination or substitution. And yes i get all the methods of how to solve simutaneous equations and matrix , but i have no idea on how to start writing my codes or what C codes i'm meant to use O.O" so can someone please start me on the coding of my program.... thanks so much

  6. #6
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    That is sort of a loaded question and we have no idea where to start. Can you successfully write/compile/run a program that prints "Hello world!"?

    If not, start with a C tutorial by searching the internet for said tutorial.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    10
    yes of course i can write a simple program like that

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    What noops was suggesting was that you show some effort. Write a piece of code that does something towards the direction you need to go and post for help when you get stuck. Help will follow.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  9. #9
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    You said you had no idea where to start but it appears you at least know how to compile and run a basic program. That's good.

    You also said you understand how to solve linear equations so that's covered.

    Ask a specific question and you will get an answer.

    Example: I want to write a program to solve linear equations using matrices but I am not sure how you represent a matrix in C. Also, how do you perform operations on a matrix?

    We can't squeeze blood from a stone.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Posts
    10
    Quote Originally Posted by noops View Post
    You said you had no idea where to start but it appears you at least know how to compile and run a basic program. That's good.

    You also said you understand how to solve linear equations so that's covered.

    Ask a specific question and you will get an answer.

    Example: I want to write a program to solve linear equations using matrices but I am not sure how you represent a matrix in C. Also, how do you perform operations on a matrix?

    We can't squeeze blood from a stone.

    SORRY i'm very new to computer programming, BUT yes that example u put there was exactly what i wanted to ask. i was planning to solve my linear equations using either substitution method or the elimination method or just a system of matrix, but i wasnt' sure how to represent the matrix in C or how to perform the operation on the matrix in C . i know how to perform it on paper though so can u please give me some pointers?
    Last edited by apple_ranger; 09-05-2008 at 05:36 AM.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Matrices are a special case of arrays -- are you familiar with array notation in C? The matrix element a_ij would be represented as a[i][j]. (Note that arrays start at 0, so you'll probably have to either put your formulas like that, or remember to take one off when you put them in your program.)

    And generally, if you know how to do it on paper, then that's what you do in C -- if you want y = a_00x_0+a_10x_1, then you type in y = a[0][0]*x[0]+a[1][0]*x[1] and be done with it.

  12. #12
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    And you have to do operations on your matrix manually. So there is no method to say "add row 2 to row 3".

    You will want to look up arrays and 2 dimensional arrays in C.

  13. #13
    Registered User
    Join Date
    Sep 2008
    Posts
    10
    oh no.. i just checked my text book and realised that i've never learnt arrays before. it is at the back of my text book but i've only started learning C programming. Is there other ways of solving a two variable linear equations without using arrays?

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by apple_ranger View Post
    oh no.. i just checked my text book and realised that i've never learnt arrays before. it is at the back of my text book but i've only started learning C programming. Is there other ways of solving a two variable linear equations without using arrays?
    Well, if you're willing to write the equations as ax+by=c and dx+ey=f, you can call the variables a, b, c, d, e, and f. (I would certainly put a comment in the program about which variable means what.)

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    If you know how to solve a 2x2 system, then the actual algorithm should be easy once you have all the parts of the equation.

    For input reading, it seems most expressive to just parse the equations but depending on what the assignment calls for, that could be overkill.

    Basically if the user types in say

    u + 2v -16 = 1
    and
    2u -4v + 2 = 4

    You need to correctly store the variables and their coefficients, the constants, and the operations for each equation before you can solve it (by using elimination, or however you choose). There are algorithms for this sort of thing, called expression parsers. One example is called the shunting yard algorithm.
    Last edited by whiteflags; 09-06-2008 at 09:34 AM.

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. solution to a linear system of equations
    By e4321 in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2009, 09:00 PM
  3. Simultaneous linear Equations
    By dvd4alll in forum C# Programming
    Replies: 8
    Last Post: 02-08-2008, 07:11 PM
  4. Solving linear equations
    By PJYelton in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2002, 06:00 PM
  5. Linear equations
    By amandad40 in forum C Programming
    Replies: 1
    Last Post: 10-29-2002, 04:54 AM