The first one is pretty simple - since induction variables go up by fixed amounts per iteration, you can pre-compute the maximum value for any induction variable used as an array subscript and check that it doesn't exceed the array bounds. You'd do this once, before executing the loop, rather than checking the subscript on each iteration.
As for the second, there are plenty of examples (in various languages, including Java) of loop-invariant and induction-variable loops out there - just Google for those terms.
manpreet
Best Answer
2 years ago
I am preparing for a Compilers exam and I found the following two questions in past papers that I don't know how to answer:
I know what an invariant and induction variable means but I don't really know what is expected of me with regards to explaining these two solutions.
I would appreciate it if someone helped me with an explanation.
Thanks!