Programming & Scripting Tutorials

C++: Arrays

Arrays, in C++, are simply arrangements of 'objects' -- in the basic usage we'll be using in this tutorial, we will be using arrays to simply store lists of data (of a certain datatype). You can think of basic arrays, such as the ones we will be using in this tutorial, like tables with only one or two row(s) but multiple columns. I say "one or two rows" because each piece of data stored (whether it be an integer, string, or something entirely different) has a number assigned to it that it can be referenced by. This is called the index, and the index number simply counts up from 0 as the array gets bigger – so the first element in the array would have the index of 0, the second the index of 1, etc.

How I visualise arrays. This example is an array with some integer data.

Obviously storing data in this tabular-like manor is very useful – a classic example that is usually given is pupils' scores in a test. Perhaps each student got a score out of 100 for their test – this data would be best stored in an integer array. In the real world the scores would probably be recorded in a text file or something similar, but we could always build in functionality to read this file and then store that data in an array inside our application.

Arrays can be created in C++ very much like "normal variables" (with a datatype followed by a name), but square brackets should be present after the array name to indicate that what you are creating is in fact an array and not just a variable. If the array needs to have a set size to it (maybe ours would have one entry for each of the 30 students in the class), then this can be specified inside of the square brackets. Taking all of this into account, we could declare an integer array in which 30 entries can be made, with the following:

int ClassScores[30];

It's important to remember that the index number is counted from 0, so if we were to try to visualise this array it would have index numbers from 0 to 29. We can proceed to initialise the various elements in the array by writing the array name (in this case, "ClassScores") , followed by the index number of the element we wish to target in square brackets, followed by an equals sign and then the value we wish to set it to (and then obviously a semi-colon to end the line). So if we wanted to initialise the first element (of index 0) in our array to 15, we could write the following:

ClassScores[0] = 15;

The same could also be done for the scores of the other members of the class (elements of the array from index 0 to index 29). If we then wanted to use these values later (for example if we wanted to cout one or all of the elements), we can access a certain element of the array just as we did when we were assigning values to each element – by writing the array name and then the index number in square brackets. So we could output the first element in the array (remember, this is the one with the index of 0!) by writing something like cout << ClassScores[0];.

You may have noticed when we learnt how to initialise the elements in arrays earlier, that the process was extremely long and drawn out (imagine having to initialise hundreds of array elements!) - luckily there is an easier way to initialise the elements in an array. Instead of individually setting each element to a certain value (which can be done at any point in the program, not just at element initialisation) we can actually initialise the elements when we declare the array! This method of initialisation is accomplished by simply shoving an equals sign after the declaration of the array and then specifying the different array elements, with commas separating them, in curly brackets. This method doesn't require any value in the square brackets either as the compiler can calculate how many elements we are initializing and set the array size to that! To show this method of initialisation, let's just set some values for each score in the class at the array declaration – let's cut it down to 20 this time for the sake of simplicity:

int ClassScores[] = { 15, 30, 55, 62, 45, 63, 12, 7, 80, 98, 11, 90, 17, 57, 12, 70, 20, 32, 66, 20 };

With the array declared and initialized, we can do a whole bunch of stuff with it! A nice example might be outputting all of the students' scores using a while loop (ideally we'd use a for loop, but we haven't learn about those yet). When outputting many numbers it can get confusing for the user, so it might be a nice idea for the application to also number each score it outputs -- what better way to do this than make use of the index number! We've covered everything necessary to create this program – if you feel like a challenge, try to create it all by yourself, if not, feel free to take a look at the code I whipped up below.

#include <iostream> //For 'cout'
#include <conio.h> //For 'getch'

using namespace std;

int main()
{
	int ClassScores[] = { 15, 30, 55, 62, 45, 63, 12, 7, 80, 98, 11, 90, 17, 57, 12, 70, 20, 32, 66, 20 }; //Declare and initialise our 'ClassScores' array
	int i = 0; //Declare and initialise an iterator variable that we will use to control the while loop

	while(i < 20) //While our iterator variable is less than 20
	{
		cout << i+1 << ": "<< ClassScores[i] << endl; //Output the element of ClassScores with an index of 'i' along with the number of elements we're outputted so far
		i++; //Increment (add one to) 'i'

	}

	getch();
	return 0;
}

If you're feeling really daring and want more of a challenge – try making the program also associate names with each score and output these along with or instead of the index number (there are a number of ways to do this – if you're stuck, try thinking about the idea of adding another row to the "table").

This C++ tutorial was written by


Back to C++

Advertisement: