Saturday, July 24, 2010

C++: arrays and Vector basics

Starting from this point on, I'll be using a lot of tech terms in
programming, along with terms that you've never seen before or the ones that
have different meaning in C++. But because of the nature of these notes,
I'll try my best to be friendly when explaining those terms. The first of
these is what's called arrays and vectors, which are used to store a group
of related data as a single package.

An array is a contiguous data. In layman's terms, it's a "line" of data as
though one data follows another, like a train. To help us identify the
actual data we are working with, a new concept of an index will be used. An
index is the location where the actual data resides. For instance, in a set
of even numbers from 2 to 10 (2, 4, 6, 8, 10), the fourth index would house
the number 8.

Here are few things you need to know about arrays: To declare an array, we
need not only the type of data we'll be using, but also the number of such
data we'll need. For instance, if we want to declare an integer variable to
store five even numbers from 2 to 10, we would write:
int even_numbers[5] = {2, 4, 6, 8, 10};
There are quite a few things we haven't seen before. The square brackets
([]) is read as subscript operator. So if we want to declare any data type
with any number of them, we would put the number of data within square
brackets. The one after the assignment operator would be the contents of
that array, enclosed in curly braces ({}) and each of them separated by a
comma.

There are other ways of declaring such variables. One of the popular ways is
using the following:
char characters[] = {"a", "b"};
Notice that we didn't say anything about the number of items within the
brackets. However, when we compile this code, the compiler would know that
we've just declared an array of two characters, namely A and B. So, in our
case, both
char characters[2] = {"a", "b"};
and
char characters[] = {"a", "b"};
are perfectly valid.

To access an item in an array, we need a special concept we've discussed a
bit earlier: using 0-based indexing. So the first item in an array would
have index of zero, followed by index of one, two, and so forth. The problem
here is that arrays, unlike vectors which I'll describe a bit later, does
not allow you to resize it i.e. does not allow you to change its size,
although it is possible to change what's stored on it using its index.

For instance, using the even number array above, if you want to change the
fifth item from 10 to 12, we can do something like this: First, create an
integer variable named "i" to store index locations. Then set this variable
to 4 (the fifth element); as you have noticed, we need to put 4, not 5. Then
assign 12 to the value stored on the fifth index (index 4).
In C++, this is done like this:
int i; // The index.
i = 4; // Assign the index location to 4 to access the fifth element.
even_number[i] = 12;
At least for now. There are other ways of doing it, but because of time and
space, we'll leave it at that.

Well, at least that's the basics of arrays. In the next installment, I'll
introduce you to so-called vectors, and enhancement of arrays. After
vectors, we'll get into the basic foundation of "reusable code of
convenience" - functions.
// JL

No comments:

Post a Comment