arrays in c++
arrays in c++
To declare an array , you must specify the data type of the elements it
will hold, followed by the array name and the size of the array within
square brackets [ ].
Syntax : dataType arrayName[arraySize];
int Array[5];
We can also initialize the array at the time of declaration by providing
a comma-separated list of values enclosed in curly braces { }. The
number of elements inside the curly braces should match the array
size
int Array[5] = {10, 20, 30, 40, 50};
Accessing Array Elements:
You can access individual elements of an array using their index. The
index notation is arrayName[index], where index is an integer
representing the position of the element you want to access. Array
index start from 0.
For example, to access the first element of array, you would use
array[0], and to access the third element, you would use array[2].
int firstElement = Array[0]; // Retrieves the first element (10)
int thirdElement = Array[2]; // Retrieves the third element (30)
Looping Through an Array:
If we declare an array of size 10, then the array will contain elements
from index 0 to 9.
However, if we try to access the element at index 10 or more than 10,
it will result in Undefined behaviour.
Searching:
The process of locating a specific element within a collection of
elements (e.g: in an array).
PURPOSE:
To determine if a particular item exists and, if so, to find its location.
Sorting:
Sorting is a concept in which the elements of an array are rearranged
in a specific order.
This order can be from lowest to highest or highest to lowest.
Sorting an unsorted array helps to solve many problems such as
searching for the minimum or maximum elements.
Multidimensional Arrays: