Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 01-24-2004, 11:22 AM   #1 (permalink)
Banned
 
Location: back to my old location
[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.

Last edited by VF19; 01-24-2004 at 01:53 PM..
VF19 is offline  
Old 01-24-2004, 01:35 PM   #2 (permalink)
Banned
 
Location: 'bout 2 feet from my iMac
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.

Last edited by cheerios; 01-24-2004 at 01:38 PM..
cheerios is offline  
Old 01-24-2004, 01:53 PM   #3 (permalink)
Banned
 
Location: back to my old location
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
VF19 is offline  
Old 01-24-2004, 02:04 PM   #4 (permalink)
Banned
 
Location: 'bout 2 feet from my iMac
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...
cheerios is offline  
Old 01-24-2004, 03:38 PM   #5 (permalink)
Banned
 
Location: back to my old location
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
on lines 21-29. Any help would be appreciated
VF19 is offline  
Old 01-24-2004, 06:25 PM   #6 (permalink)
Banned
 
Location: 'bout 2 feet from my iMac
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.
cheerios is offline  
Old 01-24-2004, 07:20 PM   #7 (permalink)
Banned
 
Location: back to my old location
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";
VF19 is offline  
Old 01-24-2004, 07:55 PM   #8 (permalink)
Banned
 
Location: 'bout 2 feet from my iMac
how do you USUALLY change a number to be negative? I generally multiply it by -1
cheerios is offline  
Old 01-24-2004, 10:15 PM   #9 (permalink)
Upright
 
Location: BC, Canada
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,
__________________
- 42 -
ni42 is offline  
Old 01-25-2004, 12:14 AM   #10 (permalink)
Banned
 
Location: 'bout 2 feet from my iMac
chomp is a function that removes trailing (and leading?) whitespaces from your input, by the way.
cheerios is offline  
Old 01-25-2004, 01:39 AM   #11 (permalink)
Upright
 
Location: BC, Canada
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
__________________
- 42 -
ni42 is offline  
Old 01-25-2004, 10:10 AM   #12 (permalink)
Banned
 
Location: back to my old location
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";
VF19 is offline  
Old 01-25-2004, 11:13 AM   #13 (permalink)
Banned
 
Location: 'bout 2 feet from my iMac
ni42: I confess, I was too lazy to find my perl book and look it up. thx for the clarification!
cheerios is offline  
 

Tags
algebraic, equation, perl, solving

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 09:42 PM.

Tilted Forum Project

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