C++ arrays, arrays and loops

In this tutorial, we are going to talk about arrays.

An array lets you declare and work with a collection of values of the same type. Let’s say you want to declare four integers. With the knowledge from the last few tutorials you would do something like this:

	int a , b , c , d;

What if you wanted to declare a thousand variables?
That will take you a long time to type. This is where arrays come in handy. An easier way is to declare an array of four integers, like this:

	int a[4];

The four separate integers inside this array are accessed by an index. Each element can be accessed, by using square brackets, with the element number inside. All arrays start at element zero and will go to n-1. (In this case from 0 to 3.)

Note: The index number, which represents the number of elements the array is going to hold, must be a constant value. Because arrays are build out of non-dynamic memory blocks. In a later tutorial we will explain arrays with a variable length, which uses dynamic memory.

So if we want to fill each element you get something like this:

	int a[4];
	a[0] = 1;
	a[1] = 2;
	a[2] = 3;
	a[3] = 4;

If you want to use an element, for example for printing, you can do this:

	cout << a[1];

Arrays and loops

One of the nice things about arrays is that you can use a loop to manipulate each element. When an array is declared, the values of each element are not set to zero automatically.

In some cases you want to “re-initialize” the array (which means, setting every element to zero). This can be done like in the example above, but it is easier to use a loop. Here is an example:

	using namespace std;

	int main()
		int a[4];
		int i;

		for ( i = 0; i < 4; i++ )
			a[i] = 0;
		for ( i = 0; i < 4; i++ )
			cout << a[i] << '\n';
		return 0;

Note: In the first “for loop” all elements are set to zero. The second “for loop” will print each element.

Multi-dimensional arrays

The arrays we have been using so far are called one-dimensional arrays. Here is an example of a one-dimensional array:

     int a[2];





Note: A one-dimensional array has one column of elements.

Two-dimensional arrays have rows and columns. See the example below:

     int a[2][2];




1 2


4 5

Note: a[0][0] contains the value 1. a[0][1] contains the value 2. a[1][0] contains the value 4. a[1][1] contains the value 5.

So let’s look at an example that initialize a two-dimensional array and prints each element:

	using namespace std;

	int main()
		int a[4][4];
		int i , j;

		for (i = 0; i < 4; i++)
			for ( j = 0; j < 4; j++)
				a[i][j] = 0;
				cout << a[i][j] << '\n';
		return 0;

Note: As you can see, we use two “for loops” in the example above. One to access the rows the other to access the columns.

You must be careful when choosing the index number, because there is no range checking done. So if you index (choose an element) past the end of the array, there is no warning or error. Instead the program will give you “garbage” data or it will crash.

Arrays as parameters

In C++ it is not possible to pass a complete block of memory by value as a parameter to (for example) a function. It is allowed to pass the arrays address to (for example) a function.

Take a look at the following example:

	using namespace std;

	void printfunc(int my_arg[], int i)
		for (int n=0; n < i; n++)
			cout << my_arg[n] << '\n';

	int main()
		int my_array[] = {1, 2, 3, 4, 5};
		return 0;

The function printfunc accepts any array (whatever the number of elements) whose elements are of the type int. The second function parameter (int i) tells function the number of elements of the array, that was passed in the first parameter of the function. With this variable we can check (in the “for” loop) for the outer bound of the array.

That’s all for this tutorial.

This entry was posted in C++ Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

There are currently 17 responses to “C++ arrays, arrays and loops”

Why not let us know what you think by adding your own comment!

  1. Tech on November 21st, 2009:

    Nice guide. This will teach about anyone what an array is and how to make one.

  2. terry on October 5th, 2011:

    I must say this is one of the best and easiest tut’s on the net

    Huge Help !

  3. Cosmos on May 10th, 2012:

    Should not
    for (int n=0; n < i; int i)


    for (int n=0; n < i; int n++)?

  4. admin on May 12th, 2012:

    @cosmos – Nice catch, corrected the problem. But also the second int is not correct, it should be: for (int n=0; n < i; n++). But thank you for the correction!

  5. aziz ali nagpurwala on June 28th, 2012:

    nice help for clearing concepts thank u

  6. Javed on September 10th, 2012:

    i want to acess all element in a 3*3
    matrix using one for loop only?

  7. Guru on November 6th, 2012:

    Can we print a multidimensional array using any other loops apart from “for”loop ????

  8. eoaneh on May 11th, 2013:

    how can we have an array that does not need an specific length for example for n numbers?

  9. Alrajabi on May 20th, 2013:

    Thanx alot i have an exam tomorrow and i can`t understand arrays 😀

  10. Mohammed on August 1st, 2013:

    i get it, but i need more about Array.

  11. Sunil on August 30th, 2013:

    Thanks for this help, This website in very much applicable for students.

  12. shankar on September 23rd, 2013:

    why is it that an array a[0] and 0[a] giving the same value when u try to cout it?

  13. maish on October 16th, 2013:

    nice tutorial.thanks

  14. Hera Hashmat on December 13th, 2013:

    Thanx for this information.

  15. Yahya on January 20th, 2014:

    Anyone help me…
    how to Initialize two integer arrays of five elements. Assign value from user to first array, then copy those elements in reverse order in second array. Display elements of second array????

  16. hassan on January 21st, 2014:

    very helpful……thanks

  17. wonde man on January 30th, 2014:

    10q for give me understandable idea about array,i have a exam after three days & am sure that am raedy for the exam .