Tilted Forum Project Discussion Community

Tilted Forum Project Discussion Community (https://thetfp.com/tfp/)
-   Tilted Technology (https://thetfp.com/tfp/tilted-technology/)
-   -   [perl] Solving an algebraic equation (https://thetfp.com/tfp/tilted-technology/42820-perl-solving-algebraic-equation.html)

VF19 01-24-2004 11:22 AM

[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.

cheerios 01-24-2004 01:35 PM

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.

VF19 01-24-2004 01:53 PM

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

cheerios 01-24-2004 02:04 PM

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";
so this line. right now, it's printing text (a string) with every variable replaced with it's value. so of course it' going to print what it did. Programs ALWAYS do what you tell them to.

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"
and this is where I get to my OTHER issue with your code which is this. you need to give your variables meaningful names. pot, hot, and not have no meaning, so it's hard to read your code and understand what it's doing. if you're reading an x value call it X, or X1 or Xval or something. if you're reading hte slope, call it m, or slope. get the idea? hope that helped...

VF19 01-24-2004 03:38 PM

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

cheerios 01-24-2004 06:25 PM

here's one issue:
Code:

$xvxsl= "$xval * $slpe";

$yvpxxs= "$yval + $xvxsl";

these don't need double-quotes, because they aren't strings. ""s are used to indicate string literals. (aka thing like "bob" or "the answer is"). you can embed variables in your strings and they'll be replaced by their values, you're doing this already.

VF19 01-24-2004 07:20 PM

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";

cheerios 01-24-2004 07:55 PM

how do you USUALLY change a number to be negative? I generally multiply it by -1 ;)

ni42 01-24-2004 10:15 PM

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 ($yval, $slpe, $xval);

i put the first line in there so you can see where to put it,

cheerios 01-25-2004 12:14 AM

chomp is a function that removes trailing (and leading?) whitespaces from your input, by the way.

ni42 01-25-2004 01:39 AM

Chomp removes newlines not spaces.
Code:

my $val = "hhh      \n";
print "$val";print "h\n";
chomp ($val);
print "$val";print "h";

that will print out
Code:

hhh     
h
hhh      h

thats for 5.8, i forget about prevous versions.

reference: http://www.perldoc.com/perl5.8.0/pod/func/chomp.html

VF19 01-25-2004 10:10 AM

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";

cheerios 01-25-2004 11:13 AM

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 09:44 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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54