DATA STRUCTURES _ C++ ARRAYS – STATIC AND DYNAMIC 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.
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: ";
// Storing 5 number entered by user in an array
// Finding the sum of numbers entered
for (int i = 0; i < 5; ++i)
{
cin >> numbers[i];
sum += numbers[i];
}
cout << "Sum = " << sum << endl;
return 0;
}
Code 2. Static Array
#include <iostream>
using namespace std;
#include <iomanip>
using std::setw;
int main () {
int n[ 10 ]; // n is an array of 10 integers
// initialize elements of array n to 0
for ( int i = 0; i < 10; i++ ) {
n[ i ] = i + 100; // set element at location i to i + 100
cout << "Element" << setw( 13 ) << "Value" << endl;
// output each array element's value
for ( int j = 0; j < 10; j++ ) {
cout << setw( 7 )<< j << setw( 13 ) << n[ j ] << endl;
return 0;
Code 3. C++ program to ask 10 numbers from user and display the
sum.
#include <iostream>
#include <conio.h>
using namespace std;
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;
}
Code4. Multi-dimensional static array
#include <iostream>
using namespace std;
int main () {
// an array with 5 rows and 2 columns.
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
// output each array element's value
for ( int i = 0; i < 5; i++ )
for ( int j = 0; j < 2; j++ ) {
cout << "a[" << i << "][" << j << "]: ";
cout << a[i][j]<< endl;
return 0;
For one dimensional, two dimensional and multidimensional arrays,
read
https://www.programtopia.net/cplusplus/docs/arrays
b. DYNAMIC ARRAYS
CODE 1: dynamic list simplified
#include <iostream.h>
void DynArray(int Num);
void main()
{
int Num;
cout<<"\nPlease enter your number"; cin>>Num;
DynArray(Num);
}
void DynArray(int Num)
{
int *Num_ptr; //Pointer function
Num_ptr=new int[Num];
for (int i=0; i<Num;++i)
{
cout<<"\nMy "<<i+1<<" number is :"; cin>>Num_ptr[i];
}
for (i=0; i<Num;++i)
{
cout<<"\nMy Numbers are "<<Num_ptr[i];
}
}
CODE 2: pointers and arrays
#include<iostream.h>
void main()
{
int **a,m,n; // double pointer for 2d arry
cout<<" \n enter the number of rows and columns for matrix:";
cin>>m>>n;
a=new int *[m]; // dynamic allocation of pointers array for rows
for(int i=0;i<m;i++)
a=new int*[n]; // dynamic allocation of colomns for each rows
cout<<"\n enter the elements:";
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
cin>>*a[j];
cout<<"\n the elements r:";
for(i=0;i<m;i++)
{
for(int j=0;j<n;j++)
cout<<a[j]<<" ";
cout<<"\n";
}
CODE 3: DYNAMIC ARRAY SIMPLIFIED
#include <iostream.h>
void DynArray(int Num)
{
int *Num_ptr;
Num_ptr=new int[Num];
for (int i=0; i<Num;++i)
{
cout<<"\nMy "<<i+1<<" number is :"; cin>>Num_ptr[i];
}
for (i=0; i<Num;++i)
{
cout<<"\nMy Numbers are "<<Num_ptr[i];
}
}
void main()
{
int Num;
cout<<"\nPlease enter your number"; cin>>Num;
DynArray(Num);