Exercises

Exercise 2
Write a function nexthour that receives one integer argument, which is an hour of the day, and returns the next hour. This assumes a 12-hour clock; so, for example, the next hour after 12 would be 1.
Requirements:
  • Function name: nexthour
  • Input: one integer argument (hour)
  • Output: next hour (12-hour clock)
  • Handle the wrap-around from 12 to 1
Here are two examples of calling this function:
>> fprintf('The next hour will be %d.\n',nexthour(3))
The next hour will be 4
>> fprintf('The next hour will be %d.\n',nexthour(12))
The next hour will be 1
Example Calls:
>> fprintf('The next hour will be %d.\n', nexthour(3)) The next hour will be 4. >> fprintf('The next hour will be %d.\n', nexthour(12)) The next hour will be 1.
Function Template:
function next_h = nexthour(current_hour) % NEXTHOUR Returns the next hour on a 12-hour clock % Input: current_hour - integer from 1 to 12 % Output: next_h - next hour (1-12) % Your code here
To take full advantage, try implementing the exercise before checking the solutions!


For more details, please contact me here.