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:
#include<iostream>
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];
0 |
1 |
1 |
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];
|
0 |
1 |
0 |
1 | 2 |
1 |
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:
#include<iostream>
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:
#include<iostream>
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};
printfunc(my_array,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.
Nice guide. This will teach about anyone what an array is and how to make one.
I must say this is one of the best and easiest tut’s on the net
Huge Help !
Should not
for (int n=0; n < i; int i)
be
for (int n=0; n < i; int n++)?
@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!
nice help for clearing concepts thank u
i want to acess all element in a 3*3
matrix using one for loop only?
Can we print a multidimensional array using any other loops apart from “for”loop ????
how can we have an array that does not need an specific length for example for n numbers?
Thanx alot i have an exam tomorrow and i can`t understand arrays 😀
i get it, but i need more about Array.
Thanks for this help, This website in very much applicable for students.
SUNIL
why is it that an array a[0] and 0[a] giving the same value when u try to cout it?
nice tutorial.thanks
Thanx for this information.
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????
very helpful……thanks
10q for give me understandable idea about array,i have a exam after three days & am sure that am raedy for the exam .