Working with Lengths and Positions
In programming we often have a notion of both a relative "length" of something, and also its absolute "position" in some environment. When we compare two items, they should either both be relative, or, both be absolute.
Here are some examples of the two:
Domain | Relative | Absolute |
---|---|---|
time | duration | date |
pointers | index | pointer |
arrays | length | position |
(to be sure pointers and arrays are closely related)
A relative item can be converted to an absolute by addition, and it requires an absolute given as a baseline:
absolute2 = absolute1 + relative; // or relative + absolute
A duration can be converted to a date by adding some baseline date.
date = 1 /*day*/ + Today; // Tomorrow..
An index can be converted to a pointer by adding some baseline pointer.
char *p ...;
int i ...;
char *q = p + i; // or i + p;
A length can be converted to a position by adding some baseline position.
int startPosition ... ;
int length ...;
int onePastTheEndPosition = startPosition + length; // or length + startPosition;
Absolute items can be converted into relative by subtraction, and it requires two absolute items:
int startPosition ... ;
int endPosition ... ;
int length = endPostion - startPosition;
Let's note now that — just like with zero-based arrays — the result of adding a baseline absolute to a length is another absolute that is one past the end of an item of length. Thus, there is a possible off by one error, that is generally avoided by treating a range (startPos, length; also startPos, endPos) as inclusive of the first position and exclusive of the end position.
Let's look at this loop where the loop control variable goes the length of some item to be located somewhere within an array. (Such a loop might be searching for a word within a string by placing it within an outer loop that advances startPosition
):
for ( i = 0; i < length; i++ )
a [ startPosition + i ] ...
Because a[0]
refers to the absolute first item, arrays must be indexed by an absolute position, here, we convert i
from an item of relative length to an absolute position within the array for subscripting.
Alternately, we can compute the position directly into the loop control variable.
for ( i = startPosition; i < startPosition + length; i++ )
a [ i ] ...
Here i
is a postion with the baseline position already added in. By adding in the baseline during the loop's initialization, we must also use an absolute position during the loop control conditional. (As with the former, this loop might be used to search for a word in a string, by wrapping it within an outer loop that advances startPosition.)
When we write simple for loops:
for ( int i = 0; i < N; i++ ) ...
It appears that we cannot actually tell if the loop control variable is a length or a position — but it is really both, because if we used a baseline absolute position here, it would be zero. Thus, as 0+0 is zero and N+0 is N, this loop control variable share the properties of both relative length and absolute position.
Here's more complete code where the outer loop checks each position to see if the inner loop finds a particular word there:
char *textToSearch = "hello world";
char *toMatch = "world";
int N = strlen(textToSearch);
int M = strlen(toMatch);
for ( int startPosition = 0; startPosition < N; startPosition++ ) {
int i; // declared outside the for so we can use it after
for ( i = 0; i < M; i++ ) {
if ( textToSearch [ startPosition + i ] != toMatch [ i ] )
break; // stop the inner loop and move on to outer loop
}
if ( i == M ) {
printf ("match found at %d\n", startPosition );
// we might stop the outer loop if we're only looking for the first ocurrence
} else {
printf ("match not found at %d\n", startPosition );
}
}
Let's observe that the inner loop's control variable, i
, is as a position in two cases: textToSearch [ startPosition + i ]
identifies the character at the next position to start looking in the larger string, while toMatch [ i ]
identifies the character at the next position in the word to be matched to compare against.
I think it’s a really good project, good luck to you .
https://tweakbox.mobi/ https://tutuappx.com/