The vector "value" you create in the program does not get a value in each turn of the for loop.
I inserted a "try and catch" at the position where the error is thrown

and it can be seen by the result

that "value" has just 14 elements when i=25 and so you obviously can't access element #24.
You have to check your nested series of "if ... else if .... if ... else ... etc." to assure that "value" is assigned a value in every 'round' of the for-loop.
BTW, your "index" function could be written by simply using the "match" function. Only difference is the behaviour if the value (z) is not found in the vector.
Your function returns 0 in this case but "match" will return an error. If this is mandatory you may throw in a try and catch as shown:

You may also do without this index function if you replace calls like

by

but you would have to assure that this if-statement is not called for the last i because then there is no element # i+1