back
Using Arrays in C++
An array allows you to use one variable name to describe many objects. This is extrememly useful to you as a programmer because you don't have to come up with millions of names for all your variables. Imagine you are writing a program for a large company and you need to make references to each employee. Instead of making one variable for each employee, you can create an array named "employee" and use only that. The reason arrays work is because of something called indexing. This is how you would create an array named "employee" that would work for three employees:

string employee[3];
employee[0]="Sam Spade";
employee[1]="Humprey Dolittle";
employee[2]="Dolly Doright";

We created an array named "employee" with three elements. Notice how the first element is always a zero rather than a one. The way arrays work is that they are stored consecutively in the computer's memory. You will see later that this turns out to be quite useful when we learn about pointers. This is how it looks in the memory of the computer:


As you can see the elementsof the array are contiguous (side-to-side).

Take a look at this sample of an array: Array.

Assignment
Create an array with five elements and have the program use a for loop to output them back in order.