Post Reply 
Swapping Registers on HP calculators
03-28-2024, 09:21 AM (This post was last modified: 03-28-2024 09:35 AM by n1msr.)
Post: #5
RE: Swapping Registers on HP calculators
Thank you all for taking the time to reply, and apologies for the vagueness of the question.
Also, thank you Marc for indenting the Python!

Valentin summarised my question well: "I have a 1 x 9 array A and I need the most efficient sequence of RPN steps to exchange element A(1,i) with element A(1,j).
Also, I am [still] curious how to toggle USER mode programatically, if it is possible.

Below is the code for the HP-15C SWAP code to exchange two elements i and j in a single 1 x 9 matrix A.
USER mode is on, although I am not reliant on this for the SWAP
i is stored in R3
j is stored in R4
"temp" store for the SWAP is R5

*please note that I add 1 (one) to the column (i and j) because the algorithm uses 0 (zero)
as the index to the first element of the list/matrix, whereas the 15c uses 1 (one). Changing the
algorithm to use 1 (one) as the index to the first element would save several steps!

Code:

1           \\ row 1
RCL3      \\ column i
1           \\ add 1 to column*
+
RCL g A  \\ recall ith element of matrix A: A[1,i]
STO 5     \\ store ith element of A in "temp" register R5
1            \\ row 1
RCL 4     \\ column j
1            \\ add 1 to column*
+
RCL g A   \\ recall jth element of matrix A: A[i,j]
1            \\ row 1
RCL 3     \\ column i
1            \\ add 1 to column*
+
STO g A  \\ store A[1,j] into A[1,i]
RCL 5     \\ recall "temp" to get original A[1,i]
1           \\ row 1
RCL 4    \\ column j
1           \\ add 1 to column*
+
STO g A   \\ store original A[1,i] in A[1,j] to complete the SWAP
That's it. Thank you.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Swapping Registers on HP calculators - n1msr - 03-28-2024 09:21 AM



User(s) browsing this thread: 1 Guest(s)