Simple Arrays_two Dimensional, Multidimensional and Dynamic Arrays COMPLETE NOTES
Simple Arrays_two Dimensional, Multidimensional and Dynamic Arrays COMPLETE NOTES
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;
That will take you a long time to type. This is where arrays come in handy. An easier way is to
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.
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:
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;
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
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]
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;
Note: As you can see, we use two “for loops” in the example above. One to access the rows the
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
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.
#include<iostream>
using namespace std;
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
CODES TO TRY
a. STATIC ARRAYS
CODE 1. Simple Static Array
#include <iostream>
using namespace std;
int main()
{
int numbers[5], sum = 0;
cout << "Enter 5 numbers: ";
return 0;
}
#include <iostream>
#include <iomanip>
using std::setw;
int main () {
return 0;
Code 3. C++ program to ask 10 numbers from user and display the
sum.
#include <iostream>
#include <conio.h>
int main()
int arr[10],sum=0,i;
cout<<"Enter 10 numbers"<<endl;
for(i=0;i<10;i++)
cin>>arr[i];
sum = sum+arr[i];
cout<<"Sum = "<<sum;
getch();
return 0;
}
int main () {
cout << "a[" << i << "][" << j << "]: ";
return 0;
b. DYNAMIC ARRAYS
void main()
{
int Num;
DynArray(Num);
}
void DynArray(int Num)
{
int *Num_ptr; //Pointer function
Num_ptr=new int[Num];
#include<iostream.h>
void main()
{
int **a,m,n; // double pointer for 2d arry
Num_ptr=new int[Num];
DynArray(Num);