In communication protocols (like processing UART bytes or TCP packets), efficient command handling is key. Using a giant switch-case statement is slow (O(N)) and hard to read. A professional alternative is a Jump Table (or Dispatch Table): an array of function pointers. The command ID acts as the index into the array, allowing O(1) (instant) execution.
Your task is to implement a simple dispatcher.
void cmdReset(): Prints System Reset.void cmdStart(): Prints System Start.void cmdStop(): Prints System Stop.main, create an array of function pointers of size 3.Program Flow:
N (number of commands).N times.cmdID.cmdID is valid (0 to 2), execute the function at dispatchTable[cmdID].cmdID is invalid, print Invalid Command.Input Format:
N.N lines: Integer cmdID.Output Format:
Invalid Command.Example:
Example 1
Input:
3
1
0
5Output:
System Start
System Reset
Invalid CommandConstraints:
switch-case or if-else to select the function (only for bounds checking).
Input
3 1 0 5
Expected Output
System Start System Reset Invalid Command