Lesson 14: Introduction to Arrays

[spoiler title=”Lesson Video”]
Direct Download of Video (For mobile / offline viewing)(right click > save target as)


[spoiler title=”Lesson Source Code”]


Homework: None

Back in the lesson about strings, I eluded a lot to how strings were an object(lesson 21) that contains same-type variables (in the case of strings, chars). A collection of same-type variables in a single container is known as an array. In ++ you can make an array of any type of variable, even user-made types. The typical way to make an array is as follows:

Array Indexing
One of the most common error among beginner programmers is that they don’t realize that arrays are 0-indexed. This means that the first item you add in an array goes into the position array[0], rather than array[1]. This means that if you make an array with the values 1,2,3,4,5 the values would be in array[0] – array[4] respectively.

Iterating through an array with a loop
Using a for loop to output the contents of an array is fairly simple when you use the following structure: for (int counter = 0; counter < arraySize; conuter++){ cout << array[counter] << endl; Below is an example of how this might look in real-code