Sometimes it is better to replace a code snippet performing calculations by a lookup table. The use cases are:
1. When the result of calculation is always a member of predefined set of numbers
2. When input is a set of predefined integers
For such cases it is easy and convenient to define an array and use it. one such example is (the code can be found here):
1. When the result of calculation is always a member of predefined set of numbers
2. When input is a set of predefined integers
For such cases it is easy and convenient to define an array and use it. one such example is (the code can be found here):
#include <stdio.h> | |
int main(void) { | |
int lookupTable[4][5] = { | |
{ -1, 9, 4, -1, 0 }, | |
{-1, -1, 9, -1, 4}, | |
{-1, -1, -1, -1, 5}, | |
{-1, -1, -1, -1, 9} | |
}; | |
int row, columns; | |
for(row=0; row < 4; row++) { | |
for(columns = 0; columns < 5; columns++){ | |
printf("Row %d Columns %d Value %d\n", row, columns, lookupTable[row][columns]); | |
} | |
} | |
return 0; | |
} |