codescracker


c++

C++ Arrays



« Previous Tutorial Next Tutorial »


An array is simply a collection of variables of the same data type that are referenced by a common name. In other words, when elements of linear structures are represented in the memory by means of contiguous memory locations, these linear structures are called arrays.

An array stores a list of finite number (n) of homogeneous data elements (i.e., data elements of the same type). The number n is called length or size or range of an array.

Calculate Array Size in C++

When upper bound (UB) and lower bound (LB) of an array are given, then its size can be calculated as follows :

Array_Size = UB - LB + 1

where UB indicates Upper Bound and LB indicates Lower Bound. For example, if an array has elements numbered as:

-7, -6, -5, ....0, 1, 2, ....15

then its UB is 15 and LB is -7 and array size can be calculate like this:

= 15 - (-7) + 1
= 15 + 7 + 1
= 23

Note - In C++, the lower bound is always 0 and the upper bound is size-1. (size specifies the number of elements in the array).

In C++, all arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. Arrays are a way to group a number of items into a largest unit. Arrays can have data items of simple types like int or float, or even of user-defined types like structures and objects.

Types of Arrays in C++

There are basically two types of arrays: one-dimensional array and multi-dimensional arrays. You can think multi-dimensional arrays as many types of arrays such as two-dimensional arrays, three-dimensional arrays and so on. You will learn one-dimensional array and two-dimensional array in detail in the following separate chapters. In chapter two-dimensional array, you will also learn about multi-dimensional arrays.

Declare Arrays in C++

Here is the general form to declare an array in C++ :

type array_name[array_size];

Above is the general form of the single or one dimensional array declaration. Here type is any valid type of C++, array_name is a valid identifier which is the name of the array given by the programmer, and the array_size represents the number of elements that this array can hold. Here is an example declaring the array named arr of type int and size 20 :

int arr[20];

Here is the general form to declare two-dimensional array in C++.

type array_name[array_size1][array_size2];

Here is the general form to declare multi-dimensional array in C++

type array_name[array_size1][array_size2][array_size3]....[array_sizeN];

Initialize Arrays in C++

Here is the general form to initialize arrays in C++:

type array_name[array_size] = {values_list};

Here is an example, initializing values to the array named arr in C++:

int arr[5] = {97, 69, 18, 46, 83};

Note - The number of values between braces { } can not be larger than the array size (here 5) or the number of elements (here 5).

If you omit the array size, then the array becomes big enough to hold the initialization is created. Therefore, if you write:

int arr[] = {97, 69, 18, 46, 83};

You will create exactly the same array as you did in the previous example. So, you can also initialize more or less than 5 values like this:

int arr[] = {10, 12, 23};

or

int arr[] = {12, 23, 34, 35, 45, 33, 10, 2, 54};

Assign Values to Specific Array Element in C++

Here is the general form to assign values to specific array elements:

array_name[index_number] = value;

Here is an example, assigning a value, 20, to the 5th element in the array named arr:

arr[4] = 20;

Note - Since, index always starts from 0, so index number 4, corresponds to the 5th element in the array.

Access Array Elements in C++

An element can be accessed by indexing the array name. This is done by placing the index number of the element within the square brackets after the name of the array. Here is the general form to access the array element in C++:

type variable_name = array_name[index_number];

Here is an example, accessing the values present at the index number 4 of the array arr:

int num = arr[4];

So, if value at arr[4] (value present at index 4, in the array arr) is 20, then 20 will initialize to the variable num.

C++ Arrays Example

Here are some example demonstrating the concept of arrays in C++ practically.

/* C++ Arrays - C++ Arrays Example Program */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int arr1[5] = {1, 2, 3, 4, 5};
	int arr2[] = {10, 20, 30, 40, 50};
	int i;
	cout<<"Array arr1 contains:\n";
	for(i=0; i<5; i++)
	{
		cout<<arr1[i]<<"\t";
	}
	cout<<"\n\n";
	cout<<"Array arr2 contains:\n";
	for(i=0; i<5; i++)
	{
		cout<<arr2[i]<<"\t";
	}

	getch();
}

Here is the sample output of the above C++ program:

c++ arrays

Here is an another array example.

/* C++ Arrays - C++ Arrays Example Program */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int arr[10];
	int i;
	cout<<"Enter 10 array elements: ";
	for(i=0; i<10; i++)
	{
		cin>>arr[i];
	}
	cout<<"\nArray contains:\n";
	for(i=0; i<10; i++)
	{
		cout<<arr[i]<<"  ";
	}
	getch();
}

Below is the sample run of this C++ program:

arrays in c++

Let's make above C++ program more user-friendly.

/* C++ Arrays - C++ Arrays Example Program */

#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int arr[100];
	int i, count=0;
	cout<<"Enter array elements(max 100) Press -1 to stop: ";
	for(i=0; i<100; i++)
	{
		cin>>arr[i];
		if(arr[i] == -1)
		{
			getch();
			break;
		}
		count++;
	}
	cout<<"\nArray contains:\n";
	for(i=0; i<count; i++)
	{
		cout<<arr[i]<<"  ";
	}
	getch();
}

Here are the two sample runs of the above C++ program:

c++ array

array in c++

More Examples

Here are some more array example programs listed, that you can go for:


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test