I am very new to the programming scene and know about nothing. I have been teaching myself a little bit of ti basic. I tried to make a program that would take the shape you input, and let you find the area of it. This is what I have so far.
Code: Disp "What is your shape"
Input "S=",S
Repeat G=S
If G=TRIANGLE
Prompt B,H
(B*H)/2>A
Disp "Area is",A
If G=SQUARE
Prompt H
H*H>A
Disp "Area is",A
If G=RECTANGLE
Then
Prompt H,W
H*W>A
Disp "Area is",A
There is probably an obvious mistake with this, but for the life of me, I can't figure out how to fix it. Thank for any help.
21tmccauley wrote:
I am very new to the programming scene and know about nothing. I have been teaching myself a little bit of ti basic. I tried to make a program that would take the shape you input, and let you find the area of it. This is what I have so far.
Disp "What is your shape"
Input "S=",S
Repeat G=S
If G=TRIANGLE
Prompt B,H
(B*H)/2>A
Disp "Area is",A
If G=SQUARE
Prompt H
H*H>A
Disp "Area is",A
If G=RECTANGLE
Then
Prompt H,W
H*W>A
Disp "Area is",A
There is probably an obvious mistake with this, but for the life of me, I can't figure out how to fix it. Thank for any help.
You forgot to surround the shape with quotes when you compared G to the string. This should work.
Code:
Disp "What is your shape"
Input "S=",S
Repeat G=S
If G="TRIANGLE"
Prompt B,H
(B*H)/2>A
Disp "Area is",A
If G="SQUARE"
Prompt H
H*H>A
Disp "Area is",A
If G="RECTANGLE"
Then
Prompt H,W
H*W>A
Disp "Area is",A
When asking for help, you should state:
what your expected outcome is
and
what you current outcome is
Looking at your program, there are some glaring flaws:
- strings, not normal variables, need to be used. (You cant input a string into the "S" variable).
- you do not seem to know what the purpose of repeat is. It repeats code until the condition is (G=S) is true.
- TRIANGLE , SQUARE etc are not strings, they insdead are numbers equal to T*R*I*A*N*G*L*E, where the letters are all variables.
Dont feel bad though, tibasic is a bit hard to understand at first.
Optimized, saves a few bytes and possibly some microseconds of speed:
Code:
Disp "What is your shape?
Input "S=",Str1
If Str1="TRIANGLE
Then
Prompt B,H
(BH)/2>A
Disp "Area is ",A
End
If Str1="SQUARE
Then
Prompt H
HH>A
Disp "Area is ",A
End
If Str1="RECTANGLE
Then
Prompt H,W
HW>A
Disp "Area is ",A
End
[/quote]
Also, I would suggest using menu(), which makes your task more continent. All together, i would write the program like so:
Code:
Menu("Select shape:", "triangle", A, "SQUARE ", B, "RECTANGLE", C)
lbl A
Prompt B,H
(B*H)/2>A
goto D
lbl B
Prompt H
H*H>A
goto D
lbl C
Prompt H,W
H*W>A
lbl D
Disp "Area is",A
pause
Moar optimizations:
Code:
Menu("Select shape:", "Triangle", A, "SQUARE ", B, "RECTANGLE", C
Lbl A
Prompt B,H
(BH)/2->A
Goto D
Lbl B
Prompt H
HH->A
Goto D
Lbl C
Prompt H,W
HW->A
Lbl D
Disp "Area is
Pause A
Sorry for derailing the thread with optimizations. Have more:
Code:
Menu("Select shape:", "Triangle", A, "SQUARE ", B, "RECTANGLE", C
Lbl A
Prompt B,H
BH/2
Goto D
Lbl B
Prompt H
HH
Goto D
Lbl C
Prompt H,W
HW
Lbl D
Disp "Area is
Pause Ans
Wow, thanks for the help with the code, and the proper way to ask for some help. Really useful information, and it is crazy for the quick response!
An interesting thing you might want to look into is creating a program to calculate the area of any polygon (provided you take an array of coordinates as inputs) using the following algorithm (in pseudocode)
Code: Get a list of X and Y coordinates
If the 1st point is not repeated at the end of the list, add it
initialize an accumulator to 0
repeat the following until end of list
add the nth-1 element of the X coordinates list with the nth coordinate
subtract the nth element of the Y coordinates list from the nth-1 element
add the product of the 2 previous results to the accumulator
At the end of the list, the area is half of the accumulator
This algorithm works on all polygons, both regular, irregular, concave and convex, but not complex i.e., self-intersecting polygons. This means that you would not need a menu with a bunch of shapes, but rather just a set of coordinates, which could represent any shape within reason, so you would also not be bound to hard-coded formulas for each shape. The algorithm would be less efficient than most formulas in terms of speed, but it would be plenty fast enough to be more or less instant on-calc anyway. I guess it really depends on what you want to do with this program, but I think even just as a learning thing, it could be a fun thing to do, regardless of whether or not it would be of any use.
Here with an update on my program. I have changed it to have a menu that allows the user to select the type of shape they would like to find the area or volume for. However, when I try to run the program, I get a syntax error when I try to choose anything in the first menu. Here is the code. Any advice would be great.
ClrHome
Menu("Select Shape Type:","3D ",Z,"Polygon ",I)
Lbl I
Menu("Select shape:","Triangle ",A,"Square ",B,"Rectangle ",C,"Cirlce ",E, "Elipse ",F,"Trapiziod ",G)
Lbl A
Prompt B,H
BH/2
Goto D
Lbl B
Prompt H
HH
Goto D
Lbl C
Prompt H,W
HW
Goto D
Lbl E
Prompt R
πR^2
Goto D
Lbl F
Prompt A,B
πAB
Goto D
Lbl G
Prompt A,B,H
((A+B)/2)H
Goto D
Lbl Z
Menu("Select Object:"Cube ",H,"Cylinder ",J)
Lbl H
Prompt H
HHH
Goto Q
Lbl J
Prompt R,H
πHR^2
Goto Q
Lbl D
Disp "Area is
Pause Ans
Lbl Q
Disp "Area is
Pause Ans
at label Z, the menu looks like this:
Menu("Select Object:"Cube ",H,"Cylinder ",J)
You are missing a comma and a " so it should look like this:
Menu("Select Object:","Cube ",H,"Cylinder ",J
Note: you can leave off the close parenthesis if there is nothing to the right of the statement
As for label I, you accidentally added a space right after ,E,
Menu("Select shape:","Triangle ",A,"Square ",B,"Rectangle ",C,"Cirlce ",E, "Elipse ",F,"Trapiziod ",G)
It should look like this (I also fixed some typos):
Menu("Select shape:","Triangle ",A,"Square ",B,"Rectangle ",C,"Circle ",E,"Elipse ",F,"Trapezoid ",G
TheLastMillennial wrote:
at label Z, the menu looks like this:
Menu("Select Object:"Cube ",H,"Cylinder ",J)
You are missing a comma and a " so it should look like this:
Menu("Select Object:","Cube ",H,"Cylinder ",J
Note: you can leave off the close parenthesis if there is nothing to the right of the statement
As for label I, you accidentally added a space right after ,E,
Menu("Select shape:","Triangle ",A,"Square ",B,"Rectangle ",C,"Cirlce ",E, "Elipse ",F,"Trapiziod ",G)
It should look like this (I also fixed some typos):
Menu("Select shape:","Triangle ",A,"Square ",B,"Rectangle ",C,"Circle ",E,"Elipse ",F,"Trapezoid ",G
Thanks!!
When I try to run the program, it is still having a syntax error that leads to the line "Lbl I". No idea why.
Check for any white-space On Lbl I or anywhere else, that could be an issue. otherwise I'm not sure, you'll just have to spend some time on it yourself.
Wow, do I feel silly. It was just a white space on some of the Lbl lines.
21tmccauley wrote:
Wow, do I feel silly. It was just a white space on some of the Lbl lines.
It's ok. We all make mistakes some times. It's just part of the learning road.
PS: Circle is spelled incorrectly.
P.P.S.: Ellipse has 2 l's, so it's ellipse.
Pssht, you can put code in [code][/code] quotes.
I would like to make an option on the menu to quit the program using a Lbl. How would I do this? Is there a quit command somewhere? Thanks in advance.
Create a new menu item with your label name, then put that Lbl at either the very end of your program, or place the 'Return' or 'Halt' commands after the Lbl.