An Array is a sequential collection of elements, of the same data type. They are stored sequentially in memory. An Array is a data structure that holds similar type of elements. The array elements are not treated as objects in c++ like they are in java. In this this article we would discuss Arrays In C++.
Arrays In C++
Imagine you are at a music record store and I tell you to arrange all the records under the label XYZ Records at one place one above the other. This sequential collection of records can be called an Array. An array is a sequential collection of elements of the same data type. In our example above, XYZ Records is the data type and all the records you collected have the same publishers. All the elements in an array are addressed by a common name.
This article on Arrays in C++ will focus these base pointers,
- Single Dimensional Array
- Initialization During Declaration
- Initialization By A User
- Accessing Array At Any Point
- Multi Dimensional Array
Let us start by understanding what are single dimensional arrays,
Single Dimensional Array
Syntax for declaring a Single Dimensional Array:
We have a data type that can be any of the basic data types like int, float or double. Array Name is the name of the array and we declare the size of the array. In our above example, the array will be,
XYZ Record recordArray[100];
Let’s consider another example:
int test[20];
The array test will hold the elements of type int and will have a size 20.
Arrays In C++: Array Size
Array size is given at the time of declaration of the array. Once the size of the array is given it cannot be changed. The compiler then allocates that much memory space to the array.
Consider the Example
int test[20];
In the example above, we have an array test, of type int. We have given the array size to be 20. This means that 20 consecutive memory locations will be left free for the array in the memory.
Array Index And Initialization
A number associated with each position in an array and this number is called the array index. Its starts from 0 and to the last element, that is the size of the array minus one. The minus one is there because we start counting from zero and not one. Array indices always begin from zero.
Consider this example, this is the age array.
Array Value | 12 | 41 | 03 | 13 | 07 |
Array Indices | 0 | 1 | 2 | 3 | 4 |
Here the array contains the values 12,41,3,13,7 and the indices are 0,1,2,3,4,5. If we want to represent an element at index 4 it is represented as age[4] and the value 7 will be displayed.
By default, the array contains all zero values. Array initialization is done at the time of declaration. This can also be carried out later if the user enters the array value as and when needed.
Let us see how initialization works during declaration,
Initialization During Declaration
An array can be initialized during declaration. This is done by specifying the array elements at the time of declaration. Here the array size is also fixed and it is decided by us.
Consider the code,
#include<iostream> using namespace std; int main() { int arr[] = { 10, 20, 30, 40 } ; return 0; }
Explanation
In the above example, we create an array of type int and with the name arr. We directly specify the array elements. The size of the array is decided by counting the number of elements in our array. In this case, the size is 4.
Next in this article on Arrays in C++ lets us see how Initialization by a user works
Initialization By A User
In this method, we let the user decide the size of the array. In this case, we need a variable to hold the size of the array and a for loop to accept the elements of the array. We assign a random size at the time of declaration and use only as needed. The size at the start is usually at the higher side. We have a variable i to control the for loop.
Consider the example,
#include<iostream> using namespace std; int main() { int arr[50],n,i ; cout<<"Enter the size of array:"<<endl; cin>>n; cout<<"Enter the elements of array:"<<endl; for(i=0;i<n;i++) { cin>>arr[i]; } return 0; }
Output
Explanation
In the above program, we declare an array of size 50. We then ask the user to enter the number of elements he wishes to enter in this array. We then accept the array elements entered by the user.
Build stunning cross-platform mobile apps with our comprehensive Flutter Training.
Arrays In C++: Displaying the Array
Displaying the array also requires the for-loop. We traverse to the entire array and display the elements of the array.
Here is an example,
#include<iostream> using namespace std; int main() { int arr[50],n,i ; cout<<"Enter the size of array:"<<endl; cin>>n; cout<<"Enter the elements of array:"<<endl; for(i=0;i<n;i++) { cin>>arr[i]; } cout<<"Array elements are:"<<endl; for(i=0;i<n;i++) { cout<<arr[i]<<"t"; } return 0; }
Output
Explanation
In the above program, we declare an array of size 50. We then ask the user to enter the number of elements he wishes to enter in this array. We then accept the array elements entered by the user. We then use a for loop again to display the array elements.
Moving on with this arrays in C++ article,
Accessing Array At Any Point
Accessing array elements is simple and is done by using the array index. Have a look at the code below.
#include<iostream> using namespace std; int main() { int arr[5],i ; arr[4]=2; arr[2]=17; arr[0]=17; cout<<"Array elements are:"<<endl; for(i=0;i<5;i++) { cout<<arr[i]<<"t"; } return 0; }
Output
Explanation
In the program above, we have an array of size 5. We enter elements at different locations using array index. We print the array to get the above output.
By Default, all the array elements are zero.
What happens if we cross the array size?
In c++, if we try to access the elements out of bound, error may not be shown by the compiler but we will not get proper output.
This bring us to the final bit of this arrays in C++ article,
Multi Dimensional Array
Arrays of arrays are multi-dimensional arrays. This is because each element in a multi-dimensional array has an array of its own. We need n for loops to iterate through a multidimensional array depending on the dimensions.
Syntax For Declaring Multi Dimensional Arrays
Datatype arrayname[size1][size2]…..[size n]
int a[10][20];
Consider the example,
The size of the above array will be 10*20 that is 200 elements. Similarly, we can have two or three or even more dimensional arrays. Each dimension requires one for loop. So, the two-dimensional array requires two- and three-dimensional array requires three.
Consider the code
#include<iostream> using namespace std; int main() { int arr[3][2] = {{0,1}, {2,3}, {4,5}}; for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { cout << "Element:"; cout <<arr[i][j]<<endl; } } return 0; }t
Output
Explanation
In the above code, we display a 3*2 matrix. This array has 3 rows and 2 columns. We have 2 for loops. Each responsible for one dimension of the array. The outer for loop takes care of rows and inner of columns.
Similarly, we can write a code for three-dimensional array and there will be three for loops and each dimension will be controlled by one for loop.
Thus we have come to an end of this article on ‘Arrays in C++’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible