version7 (endless loop is fixed)

I fixed the endless loop; now it correctly uses the res[] array.