The simple data types we have considered so far represent single values. In many situations, however, it is necessary to process a collection of values that are related in some way, for example, a list of student marks or a collection of measurements resulting from some experiment.

Processing such collections using only simple variables can be extremely cumbersome and for this reason, most high level languages include data structure known as array.

There are two types of arrays:


One dimensional array

In some cases, it is better to store the data items in a sequence of main memory locations, each of which can be accessed directly. Such a data structure is called an array. In C++, we can refer to each individual item or element in the array by a subscript in brackets.

For example, if the marks of ten students are to be processed in a program, we might use an array to store them. The computer must first be instructed to reserve a sequence of ten memory locations for the marks. The array for storing marks can be defined as

int marks[10];

here int specifies the type of elements or items that will be stored in the array and the word marks is the name of the array. The [10] following the array name tells how many variables of type int will be in our array and brackets tell the compiler that we are dealing with an array.

To refer to an individual element of the array, we use subscripts, the numbers in the brackets following the array name. This number specifies the element’s position in the array. In C++, all the array elements are numbered, starting at 0.

For example, the assignment statement


stores the value 83 in the fifth location of the array because the numbering starts with 0. Thus, the last array element will have subscript one less than the size of the array. That means the array subscript will be in the range of 0 to 9.

Now let us consider the following program that reads marks of ten students and prints the marks that are above average.

# include<iostream>

void main ()


int marks[10], j, sum, avg;



cout<<”enter marks:”;







cout<<”list of marks above average”<<endl;

for(j=0; j<10;j++)




in this program, the first for loop allows the user to enter 10 marks and stores them in the marks array. The second loop finds the sum of all the marks and then the average marks are calculated. Finally the last for loop compares the marks of each student with the average marks and prints those that are greater than average.


Initializing one dimensional array

The following program demonstrates the initializing of an array.

# include<iostream>

# define len 5

Void main()


int a[len]={10,34,16,40,22}, j, sum, avg;

sum =0;


     sum=sum + a[j];


cout<<endl<<”the average of given five numbers is “<<avg;


The above program used the variable len that is a constant and is defined as 5.

int a[len]={10,34,16,40,22}

we can also omit the size of the array, leaving an empty pair of brackets following the array name.

int a[]={10,34,16,40,22};

if the size of the array is not supplied, the compiler will count the number of items in the initialization list and fix that as the array size.

If the number of values supplied is less than the actual number of items in the list then extra spaces in the array will be filled in with zeros. If the number of values supplied is greater than the actual number of items then the compiler will give an error message.


Two dimensional arrays

There are many problems in which the data being processed can be naturally organized as a table. For example, if for each of twenty five different students, four test marks are to be processed in a program, the data can be arranged in a table having twenty five rows and four columns.


In this table, the four marks of student 1 are in the first row, the four marks of student 2 are in the second row, and so on. These one hundred data items can be conveniently stored in a two dimensional array. The declaration

Int marks[25][4];

Reserves one hundred memory locations for these data items. The array element


Refers to the entry in the second row and third column of the table, that is, to the marks 92 earned by 2 students in test 3. In general,


Refers to the entry in the row I and column j, that is, to the marks of student i in test j.


To calculate the average marks in the four tests for each of the twenty five students, it is necessary to calculate the sum of the entries in each row and to divide each of these sums by 4. The following program can be used to carry out this computation and to display the average marks for each student.

# include<iostream>

Void main()


int marks[50][10], I,  j, student, test, sum, avg;

cout<<”enter number of students”;


cout<<enter number of tests”;


cout<<”enter marks of each students in one line”;

for(I=0; I<student;I++)

      for(j=0; j<test; j++)






      sum=sum + marks[I][j];


cout<<”average marks for student  “<<i+1<<” are “<<avg;




Matrix Transpose

The transpose of a two dimensional array is one formed by interchanging the rows and columns of the original matrix. For example, if


Then the transpose of A is


If the matrix has the same number of rows and columns, it is called a square matrix.

The following program reads a square matrix and prints its transpose.


void main()


int i, j, a[10][10], n, holder;

cout<<endl<<“enter size of square matrix(max. 10):”;


cout<<“enter each row in one line”<<endl;

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





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







cout<<endl<<“*****THE TRANSPOSE*****”<<endl;









Initializing Two-Dimensional Array

The following program demonstrates the initializing of a two dimensional array and count the number of positive values in it.


#define row 3

# define col 4

Void main()


int i, j, pos;

int a[row][col]=

{  {4,45,-6,9}, {-8,22,-37,10}, {50,-5,62,85} };






cout<<”the number of positive values = “<<pos;


In the above program note the format used to initialize the array. An outer set of braces and then 3 inner sets of braces, each with 4 items separated by commas as well.