Synfire's Guide to C++

7. Arrays

So far, we have covered the basics of C++ but now we are getting into some more advanced stuff. By now you should be able to write a basic program to do some basic tasks, VERY basic tasks. But now you are embarking into what separates the pretend programmers from the real deal.

In the following sections you will learn some fundamental techniques that all real programmer must use to create those kick ass programs we all love. In fact the DEC algorithm was created with mostly just a set of Matrices, or arrays of arrays. So lets venture on, Here you will learn about Arrays, how to define and use them.

An array is like a collection of variables called elements. Each element can hold information. An array looks like 'ArrayName[Element]', the element is a number, or variable representing a number. The elements of an array start from 0. This is very important to remember. In other words, 'Array[0]' is the first element of 'Array[]' not 'Array[1]'. Now this may sound confusing so let me try to explain it My Way!

A variable is like a box. You can put your information in that box and use it as you need to. An array is like that same box, but with dividers inserted to allow you to have many boxes in one. Each of these boxes are labeled with the name of the box, and the number 0-whatever. Just like variables you define the array with a type. Each box within this box can only hold values of that type. Get it? Good. Now I will give an example code of a basic array.

#include <iostream.h>

int Grades[200];
// creates an array with 200 elements

int main()
    long i; // creates 'i' as an element reference variable
    long Aver, Added; // creates other variables needed

    cout << "Please enter the grade of each student." << endl;
    cout << "to end press enter without input" << endl;
    // this just tells the user that they can show that
    // they are done by hitting enter on a blank line

    for (i = 0; Grades[i] != NULL; i++)
    {  // uses i to loop through the array until the user
       // enters a blank line, NULL means that there is no
       // value.

     cout << "> "; // prints a prompt for the user to use
               // This is just for looks.
     cin >> Grades[i]; // assigns the input to the element

    for (i = 0; Grades[i] != NULL; i++)
    { // once again we start at the beginning and use i to loop
      // through the array until it comes to an empty one.

     Added = Added + Grades[i];
     // Okay this is a trick I saw once, this allows the
     // elements to be added together fairly easy basically
     // Grades[0] is added to Added, then the sum is placed
     // into Added, overwriting the old value, then Grades[1]
     // is added to that and the sum once again overwrites the
     // Added variable, this is repeated as long as needed.
    Aver = i / Added;
     // The final step is dividing the amount of elements
     // by their sum.
    cout << "The class average is: " << Aver;
    return 0;
WoW, just by introducing arrays we have now written a program to average an entire classes, up to 200 students, grades together. So are you still having the feeling that C++ is hard?? And you know what else, We are done with the basics of arrays. If you don't fully understand arrays, or you don't understand what I just showed you, go over this section a couple of times. This is important and easy stuff. Once you read it a couple of times you will be asking yourself how you could have gotten confused...

So what now, Well how about the Matrix! NO NOT THE MOVIE! :-/ A matrix is an array with in an array. Confused? Okay a matrix is also called a multidimensional array. This is an array with two element groups that work together. example...

int myMatrix[10][10];
Okay this created a matrix with 100 elements separated into groups of 10. How a bout a visual?
myMatrix[10][10] is the same as:       
Matrices give you the ability to create a complete 100 element array with an easier nicer looking display. Instead of just having 100 elements lined up one right after another you can have them stacked in groups that allow for a lot of different uses. Look at it like this, lets say we had a chess game and we wanted to move a piece from point a-c to b-c, with a normal array you would have to have all sorts of calculations of placements but with the use of the matrix you can do this by just assigning the letters a numerical value 0-8 and just placing them in the matrix then passing both positions to a move() function:
move(chess[a][c], chess[b][c])

easy huh!, just use them like arrays but in groups and you should have no problems.

In this section you learned how to create an use arrays and matrices.

< < < Lesson 6: Functions | Lesson 8: Character Arrays > > >

Fancy learning C, C++ or Perl? The have a look in our tutorials section.