Tired of calculating vector cross products or unit vectors by hand? This handy program allows you to input two vectors and add or subtract them, or take the dot or cross product, and receive the resulting vector along with its magnitude and unit vector (or just magnitude in the case of the dot product). This program can help save a lot of time on tests or homework in classes that deal with vectors such as physics or calculus. Continue reading for a walk-through of how to create the program yourself, or press the button below to jump directly to the finished code.

### Creating the Program

To create a program, press the *prgm* button and scroll over to **NEW**. You can name it whatever you like, but it’s wise to name it something related to its function. I will name it VECTORS.

### Coding the Program

To begin, we need to prompt the user for the values of the two vectors. Use the *Input* command to collect the x, y, and z values for two vectors (u and v). Before doing so however, we should clear the home screen and erase all stored lists because we will be storing stuff there later. The *ClrAllLists* command can be found in the catalog (*2nd* → *0*).

`: ClrHome`

: ClrAllLists

: Input "U1= ",A

: Input "U2= ",B

: Input "U3= ",C

: Disp ""

: Input "V1= ",D

: Input "V2= ",E

: Input "V3= ",F

Once the user inputs the vector values, we should clear the home screen and prompt which mathematical operation they want to perform. Do this by creating a menu. The *Menu(* command is found by pressing *prgm* while in the code editor. Inside the menu, first create a title, and then add each menu option followed by a variable to be used as a label. Make sure you are using unique variables for each option. To mark the start location for each label, use the *Lbl* command found under *prgm* as well. Here we can perform the necessary calculations to solve for each operation. Since 3 of the 4 options produces a vector, we can print that value at the end of the program to save memory. We can instruct the program to jump to a different place in the code using the *Goto* command to jump to a different label. Because the dot product produces a scalar value, we can print the value right away and then stop the program using the *Stop* command.

`: ClrHome`

: Menu("OPERATION","U+V",G,"U-V",H,"U•V",I,"U*V",J)

: Lbl G

: {A,B,C}+{D,E,F}→L₁

: Disp "U+V="

: Goto L

: Lbl H

: {A,B,C}-{D,E,F}→L₁

: Disp "U-V="

: Goto L

: Lbl I

: sum({A,B,C}*{D,E,F})→K

: Disp "U•V="

: Output(1,6,K)

: Stop

: Lbl J

: BF-CE→L₁(1)

: CD-AF→L₁(2)

: AE-BD→L₁(3)

: Disp "U*V="

: Goto L

: Lbl L

: Output(1,6,L₁)

For the vector answers, it may also be helpful to know their magnitude and unit vector. Because we stored the vector in list L₁, we can easily calculate and output the values. For the unit vector, I rounded each value to 4 decimal places to make the result cleaner, but feel free to remove that and just print L₁/M. The final *Disp* command is just used for aesthetics to provide some space at the bottom of the result.

`: Disp "MAG="`

: √(sum(L₁²))→M

: Output(2,6,M)

: Disp "UNIT="

: Output(3,7,round(L₁/M,4))

: Disp ""

### Running the Program

It’s now time to test it! Try finding the cross product of <5 -3 1> and <-1 2 -1>. Run the program and input the correct 6 values. Next, the menu should appear. Select the last option. If successful, you should find the result to be <1 4 7>. The magnitude of this vector is 8.124 units and the unit vector is <.123 .492 .862>. To confirm all the code is correct, try testing all four operations.

### Complete Code

`: ClrHome`

: ClrAllLists

: Input "U1= ",A

: Input "U2= ",B

: Input "U3= ",C

: Disp ""

: Input "V1= ",D

: Input "V2= ",E

: Input "V3= ",F

: ClrHome

: Menu("OPERATION","U+V",G,"U-V",H,"U•V",I,"U*V",J)

: Lbl G

: {A,B,C}+{D,E,F}→L₁

: Disp "U+V="

: Goto L

: Lbl H

: {A,B,C}-{D,E,F}→L₁

: Disp "U-V="

: Goto L

: Lbl I

: sum({A,B,C}*{D,E,F})→K

: Disp "U•V="

: Output(1,6,K)

: Stop

: Lbl J

: BF-CE→L₁(1)

: CD-AF→L₁(2)

: AE-BD→L₁(3)

: Disp "U*V="

: Goto L

: Lbl L

: Output(1,6,L₁)

: Disp "MAG="

: √(sum(L₁²))→M

: Output(2,6,M)

: Disp "UNIT="

: Output(3,7,round(L₁/M,4))

: Disp ""

Thank you so much!!! I had no idea what I was putting into my calculator, but this works amazingly!!!

Thank you for the program. There is one typo in your “running the program section”. You have -5 instead of 5 for the vector. If you enter -5 you get the wrong answer.

This code is awesome…such a time-saver thank you!

My only complaint is that when you do the cross product, it won’t show the z-component if the decimal places are too long. I know I could always make it round like we do the univ vector, but that messes with the magnitude A LOT.

How do you alter the program so that the actual value of the vector will go down to a second line if it is long like the unit vector does? Then we could get all three components without having to deal with an inaccurate magnitude due to rounding.

Thanks!