How can arrays within decisions improve program efficiency?

Answer:

When a significant number of values must be tested to perform an operation, an array is more efficient then "if else" or "switch case" statements. Juge by the following example in C:

const char* ConverteIntegerToCStr( unsigned int number, char cstr[] )
{
// predefined array with the first 10000 number string representation
// Note: the ellipsis is not part of the code
static const Array[10000] = { "0", "1", "2", ..., "9998", "9999" };

if (number < 10000)
memcpy( cstr, Array[number], strlen( Array[number] ) ); // very fast
else
itoa( number, cstr ); // standard C function that is much slower
return cstr;
}

First answer by Ftrudel. Last edit by Ftrudel. Contributor trust: 0 [recommend contributor recommended]. Question popularity: 2 [recommend question].