Why is the first index on shitty languages equals to 0?

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.

Attached: 1200px-ISO_C++_Logo.svg.png (1200x1349, 69K)

Other urls found in this thread:

cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html
twitter.com/NSFWRedditGif

>the first ordinal is 0

Attached: brainlet1.png (183x275, 4K)

POO

>On any good language, the first index on an array, collection or vector is equal to 1.
Name one.

read this pajeet
cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html

>on [language] the first index is 1
literally who?

Lua and Matlab

Because arrays.

>what is pointer arithmetic?

an array index is an offset, that's the most fundamental rule of programming.

those are some of the worst programming languages invented in history

Exactly

On Pascal the first index can be whatever integer you want, even negative numbers (sometimes it's useful).

on superior mathematica 0 gives you the head of the element and at position 1 you get the first element

Lua is quite nice actually

Smalltalk.

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.

Seconded

the best thing about Lua is whitespace padding inside parentheses

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.

t. brainlet

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.

>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?

Only if the array index is a constant expression.

>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)]

Attached: brainlettttt.jpg (800x450, 41K)

>OP

Attached: 1518075349759.png (625x773, 87K)

so humans can distinguish them self from trash

user, is sepples too difficult?
#include

template
class sarray {
public:
const T &operator [] (size_t i) const {
return this->_items[i - B];
}
T &operator [] (size_t i) {
return this->_items[i - B];
}
size_t size () const {
return N;
}
private:
T _items[N];
};

int main () {
sarray a;
a[1] = 12;
a[2] = 13;
a[3] = 14;
for (size_t i (1); i

OP is a faggot

The real question is, why does OpenGL reverse the Z coord?

Any other answer than this is wrong

Just change the matrix transformation et voilĂ .

iirc you can't have a variable as the index of an array, in Pascal at least.

lua is the only one I know of. and lua is literal trash. worst shit ever

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)

var
arraytest: array of boolean;

begin
showmwssage(length(arraytest));
//equals to 0
end;

wtf u talk about pascal?

That's a dynamic array in an extension of Pascal. You probably want to use SetLength to allocate a size.

but the first ordinal is 0 not 1

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.

This

do sets also work in the same way? :/

been coding in delphi for 4 years now

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];

RespondToInvite(fri, workdays);
RespondToInvite(sat, workdays);

workdays := workdays - [fri];

RespondToInvite(fri, workdays)
end.

Output:
Sorry, I have to work fri
Sure, I can come over sat
Sure, I can come over fri

>edsgay dickster
disregarded

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.

for(i = 0; i < whatever.length; ++i)
some expression involving whatever[i];

/thread

math

>I have never read a book or wikipedia article about CS in my life
Quality thread, OP.

R you dumb nigger