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.

+ Code Snippet
remstart
######################################
##  Pythagorean Theorem Calculator  ##
##  ==============================  ##
##  Created By: Gold Falcon         ##
######################################
remend

do
    cls
	input "What is the height of the triangle (A)? ";A
	input "What is the base of the triangle (B)? ";B
	input "If you have it, what is the hypotenuse (C)? ";C
	
	if a$<>"?" and b$<>"?"
    	A = A^2
    	B = B^2
    	C = C^2
    
		C = A+B
	
		A = sqrt(A)
		B = sqrt(B)
		C = sqrt(A+B)
	
		cls
		print "Therefore;"
		print "The height (A) is equal to [";a;"]"
		print "The base (B) is equal to [";b;"]"
		print "*** The hypotenuse (C) is equal to [";c;"]"
		print "------------------------"
		input "Do you wish to quit or continue? ";action$
		
		if action$ = "quit"
			end
		endif
		
		if action$ = "continue"
			`Do Nothing
		endif
	endif
loop
Posted: 18th Oct 2013 8:10
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:
+ Code Snippet
A = A^2
B = B^2

A = sqrt(A)
B = sqrt(B)
C = sqrt(A+B)


Should be this:
+ Code Snippet
A = A^2
B = B^2

C = sqrt(A+B)
A = sqrt(A)
B = sqrt(B)


Also, this code will always return true because those variables are never defined, thus always being an empty string which is not equal to "?".
+ Code Snippet
if a$<>"?" and b$<>"?"


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.
+ Code Snippet
do
    cls
    print "Right-triangle Calculator"
    print "-------------------------"
    input "What is the length of side A? ", A#
    input "What is the length of side B? ", B#
    input "What is the length of the hypotenuse? ", C#
    
    rem if user didn't specify hypotenuse value, 
    rem assume both legs were entered
    if C# = 0
        C# = sqrt(A#^2 + B#^2)
    rem Hypotenuse was given, calculate missing leg length
    else
        rem User supplied hypotenuse and leg B
        if A# = 0
            A# = sqrt(C#^2 - B#^2)
        rem User supplied hypotenuse and leg A
        else
            B# = sqrt(C#^2 - A#^2)
        endif
    endif
    
    
    print ""
    print "Answer:"
    print "The length of leg A is: ", A#
    print "The length of leg A is: ", B#
    print "The length of the hypotenuse is: ", C#
    print ""

    rem Repeat the question until the user provides an acceptable answer
    repeat
        input "Do you wish to quit or continue (Y/N)? ", action$
    until upper$(action$) = "N" or upper$(action$) = "Y"
    
    rem Quit the program
    if upper$(action$) = "N" then end
    
loop
Posted: 19th Oct 2013 1: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
Posted: 20th Oct 2013 10:13
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.
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
Posted: 5th Nov 2013 19:19
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.

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.