![]() |
[perl] Solving an algebraic equation
Hi, im a complete no0b at Perl, but I attempted to write a program that can solve the y-y=m(x-x) equation (forgot what its called).
Here it is: #!/usr/bin/perl use warnings; use strict; print "You solve the y-y=m(x-x) equation with this program. \n"; print "Enter y variable. \n"; my $cot = [STDIN]; print "Enter slope. \n"; my $hot = [STDIN]; print "Enter x variable. \n"; my $pot = [STDIN]; print "y-$cot=$hot(x-$pot) \n"; So, when I execute it, it works, no errors, I enter the slope and stuff, but it doesnt solve the equation. It's like: y-4 =2.5 (x-1 ) What could be the problem? Edit: After the variables there should be the STDIN tag thing. |
you can use tags [code] and [/ code] to encase your code so we can see it all... please...
by the way, what is is SUPPOSED to print out? it's doing EXACTLY what you're asking it to do, printing out the values you put in. |
Like I said I'm a no0b I have no clue what im doing.
I want it to solve the y-y=m(x-x) equation.(i think its called writing the equation of the line?) The point was (1,4)(x and y) and the slope was 2.5 (m) In this case, it was supposed to put out y=2.5x+1.5 |
ok, well you need to tell it to do that. your eqyation is y - y1 = m * (x - x1) you need to show the program HOW to print the equation you want.
Code:
print "y-$cot=$hot(x-$pot) \n"; what YOU want to do is break this up into steps. you always want to print "y =" right? and that will always be followed by your m value, followed by "x + " and then that last value. so you have every bit of information you need, except that last value. so, solve that generically, $b = something then change your print statement to be something like this: Code:
print "y = $m x + $b \n" |
Ok.. I got you there. I should have known I had to do the calculating and whatnot ..
Still doesn't work, but a hell of a lot closer than where I was. Here it is: #!/usr/bin/perl use warnings; use strict; print "You can solve the y-y=m(x-x) equation with this program. \n"; print "Enter y variable. \n"; my $yval = [STDIN]; print "Enter slope. \n"; my $slpe = [STDIN]; print "Enter x variable. \n"; my $xval = [STDIN]; $xvxsl= "$xval * $slpe"; $yvpxxs= "$yval + $xvxsl"; print "Step one: y-$yval=$slpe x- $xvxsl \n"; print "Step two: y-$yval+$yval=$slpv x - $yvpxxs \n"; print "Answer: y= $slpe x - $yvpxxs \n"; Got a bunch of errors, says that I need an explicit package name :confused: on lines 21-29. Any help would be appreciated |
here's one issue:
Code:
$xvxsl= "$xval * $slpe"; |
Hooray! It works!
Well sort of. The answer it shows for the (1,4) m=2.5 equation is y=2.5x-6.5, which is wrong. It's y=2.5x-1.5. Any idea on how I can change that? Like change the $yvpxxs to a negative number? Ok, so I deleted to "" signs, didnt work, but I put 'my' in front of the variable things and it worked.. Here's the working script: #!/usr/bin/perl use warnings; use strict; print "You can solve the y-y=m(x-x) equation with this program. \n"; print "Enter y variable. \n"; my $yval = [STDIN]; print "Enter slope. \n"; my $slpe = [STDIN]; print "Enter x variable. \n"; my $xval = [STDIN]; my $xvxsl= $xval * $slpe ; my $yvpxxs= $yval + $xvxsl ; print "Step one: y-$yval=$slpe x-$xvxsl \n"; print "Step two: y-$yval+$yval=$slpe x-$yvpxxs \n"; print "Answer: y= $slpe x-$yvpxxs \n"; |
how do you USUALLY change a number to be negative? I generally multiply it by -1 ;)
|
chomp chomp chomp
as a general rule of thumb you should always chomp everything you get from STDIN. heres an example that you can put into your code.
Code:
my $xval = <STDIN>; |
chomp is a function that removes trailing (and leading?) whitespaces from your input, by the way.
|
Chomp removes newlines not spaces.
Code:
my $val = "hhh \n"; Code:
hhh reference: http://www.perldoc.com/perl5.8.0/pod/func/chomp.html |
Oh ok.. Thanks for that chomp function, really makes it easier to read the output.
Yeah, I just multiplied $yvpxxs by -1. Now it's: #!/usr/bin/perl use warnings; use strict; print "You can solve the y-y=m(x-x) equation with this program. \n"; print "Enter y variable. \n"; my $yval = [STDIN]; print "Enter slope. \n"; my $slpe = [STDIN]; print "Enter x variable. \n"; my $xval = [STDIN]; chomp ($yval, $slpe, $xval); my $xvxsl= $xval * $slpe ; my $yvpxxs= $yval + $xvxsl *-1 ; print "Step one: y-$yval=$slpe x-$xvxsl \n"; print "Step two: y-$yval+$yval=$slpe x-$yvpxxs \n"; print "Answer: y= $slpe x-$yvpxxs \n"; |
ni42: I confess, I was too lazy to find my perl book and look it up. thx for the clarification! :)
|
All times are GMT -8. The time now is 12:29 PM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project