Scaled partial pivoting pseudocode doesn't seem to make sense I got this pseudocode from my textbook for gaussian elimination with scaled partial pivoting:
for i=1 to n:
    L[i] = i
    smax = 0
    for j=1 to n:
        smax = max(smax, |A[i][j]|)
for k=1 to n-1:
    rmax = 0
    for i=k to n:
        r = |A[L[i]][k] / S[L[i]]|
        if (r > rmax):
            rmax = r
            j = i
    L[i] <-> L[k]
    for i=k+1 to n:
        xmult = A[L[i]][k] / A[L[k]][k]
        A[L[i]][k] = xmult
        for j=k+1 to n:
            A[L[i]][j] = A[L[i]][j] - xmult*A[L[k]][j]

I have two problems with this pseudocode, and both of them are in the two nested for-loops.
My first problem: Why am I assigning a value in the coefficient matrix to xmult (the number you multiply  the pivot row by prior to subtraction)? I could be wrong, but this makes no sense whatsoever, mathematically. Doing so changes a single value in the equation, rendering the system inconsistent. Am I wrong?
My second problem: the int j in the nested loop where the forward elimination occurs starts at k+1, which is 2 at its lowest, but j needs to be 1 in order to zero-out the values in the first column. Since j is 2 at its lowest, how can the first column ever get modified during the forward elimination phase?
Bonus question that is to be answered if my two conclusions above are true: why bother publishing pseudo-code if it literally doesn't work?
 A: Problem 1
The value xmult is assigned prior to the for loop for optimization purposes.  The value xmult would otherwise have to computed n-k times.  xmult is known as the scalar coefficient which is required so that we can do row operations.
Problem 2
This is probably the most confusing part of the algorithm.  We expect an upper-triangular matirx U after rearranging the rows of A.  However, by assigning A[L[i]][k] = xmult and then iterating from i=k+1 to n, we do not get that.  This is because the matrix A is not the upper-triangular matrix U once its rows are rearranged.  It is actually a shared matrix between L and U, where L is the lower-triangular matrix.  Imagine A were the upper-triangular matrix U after rearranging its rows.  Our code then becomes
for i=1 to n:
    L[i] = i
    smax = 0
    for j=1 to n:
        smax = max(smax, |A[i][j]|)
for k=1 to n-1:
    rmax = 0
    for i=k to n:
        r = |A[L[i]][k] / S[L[i]]|
        if (r > rmax):
            rmax = r
            j = i
    L[i] <-> L[k]
    for i=k+1 to n:
        xmult = A[L[i]][k] / A[L[k]][k]
        for j=k to n:
            A[L[i]][j] = A[L[i]][j] - xmult*A[L[k]][j]
# rearrange the rows of A here to get U

Optimized code can be really confusing for the developers reading it.  The algorithm works, just not exactly as you would expect.  The reason the code is written as it is is because it is optimized for the solve procedure which consists of forward elimination and back substitution.  Here, we only see the forward elimination phase.
