Site hosted by Angelfire.com: Build your free website today!

Menu bar
Data Structures And Number Systems
© Copyright Brian Brown, 1984-2000. All rights reserved.

Part 3
index prev next


Lookup Tables
Lookup tables are a sequence of data (numbers or characters) which are used for conversion purposes.

The start of the table represents the base value, and each location (table entry) contains the desired answer.

It is essentially an array. Consider the example of converting the numeric digits 0-15 to their hexadecimal character equivalents. This can be achieved as shown below,


     +--------+
     |  '0'   | Table[0]
     +--------+ 
     |  '1'   | Table[1]
     +--------+ 
     |  '2'   | Table[2]
     +--------+
     ..........
     +--------+
     |   'A'  | Table[10]
     +--------+
     ..........
     +--------+
     |   'F'  | Table[15]
     +--------+

As you can see from the table above, the index into the array is the value you trying to convert, with the answer stored at that element number.

Lets implement this in C


        #include <stdio.h>

        static char Table[] = "0123456789ABCDEF";

        main() {
           int value, valid;
           char answer;

           valid = 0;
           while( valid == 0 ) {
               printf("Enter integer value to convert [0-15]\n");
               scanf(" %d", &value );
               if( (value < 0) || (value > 15) ) {
                  printf("Input value outside legal range 0-15]\n");
               }
               else {
                  valid = 1;
               }
           }
           answer = Table[value];
           printf("The answer is %c\n", answer );
        }

Another use can be converting a range of degrees from Fahrenheit to Celcius.

The major advantages of lookup tables are

Lookup tables are often used where the number of input values is limited (not suited to a large range of values) and where a small amount of inaccuracy is unimportant.


RECORDS OR STRUCTURES
A record is a composite user-defined multi-element data structure. It is composite in that the different elements of the record can have different data items. It is user-defined in that the programmer decides what the different data types should be.

The drawback of using arrays is that only one data type can be used. Records remove this dis-advantage, allowing closer modelling of the data information.

Consider a data structure for a particular component in an electronic sub-system. The information we might wish to store is,

Using arrays for storing such information would result in real problems (7 arrays to manage).

Records provident a way of representing this conglomerate of information in a totally efficient, manageable way. A record definition of the above could be,

 
	type	system = record 
			type : array [1..20] of char; 
			value : real; 
			i_rating: real; 
			cost : real; 
			size : integer; 
			n_pins : integer; 
			ref_deg : array [1..4] of char; 
		end; 

	var	component1 : system; 
 

Each element of a record is accessed using the following convention,

 
	record_name.element_name 

Thus, the statement

 
	component1.cost := 0.20; 

initializes the cost field of the record component1 to twenty cents.

In Pascal, records can be defined and used as follows,

 
	var 
		example_record = RECORD 
		                    int_number : integer; 
		                    fp_number  : real; 
		                    letter     : character; 
		                 END; 
	begin 
		example_record.int_number := 1267; 
		example_record.fp_number  := 12.5; 
		example_record.letter     := '+'; 
 

The code example declares a record called example_record which has three elements, an integer, a floating point and character variable.

The structure in memory would look like,

 
	+---------+<---+-------------+ 
	+---------+    |int_number   | 
	+---------+<---+             | 
	+---------+    |             | 
	+---------+    |fp_number    |example_record 
	+---------+    |             | 
	+---------+<---+             | 
	+---------+<---+letter-------+ 

	+---------+ 
	+---------+ = 8 bits of memory storage 
 

Inside the main body of the program, after the keyword begin, the statement shown assigns the numeric value 1267 to the first element of example_record, the integer variable int_number.

The second code fragment assigns the value 12.5 to the variable fp_number inside the example_record.

The last code fragment assigns the character value '+' to the variable example_record.letter.

The advantages of using records are,


index prev next

Home | Other Courses | Feedback | Notes | Tests

© Copyright Brian Brown, 1984-2000. All rights reserved.