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
Printable View
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
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
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...
I think the OP means systems of equations in two variables.
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
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.
yes of course i can write a simple program like that
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
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?
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.
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.
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?
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.