Chapter 18: Problem 27
Write two different code segments that may be used to wrap an index back around to the beginning of an array when it moves past the end of the array. Use an if/else statement in one segment and modular arithmetic in the other.
Short Answer
Expert verified
The two methods used to handle wrapping an index back to the beginning of an array when it moves past the end of the array are:
1. Using an if/else statement to check if the index is greater than or equal to the length of the array, and adjusting the index accordingly when it surpasses the array length.
2. Using modular arithmetic to calculate the remainder of the index divided by the length of the array, effectively wrapping the index back to the beginning if it surpasses the array length.