Two-dimensional arrays

A char is one character. The best option here is a byte, but this is a proof-of-concept.

The index numbers are to make my life easier. Counting from 0 is hard.

This is a working proof-of-concept.

Computers are the stupidest things I have ever yet encountered, but they manage it easily.
(Hint: If you find it difficult, put the indices in the comments, not the data)

Like 20 extra bytes are not going to make a significant difference, and if they do, I'll get rid of 'em. But I want them where they are for now.

sp. "26". It eats into your RAM (1.25%) and your flash.

^^^

LOL. Do the world a favor and don’t ”help” by trying to correct that deficit.

a7

3 Likes

I don't understand.

I believe the point is: Don't make the situation worse

(there really is no deficit of good information about arrays)

Isn't the sixth element 36 ?
The first is 2, yes? So 241 is the seventh element.
Or do you reckon the first is the 'zero-eth'?

Yes.

Haha, adding zero to the ordinal numbering of things is the mark of a real C/C++ programmer.

a7

Index 2 refers to the third item, and index 6 refers to the seventh item. "2" is nominal (name) while "third" is ordinal (position).

Here's one way I like to visualize index numbers:

  _________________
  |   |   |   |   |
  | A | B | C | D |
  |___|___|___|___|
  0   1   2   3   4
  ^
  |
index 

Imagine an array is like a ruler. A ruler starts at 0, just as index numbers do. An index (e.g., 0) refers to the entire "unit" up to but not including the next index (e.g., 1). The size of this array is 4, which means indexes can go up to but not including 4.

Understanding the "up to but not including" concept is very useful in a lot of different areas of computer science—time, for example (a day starts at 0 and goes up to but not including 86400 seconds (discounting days with leap seconds)).

See also E.W. Dijkstra Archive: Why numbering should start at zero (EWD 831).

The point I like is

an element's ordinal (subscript) equals the number of elements preceding it in the sequence.

2 Likes

100%, for shure :nerd_face:

Padding your real data with dummy values just to "correct" the counting from zero which you don't like is a bad idea that will come back to haunt you with scary bugs sooner or later (usually, sooner). You can easily take care of the offset in code when you retrieve a value from the array. If you count from 1 and you want n, just retrieve the element indexed [n-1], and maybe add some comment to remember yourself why you are doing this. Or, make peace with how counting works in most languages and count from zero yourself. You may not have thought of it, but many things we are used to are zero-indexed. Decimal number 789 is zero indexed. To find out how much 789 is, you do:

9 × 10^0 + 8 × 10^1 + 7 × 10^2

Notice the pattern in the exponents?

Oops

Nice spot.

Well, at least that last vowel is not an e.

don't assume... read the C++ language specification / documentation

https://en.cppreference.com/w/cpp/language/array

(hint: you can).

Don't put money on that.

You can make arrays with as many dimensions as you like, if you have enough memory.

Please they are not weird, the fact you can not understand them is your problem, don't be so arrogant as to think that every one shares your view.

Counting from 0 is not hard, it is not weird it makes perfect sense. Do not waste precious memory by having blank zeroth entries in each dimension.