On any good language, the first index on an array, collection or vector is equal to 1.
It is so dumb to have the first to be 0. You then setup an array of size equals 5. The last index will be 4. Everytime time you want the last item you have to input array(array.size() - 1) This is so dumb, you have to put - 1.
>on [language] the first index is 1 literally who?
Jaxson Robinson
Lua and Matlab
Robert Perez
Because arrays.
William Wood
>what is pointer arithmetic?
Oliver Brown
an array index is an offset, that's the most fundamental rule of programming.
Carter Nguyen
those are some of the worst programming languages invented in history
Grayson Cook
Exactly
Oliver Clark
On Pascal the first index can be whatever integer you want, even negative numbers (sometimes it's useful).
Lucas Martinez
on superior mathematica 0 gives you the head of the element and at position 1 you get the first element
Dylan Reyes
Lua is quite nice actually
Noah Cruz
Smalltalk.
Aiden Reed
It's an artifact from the implementation of an array that has stuck as convention. Arrays are implemented such that the array is the pointer to the first element in memory, and each subsequent element is accessed by adding the offset for that element to the memory address of the first. arr[0] says go to the memory address of arr and add 0 to it and give me the value at that address.
Kevin Garcia
Seconded
Mason Bennett
the best thing about Lua is whitespace padding inside parentheses
Sebastian Long
the index is an offset, so array[5] points to the memory location of array + 5 * the size of the datatype of the array. The first item is stored at the memory location of the array itself, so it's index must be 0. Ofcourse, they could have set the starting index to 1 and then just subtract 1 behind the scenes, but that means that someone could have entered 0 as index and then it would point to the memory location of the array - the size of the datatype of the array. So, having 0 as the starting index makes it possible to have the index as an unsigned int.
Josiah Cruz
t. brainlet
Andrew Harris
Almost every programming language uses 0 based indexing because it makes obtaining the array element as simple as dereferencing pointer+offset. Very few languages use anything else, because it would require inefficient conversion to a 0-based system.
Lua uses 1-based indexing, but it also doesn't really have arrays. It has tables. An array in Lua is just a table with only integer keys.
Ada and Pascal have 1-based indexing... if you want them. They can actually use any means of indexing their arrays, and if you're smart, you'll use 0-based indexing because it means you're not performing an extra subtract operation every time under the hood.
Fortran and Matlab use 1-based indexing, but basically nobody uses these languages anymore.
Meanwhile, some 0-based languages have gotten around your annoyance at getting the last elements in an array. Do you know how Ruby and Python do it? Fucking negative indexes. ary[-1] is the last element. ary[-2] is the second to last element, and so on. And Ruby's "there's more than one way to do it" idea allows for alternatives too, like ary.last.
Camden Miller
>Ada and Pascal have 1-based indexing... if you want them. They can actually use any means of indexing their arrays, and if you're smart, you'll use 0-based indexing because it means you're not performing an extra subtract operation every time under the hood. Eh... wouldn't the compiler optimize that away?
Jason Cooper
Only if the array index is a constant expression.
Jayden Ward
>On Pascal the first index can be whatever integer you want, even negative numbers (sometimes it's useful). ...in static arrays only. Which basically doesn't matter because when array is static you just know lowest and highest index and put it as numeral. Why the fuck would you make your program count array elements number that you declared in a var section earlier? Static arrays are barely used on anything more advanced than "Hello world" >Everytime time you want the last item you have to input array(array.size() - 1) in Pascal: array[high(array)]
int main () { sarray a; a[1] = 12; a[2] = 13; a[3] = 14; for (size_t i (1); i
Cooper Miller
OP is a faggot
Jeremiah Morales
The real question is, why does OpenGL reverse the Z coord?
Robert Martin
Any other answer than this is wrong
Hudson Harris
Just change the matrix transformation et voilĂ .
Hudson Ramirez
iirc you can't have a variable as the index of an array, in Pascal at least.
Carson Long
lua is the only one I know of. and lua is literal trash. worst shit ever
Wyatt Stewart
Languages that are meant to handle real data like R, Matlab, Julia all start indexing at zero
systems languages like C start from zero because you have to signify an empty array memory space (which is technically incorrect because zero is actually pointing at something)
Tyler Perry
var arraytest: array of boolean;
begin showmwssage(length(arraytest)); //equals to 0 end;
wtf u talk about pascal?
Matthew Sanchez
That's a dynamic array in an extension of Pascal. You probably want to use SetLength to allocate a size.
Aiden Rodriguez
but the first ordinal is 0 not 1
Jack Morgan
Because it's not a "normal" Pascal array. Also, your code used the Length function which would logically return 0 for an array with 0 elements regardless of how it was indexed...
var A: array [1..5] of boolean; That is a boolean array index 1 .. 5
var A: array ['a' .. 'z'] of integer; That is an integer array index by 'a' .. 'z'. It could store the count of the occurrences of each letter in a string, for example.
type day = (mon, tue, wed, thu, fri, sat, sun); task = (work, play);
var schedule: array [day] of task; begin schedule[mon] := work; schedule[tue] := work; { ... } schedule[sat] := play; schedule[sun] := play;
writeln('On Monday I will ', schedule[mon]); end. That's an array of tasks indexed for each day of the week.
Sebastian Foster
This
Jeremiah Taylor
do sets also work in the same way? :/
been coding in delphi for 4 years now
Jaxon Thompson
Yes program test;
type day = (mon, tue, wed, thu, fri, sat, sun); days = set of day;
procedure RespondToInvite(d: day; w: days); begin if d in w then writeln('Sorry, I have to work ', d) else writeln('Sure, I can come over ', d) end;
var workdays: days; begin workdays := [mon, tue, wed, thu, fri];
Output: Sorry, I have to work fri Sure, I can come over sat Sure, I can come over fri
Hudson White
>edsgay dickster disregarded
Cooper Gutierrez
because arrays are accessed using an SIB (scale,index,base) byte in the opcode. the scale is the size of each item, the base is the address of the arrary, and the index is the offset into memory. think of SIB as SO(offset)B where the first item in the array is at offset zero. so uint32_t arr[10]; arr[2] = 255; would store the bytes 0x000000ff at address arr + 2 * 4. likewise arr[0] = 255; would store the bytes 0x000000ff at address arr + 0 * 4.
Aaron Reyes
for(i = 0; i < whatever.length; ++i) some expression involving whatever[i];
/thread
Colton Anderson
math
Julian Edwards
>I have never read a book or wikipedia article about CS in my life Quality thread, OP.