152. Command Dispatch Table

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.

  1. Define three global functions:
    • void cmdReset(): Prints System Reset.
    • void cmdStart(): Prints System Start.
    • void cmdStop(): Prints System Stop.
  2. In main, create an array of function pointers of size 3.
  3. Initialize the array with the addresses of the three functions above (Index 0=Reset, 1=Start, 2=Stop).
  4. Process user input commands (integers).

Program Flow:

  1. Read integer N (number of commands).
  2. Loop N times.
  3. Read integer cmdID.
  4. If cmdID is valid (0 to 2), execute the function at dispatchTable[cmdID].
  5. If cmdID is invalid, print Invalid Command.

Input Format:

  • First line: Integer N.
  • Next N lines: Integer cmdID.
  • Input is provided via standard input (stdin).

Output Format:

  • Output of the called function.
  • Or Invalid Command.
  • Each output on a new line.

Example: 

Example 1

Input:

3
1
0
5

Output:

System Start
System Reset
Invalid Command

Constraints:

  • Must use an Array of Function Pointers.
  • Must NOT use switch-case or if-else to select the function (only for bounds checking).

 

 

 

Loading...

Input

3 1 0 5

Expected Output

System Start System Reset Invalid Command