C – Lookup Tables


Lookup Tables in C



”;


Lookup tables (popularly known by the abbreviation LUT) in C are arrays populated with certain pre-computed values. Lookup tables help avoid performing a lot of calculations in a program. Instead of lengthy nested if-else statements or switch statements, one can use Lookup tables to enhance the efficiency of a C program.

Example 1

Let us have a look at a simple application of a lookup table. In the following code, we compute the square of a given integer.


#include <stdio.h>

int square(int x){
   return x*x;
}

int main(){

   int num[5] = {1, 2, 3, 4, 5};

   for (int i = 0; i <= 4; i++){
      printf("No: %d tSquare(%d): %dn", i+1, i+1, square(i+1));
   }
   
   return 0;
}

Output

When you run this code, it will produce the following output −


No: 1 	Square(1): 1
No: 2 	Square(2): 4
No: 3 	Square(3): 9
No: 4 	Square(4): 16
No: 5 	Square(5): 25

Example 2

While the above program works satisfactorily, it involves frequent calls to the square() function, for each value of the array index.

Instead, we can declare an array to store the squares of numbers and access the computed square directly from the index.


#include <stdio.h>

int main(){

   int squares[5] = {1, 4, 9, 16, 25};
   for (int i = 0; i <= 4; i++){
      printf("No: %d tSquare(%d): %dn", i+1, i+1, squares[i]);
   }

   return 0;
}

Output

Run the code and check its output −


No: 1 	Square(1): 1
No: 2 	Square(2): 4
No: 3 	Square(3): 9
No: 4 	Square(4): 16
No: 5 	Square(5): 25

Example 3

In the example below, we fetch the name of the element corresponding to its atomic number.


# include <stdio.h>

int main(){

   int num = 3;

   switch (num){
      case 1: puts("Hydrogen"); break;
      case 2: puts("Helium"); break;
      case 3: puts("Lithium"); break;
      case 4: puts("Beryllium"); break;
      case 5: puts("Boron"); break;
      default: puts("error: unknown element!");
   }
   
   return 0;
}

Output

It will produce the following output −


Lithium

Example 4

Instead of a lengthy switch statements with a case for each element, we use a lookup table (an array populated with the names of all the elements) to simplify the program −


#include <stdio.h>

static const char *table[] = {
   "Hydrogen", "Helium", "Lithium", "Beryllium", "Boron"
};

int main(){

   int num = 3;

   if (num >= 1 && num <= 5){
      printf("Name of the element with atomic number %d is %s", num, table[num-1]);
   } else {
      puts("error: Atomic number not in the lookup table!");
   }
   
   return 0;
}

Output

Run the code and check its output −


Name of the element with atomic number 3 is Lithium

Lookup Tables in 7-Segment LED Display

Lookup tables are extensively used in the design of embedded systems, as they enhance the performance of the application.

In many devices, the seven-segment LED display is used to show visual output. Eight segments of the unit are illuminated based on a sequence of binary digits. We use a lookup table to convert numbers ranging from 0 to 9 to seven-segment signals to drive a display.

Example

The 7-segment binary code for the number is stored as an array element. The hexadecimal code is then converted to binary code that will drive the 7-segment display.


#include <stdio.h>

int main(){

   // Array to represent numbers 0-9 in 7-segment display binary encoding
   int const nums[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};

   static int bin[8];

   int i = 0, num = 7,  rem;

   printf("The binary equivalent of 7 is: ");

   for (i = 7; i >= 0; i--){
      rem = num % 2;
      bin[i] = rem;
      num /= 2;
   }

   for (i = 0; i <= 7; i++){
      printf("%d", bin[i]);
      if (i == 3) printf(" ");
   }

   return 0;
}

Output

Run the code and check its output −


The binary equivalent of 7 is: 0000 0111

The least significant bits represent segments “a”, “b”, “c” and “d”. The most significant bits are “e”, “f”, “g” and “h”. The segments “a”, “b” and “c” illuminate to display 7, leaving the other segments off.

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *