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


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 03:12 AM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, 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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360