Вы находитесь на странице: 1из 14

C++ Arrays

In this article, you will learn to work with arrays. You will learn to declare, initialize
and, access array elements in C++ programming.

In programming, one of the frequently arising problem is to handle numerous data of


same type.

Consider this situation, you are taking a survey of 100 people and you have to store
their age. To solve this problem in C++, you can create an integer array having 100
elements.

An array is a collection of data that holds fixed number of values of same type. For
example:

int age[100];

Here, the age array can hold maximum of 100 elements of integer type.

The size and type of arrays cannot be changed after its declaration.
How to declare an array in C++?
dataType arrayName[arraySize];

For example,

float mark[5];

Here, we declared an array, mark, of floating-point type and size 5. Meaning, it can hold
5 floating-point values.

Elements of an Array and How to access


them?
You can access elements of an array by using indices.

Suppose you declared an array mark as above. The first element is mark[0], second
element is mark[1] and so on.

Few key notes:


 Arrays have 0 as the first index not 1. In this example, mark[0] is the first
element.
 If the size of an array is n, to access the last element, (n-1) index is used. In this
example, mark[4] is the last element.
 Suppose the starting address of mark[0] is 2120d. Then, the next address, a[1],
will be 2124d, address of a[2] will be 2128d and so on. It's because the size of a
float is 4 bytes.

How to initialize an array in C++


programming?
It's possible to initialize an array during declaration. For example,

int mark[5] = {19, 10, 8, 17, 9};

Another method to initialize array during declaration:

int mark[] = {19, 10, 8, 17, 9};

Here,

mark[0] is equal to 19

mark[1] is equal to 10

mark[2] is equal to 8

mark[3] is equal to 17

mark[4] is equal to 9
How to insert and print array elements?
int mark[5] = {19, 10, 8, 17, 9}

// insert different value to third element


mark[3] = 9;

// take input from the user and insert in third element


cin >> mark[2];

// take input from the user and insert in (i+1)th element


cin >> mark[i];

// print first element of an array


cout << mark[0];

// print ith element of an array


cin >> mark[i-1];

Example: C++ Array


C++ program to store and calculate the sum of 5 numbers entered by the user
using arrays.

#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;
}

Output

Enter 5 numbers: 3

4
2

Sum = 18

Things to remember when working with


arrays in C++
Suppose you declared an array of 10 elements. Let's say,

int testArray[10];

You can use the array members from testArray[0] to testArray[9].

If you try to access array elements outside of its bound, let's say testArray[14], the
compiler may not show any error. However, this may cause unexpected output
(undefined behavior).
C++ Multidimensional Arrays

In this article, you'll learn about multi-dimensional arrays in C++. More specifically, how to declare
them, access them and use them efficiently in your program.

In C++, you can create an array of an array known as multi-dimensional array. For
example:

int x[3][4];

Here, x is a two dimensional array. It can hold a maximum of 12 elements.


You can think this array as table with 3 rows and each row has 4 columns as shown
below.

Three dimensional array also works in a similar way. For example:

float x[2][4][3];

This array x can hold a maximum of 24 elements. You can think this example as: Each
of the 2 elements can hold 4 elements, which makes 8 elements and each of those 8
elements can hold 3 elements. Hence, total number of elements this array can hold is
24.

Multidimensional Array Initialisation


You can initialise a multidimensional array in more than one way.

Initialisation of two dimensional array


int test[2][3] = {2, 4, -5, 9, 0, 9};

Better way to initialise this array with same array elements as above.
int test[2][3] = { {2, 4, 5}, {9, 0 0}};

Initialisation of three dimensional array


int test[2][3][4] = {3, 4, 2, 3, 0, -3, 9, 11, 23, 12, 23,

2, 13, 4, 56, 3, 5, 9, 3, 5, 5, 1, 4, 9};

Better way to initialise this array with same elements as above.

int test[2][3][4] = {

{ {3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2} },

{ {13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9} }

};

Example 1: Two Dimensional Array


C++ Program to display all elements of an initialised two dimensional array.

#include <iostream>
using namespace std;

int main()
{
int test[3][2] =
{
{2, -5},
{4, 0},
{9, 1}
};

// Accessing two dimensional array using


// nested for loops
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 2; ++j)
{
cou t<< "test[" << i << "][" << j << "] = " << test[i][j] <<
endl;
}
}

return 0;
}

Output

test[0][0] = 2

test[0][1] = -5

test[1][0] = 4

test[1][1] = 0

test[2][0] = 9
test[2][1] = 1

Example 2: Two Dimensional Array


C++ Program to store temperature of two different cities for a week and display it.

#include <iostream>
using namespace std;

const int CITY = 2;


const int WEEK = 7;

int main()
{
int temperature[CITY][WEEK];

cout << "Enter all temperature for a week of first city and then
second city. \n";

// Inserting the values into the temperature array


for (int i = 0; i < CITY; ++i)
{
for(int j = 0; j < WEEK; ++j)
{
cout << "City " << i + 1 << ", Day " << j + 1 << " : ";
cin >> temperature[i][j];
}
}
cout << "\n\nDisplaying Values:\n";

// Accessing the values from the temperature array


for (int i = 0; i < CITY; ++i)
{
for(int j = 0; j < WEEK; ++j)
{
cout << "City " << i + 1 << ", Day " << j + 1 << " = " <<
temperature[i][j] << endl;
}
}

return 0;
}

Output

Enter all temperature for a week of first city and then second city.

City 1, Day 1 : 32

City 1, Day 2 : 33

City 1, Day 3 : 32

City 1, Day 4 : 34

City 1, Day 5 : 35

City 1, Day 6 : 36

City 1, Day 7 : 38

City 2, Day 1 : 23
City 2, Day 2 : 24

City 2, Day 3 : 26

City 2, Day 4 : 22

City 2, Day 5 : 29

City 2, Day 6 : 27

City 2, Day 7 : 23

Displaying Values:

City 1, Day 1 = 32

City 1, Day 2 = 33

City 1, Day 3 = 32

City 1, Day 4 = 34

City 1, Day 5 = 35

City 1, Day 6 = 36

City 1, Day 7 = 38

City 2, Day 1 = 23

City 2, Day 2 = 24

City 2, Day 3 = 26

City 2, Day 4 = 22
City 2, Day 5 = 29

City 2, Day 6 = 27

City 2, Day 7 = 23

Вам также может понравиться