Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] - Pythagorean Theorem Calculator

Author
Message
Gold Falcon
11
Years of Service
User Offline
Joined: 1st Oct 2013
Location:
Posted: 16th Oct 2013 23:10
I'm just starting to learn and work with DBPro, so I figured I'd try making my own little Pythagorean Theorem Calculator, using mathematical functions.

To calculate the hypotenuse, one will take the Height^2 + Base^2 which will equal Hypotenuse^2. Now just do the exponents of the height and base, and square root the Hypotenuse to get the answer. This is all summed up in "A^2 + B^2 = C^2". This little script that I wrote does just that, all you have to do is punch in the height and the base, and it will be calculated for you.

Please don't give un-constructive criticism or hate, as this is my first script with DBPro. I would however appreciate some feedback and constructive criticism.

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 18th Oct 2013 08:10
Quote: "all you have to do is punch in the height and the base, and it will be calculated for you."

Nope, sorry but it won't.

Your code doesn't really make a lot of sense nor is it a calculator since all 3 values need to be provided by the user.

You have:
C = C^2

Followed by:
C = A+B

Therefore making the first statement pointless. All the code really does is take all 3 values and returns the square root of the squared values, or in other words returning the same numbers the user already entered. Your hypotenuse value is taking the square root of the sum of the legs, when it should be taking the sum of the squared legs.

This:


Should be this:


Also, this code will always return true because those variables are never defined, thus always being an empty string which is not equal to "?".


What you should do, is allow the user to give 2 of the sides and let the program calculate the 3rd. So the user provides either one leg and the hypotenuse or just two legs. I would also recommend making the variables floats instead of integers if you wish to use decimal values.

Also, the program loops continuously unless the user specifically types "quit". Anything else will continue onward, making the conditional check for "continue" rather unnecessary. Provide information to the user so they know what proper values are accepted. Typically, something like this would accept either Y or N for such a question.

I'm not trying to sound offensive, but did you test your program? Because it simply won't return the correct value for C like you're expecting.


Here's some code to study.


Gold Falcon
11
Years of Service
User Offline
Joined: 1st Oct 2013
Location:
Posted: 19th Oct 2013 01:52
I take no offense, but I did test the script and it seemed to work for all 3 of the equations I tested. Anyways, thanks for the advice, just trying to learn DBPro a little bit and thought trying to find a hypotenuse was a fun way to put together what I've learned so far into an actual script (not being a "hello world" script).

About the continue/quit message, yeah I forgot to put an else statement
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 20th Oct 2013 10:13
Quote: "but I did test the script and it seemed to work for all 3 of the equations I tested."

Can I ask which equations you tested?

If we take a look at your code, you're only taking the square root of A+B when both of those should be squared first, then added together before taking the square root.

Though you've probably already figured this out, the help file doesn't mention this; You can use other data types with than Input command other than integers. Such as the case in my example where I used floats instead.

Gold Falcon
11
Years of Service
User Offline
Joined: 1st Oct 2013
Location:
Posted: 20th Oct 2013 16:59
I believe I tested equations: Base = 4cm Height = 5cm. I can't exactly remember as I don't have the sheet of paper with me at the moment. About the only taking the square root of A+B, before that I did A = A^2 etc (DBPro is procedural isn't it?). Anyways thanks for the help, need to learn somehow
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 5th Nov 2013 19:19
Quote: "I believe I tested equations: Base = 4cm Height = 5cm."


Your code gives the wrong answer in that case too. Try A = 3, B = 4 if you want a simple test.

Quote: "About the only taking the square root of A+B, before that I did A = A^2 etc"


No you didn't. Read your code again - and then study Phaelax's code snippets again.

Also, you should be using floats, i.e. variables with decimal places, if you want more accurate non-integer answers.



Powered by Free Banners

Login to post a reply

Server time is: 2024-11-21 15:55:44
Your offset time is: 2024-11-21 15:55:44