 Practice
Resources
Contests
Online IDE
New
Free Mock
Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Scaler
Explore Offerings by SCALER ## Begin your success journey!

Full name *
Email *

By creating an account, I acknowledge that I have read and agree to InterviewBit’s Terms and Privacy Policy . ## Welcome back!

Email * # Array

An `array` is a collection of similar data types under the same name.

In C++, arrays are declared in the following way​:

`//dataType arrayName[arraySize];int arr; //int is the datatype, arr is the name, 5 is the size of array`

The above statement declares a `static` array of 5 elements, which can be accessed individually.

Note: Although arr behaves like a pointer, its value cannot be changed as it references a specific region in memory.

#### Initializing​ Arrays

`int main() {    int arr = {19, 10, 5, 6, 14}; //initializing the array with 5 values    cout << "The value of arr, that is, the first value in the array is: " << arr << endl;    cout<< "The value of arr, that is, the second value in the array is: " << arr << endl;    cout<< "The value of arr, that is, the third value in the array is: " << arr << endl;    cout<< "The value of arr, that is, the fourth value in the array is: " << arr << endl;    cout<< "The value of arr, that is, the fifth value in the array is: " << arr << endl;    int arr2[] = {1,2,3,4}; //we don't specify the size and the compiler assumes a size of 4      //we calculate size of the arr2 using the inbuilt sizeof function     //divide the total size of the array by the size of the array element to claculate the size    cout << "The size of arr2 is: "<<sizeof(arr2)/sizeof(arr2)<<endl;                             }`

In the above example, we have an array `arr` of size 5. We use indexing to refer to the location of the values in an array. For example:

• The value 19 is present at the index 0 of the array. This is written as `arr` = 19. Here, by using `arr` we are referring to the location of 19.
• Similarly, the index of the value 10 in the array will be 1. It’ll be written `arr` = 10.
• The index of the value 5 is 2 in the array. So `arr` = 5.

Note: Arrays have 0 as the first index not 1.

#### Accessing Array Values

We use indexing to access arrays values just like we did in the example above.

Suppose you declared an array of 15 elements.

You can use the array members from `arr` to `arr`.

Note: If you try to access array elements outside of its bound, let’s say `arr`, the compiler may not show any error. However, this may cause unexpected output (undefined behavior).

`int arr = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};cout << "The value of arr is: "<< arr <<endl;// We will get a garbage value at arr which is an index that is out of bounds.`

#### Iterate over array

We can also iterate over the whole array and access any value easily using any loop.
Below is an example that use `for` loop to iterate over the array.

`int main() {    int arr = {}; // no values stored in array by default    int num = 1;    for (int i=0; i<5; i++){        arr[i] = num; //setting index i of array arr equal to num        num++;        //incrementing num        cout << "The value of arr["<<i<<"] is equal to: "<< arr[i]<<endl;    }    return 0;}`

Try the following example in the editor below.

Complete the given function named ‘reverseArray’ containing an integer array ‘arr’ and length of arr N. Reverse the array ‘arr’. Hints
• Complete Solution

## Discussion Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Select an option *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
*Enter the expected year of graduation if you're student
Current Employer *
Company Name * 