Re: solving an equation multiple directions



On Jan 16, 8:34 am, William Elliot <ma...@xxxxxxxxxxxxxxxx> wrote:
On Fri, 16 Jan 2009, Jason wrote:
I want to write a function in a computer language like C or Java.
I want the function to solve for one variable then another without
writing the equation twice.

Example:
x = y + z (given y and z)
y = x - z (given x and z)

but without writing two equations to prevent duplication in my code.
Any ideas?

Are you sure you want a function?
Perhaps you want a subroutine.
More likely you need clear thinking.

What input are you giving the function or subroutine?
An equation such as z = x + y ?

What output are you expecting?
More equations such as x = y - z and y = x - z ?

What do you mean without writing the code twice?
That's what functions and subroutines are used for,
to not repleat code at the additional time cost of
calling and returning from a function or subroutine.

Thank you for replying!
What I mean is this:

I have simplified my equation for clarity.
I want to write a computer program that will solve the equation x = y
+ z.
The computer will prompt the user for 2 of the 3 variables as input to
the program.
If the user inputs y and z, then the equation is solved as x = y + z.
If the user inputs x and z, then the equation is solved as y = x - z.
If the user inputs x and y, then the equation is solved as z = x - y.

but I don't want to write a program that writes that equation 3 times
because I'm duplicating my code. I thought I remember that some
programming methodology exists that allows the equation to be solved
more dynamically. Does that make sense?

Thank you.
.