C++ Arrays Tutorial
C++ Arrays Tutorial
In this article, you will learn to work with arrays. You will learn to declare, initialize
and, access array elements in C++ programming.
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.
Suppose you declared an array mark as above. The first element is mark[0], second
element is mark[1] and so on.
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}
#include <iostream>
using namespace std;
int main()
{
int numbers[5], sum = 0;
cout << "Enter 5 numbers: ";
return 0;
}
Output
Enter 5 numbers: 3
4
2
Sum = 18
int testArray[10];
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];
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.
Better way to initialise this array with same array elements as above.
int test[2][3] = { {2, 4, 5}, {9, 0 0}};
int test[2][3][4] = {
};
#include <iostream>
using namespace std;
int main()
{
int test[3][2] =
{
{2, -5},
{4, 0},
{9, 1}
};
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
#include <iostream>
using namespace std;
int main()
{
int temperature[CITY][WEEK];
cout << "Enter all temperature for a week of first city and then
second city. \n";
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