It looks like your logic for handling the output layer after you fall out of the “for” loop over the hidden layers is wrong. You are using an incorrect A value as the input to that step.
Here’s a thread which shows how to do the “dimensional analysis” for this particular test case, so that you can recognize what should be expected at each layer.