In this video, we'll learn about arrays and their characteristics.
We'll also see the typical use case for an array.
An array is a compound data type or a structured data type.
This means that it's a data type that's made up of other data types.
For example, a collection of test scores where each test score
is an integer is considered a compound data type made up of integers.
We say that this is an array of integers.
It's important to understand that all elements of an array must be of the same type.
And each element can be accessed directly.
We'll see some other array characteristics shortly,
but first, let's see why we use arrays and why they're so useful.
Suppose we need to model my test scores in a class.
That's pretty easy. We've already seen how to use variables and this would be a perfect use of variables.
So suppose I have three test scores that I need to track,
and they're all graded as integers out of a 100.
I can simply declare three variables as integers, give them meaningful names,
initialize them to 0 or to whatever values I wish.
But what if I need to model more than just a few test scores.
Suppose I'm a school administrator, and I need to model the test scores for
100 students,
so that I could do some statistics on them.
Modeling each one of them as its own variable with its own name
can quickly get out of control.
Now I have to keep track of 100 variable names.
You could still do it but it becomes tedious and error prone.
What if I had 10,000 test scores to model? You get the idea.
This is a typical use case for an array
and that's when they're really, really useful.
Arrays allow you to tell the compiler
to give you a collection of as many elements as you want
and give the entire collection a single name.
So you no longer have to name each individual element.
Let's go over some of the characteristics or properties of arrays.
Arrays are fixed in size. Once you create an array of a specific size,
the size cannot change.
So if you create an array of 100 test scores and then you need space for more than that,
you'd have to go back to your source code, change the max size and recompile.
The size of the array is fixed. Arrays will not grow or shrink on their own while
the program is running.
As mentioned earlier, all the elements of an array must be of the same type.
So we can have arrays of integers, arrays of doubles, arrays of persons,
arrays of accounts and so forth.
Arrays are stored contiguously in memory.
So if you tell the compiler that you need an array of 100 integers,
the compiler will generate code that allocates exactly 100 integers
that are contiguous in memory.
That means that the memory will be allocated as one chunk.
Array elements are individually accessible
so I can access any test score that I wish.
You can do that using the position or index of that array element.
So I can tell the compiler to give me the second element in the array
or the 1500th element in the array.
Be aware that array indexes start at 0.
This means that the first element in an array is at position 0, not at position 1.
And the last element is at position size -1.
You can see an example in the figure on the right.
Here we have an array of 10 test scores.
The array is named test scores and it's indexed from 0 to 9.
Many, many programming languages have compound data types like arrays,
and the majority of them start their array indexes at 0.
I know that most of us start counting at 1. With arrays, you must start at 0.
This is a common cause of errors and programs, especially with beginning programmers.
It's also important to understand that c++
arrays are not bounds checked.
That means that there's no check to make sure that when you access an individual array element
that it's between 0 and size -1.
It's the programmer's responsibility to be sure that you don't go over bounds.
If you go overbound, your program may have strange behavior and could even crash.
Finally, in a few sections, we'll talk about iteration or looping.
As you can imagine, looping is used all the time with array since we often need to loop
through the arrays and process every element.
Just like variables, always initialize the arrays
or they'll contain unknown values.
Arrays are extremely efficient in c++ since they are
bare bones raw arrays.
Later in this section, we'll talk about c++ vectors,
which are super powerful dynamic and safer to use in arrays.
In the next video, we'll see how to declare arrays.