One name for a whole row of values. Slide the index past the end and watch a real program hand you memory that was never yours — with no error at all.
size − 1 and what happens past itSo far one name has meant one value. An array is one name for a whole row of them, and you pick which one with a number in square brackets.
Counting from zero looks like a quirk until you notice it makes the loop from level 7 fit perfectly: for (int i = 0; i < 5; i++) visits every box and stops in exactly the right place.
i < size habit matters here.Slide past 4 and see what a real program does.
Reading past the end does not crash and does not warn at runtime. Verified on a real program: a[5] printed 1 — a value that simply happened to be sitting in the next piece of memory. Writing past the end is worse: you are changing memory belonging to something else, and the damage shows up somewhere unrelated, later. This is the single most notorious bug class in C and C++.
The compiler does warn when it can see the number you typed, like a[5]. It cannot warn when the index is a variable — which is where every real bug of this kind lives.
An array has one enormous limitation: its size is fixed when you write it, and it does not know how big it is. A vector fixes both.
push_back.size() tells you how manyYour course will teach arrays first, because they are what is underneath and because pointers in level 13 make more sense once you have seen them. Use whichever your assignment asks for — and note that v.size() in the loop condition removes a whole category of off-by-one, because the number is never written down twice.
Learn that as one unit. It is level 5’s “find the biggest” shape, level 7’s loop and this level’s indexing, all at once — and it is most of what a first course asks you to do with a list.
A seating plan. A noughts-and-crosses board. Marks for five students across four assignments. When your data has rows and columns, you give the array two sets of brackets:
Read it as [row][column], in that order, and both still start at zero — so int grid[3][4] has rows 0 to 2 and columns 0 to 3. There is no grid[3] and no [4], and going past either end is as silent as it was in one dimension.
To visit every box you need level 8’s loop inside another one — the outer walks the rows, the inner walks the columns of that row:
0 1 2 3 10 11 12 13 20 21 22 23
cout << endl; belongs to the outer loop, after the inner one finishes. Put it inside the inner loop and every single number lands on its own line; leave it out altogether and all twelve run together in one long row. Neither is an error and neither warns — you just get a mess, and the fix is one line moving by one level of indentation.
The nesting is worth pausing on: the inner loop runs completely, all four columns, for every single lap of the outer one. Three rows × four columns is twelve laps of the inner body. Trace the first two rows by hand — level 4, with two counters instead of one — and nested loops stop being confusing for good.
Ask for values by index. “Give me a[0].” “Give me a[5].” Then ask for a[6] and see what B does.
Hand over whatever is in that cup. For a[6], do not refuse — reach past the end of the row and grab whatever object is there. That is exactly what C++ does.
Then run level 5’s find-the-biggest on the cups, out loud, using only “best so far”. You will find you already know the algorithm and only needed the notation — which is the point of having done the thinking track first.
Five boxes are numbered 0 to 4. This is why loops use < and not <=.
No crash, no warning at runtime. You get someone else’s memory and carry on.
i < v.size() beats a number you typed, because the number can go stale.
Not recall — these are the shapes an exam actually uses. Every answer below was produced by compiling and running the code, so if you disagree with one, the compiler is the one to believe.
Have a real go before revealing. Being wrong here is worth more than being right in three weeks.
What does this print?
What happens if you read a[5] on that same array while the program runs?
A 2 by 3 grid where each cell holds row×10 + column. What is g[1][2]?
In a nested loop printing a grid, where does cout << endl; belong?
← back to the whole C++ track · stuck on anything? ask peter.