Here is your program fixed :

But I would rather prefer to make the program self-contained, independent from the existence of global worksheet variables ninc, Avals and Bvals. And it may also be a good idea to make it ORIGIN aware so that it does not rely on ORIGIN being set to zero:

You now can create a table similar to the one in the picture embedded in your sheet

or you may create a plot showing all the points of intersection

For the plot I had replaced two values in the C-vector to test my function in case a horizontal line is too low or too high and does not intersect the graph at all. Therefore the lines at height 24 and 36 are missing as they were replaced by lines in height -10 and 50. In this case my program returns a simple NaN instead of a row vector with x-values.
I am aware that you wrote that this case never occurs in your application, but I believe that a program should always be able to handle all possible cases whenever possible (provided that this can be achieved with reasonable programming effort).
EDIT: Added a version where multiple identical values such as the duplicate 500 in the last row are removed.
Hope that the attached Prime 11 file will help.