We're getting highly OT here, but between any two rationals lie an
infinity of rationals. But it's a smaller infinity than the infinity of
trancendentals.
Rationals are countable because they can be expressed as a pair of
integers (numerator, denominator) and the set of pairs of integers forms
a bijection with the set of integers (proof omitted here).
Trancendentals are uncountable due to cantor's diagonal argument.
Phil
OKAY lets make it topical. I came up with this algorithm to solve a
problem in Oracle PLSQL (which only has one dimensional arrays), but
it can be useful in C also.
To make it more relevant to C let me set the stage.
You need an extensible 2D array. You can simulate this with a linear
array and this routine.
/**************
For a linear array X[]
Given 2 indices A,B
return an index value that maps to a location in X[]
With first element at (0,0) and X[] is zero based.
*********************************************/
int twod( a, b )
{
int k,n,ndx;
k=a+b+1;
n=k*(k+1);
n=n/2;
ndx = n-b-1; /* make zero based for C */
return ndx;
}
and the caller uses this within the array, like
X[twod(3,5)]++ ;
/* and for extensible array situations, check the index first */
if ( twod(aa,bb) >numelementsin(X) )
{
/* need to reallocate to have room for the next entry */
<realloc code>
/* wouldn't need this in Oracle, which has sparse arrays */
/* and allocates elements for you */
}
X[twod(aa,bb)]=nextvalue;
A final comment:
this routine basically fills the array along diagonals. So it really
is best if your application needs a triangular array rather than a
square one. For example if you want to be able to use X[twod(10,10)],
then X must have 221 elements.
Enjoy.
Ed