0% found this document useful (0 votes)
8 views8 pages

arrays in c++

arrays all chapter

Uploaded by

mrbossff207
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

arrays in c++

arrays all chapter

Uploaded by

mrbossff207
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

What is an Array?

An array is “a data structure that allows you to store a fixed-size,


contiguous collection of elements of the same data type” OR “an
array is a collection of consecutive memory locations which can
store multiple values of homogenous type”.
Each element in an array is identified by an index, starting from 0 and
going up to ‘array size-1’.
Arrays provide a way to group multiple values under a single name,
making it easier to work with large sets of data.
Declaration and Initialization:

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:

Loops are often used to iterate through the elements of an array. A


common loop to iterate through a 1D array is the for loop, as shown
in the following example:
for (int x = 0; x < 5; x++)
{
cout << Array[x] << “ “; // Prints all elements of the array
}
Array Out of Bounds:

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:

Multidimensional arrays, allows us to create arrays with more than


one dimension (2D, 3D, etc.).
For example, a 2D array can be declared as follows:
int matrix[3][3]; // Declares a 2D array of 3x3 integers
To access elements in a 2D array, we use two indices — one for the
row and another for the column.
 For example, matrix[1][2] will access the element at the second row
and third column.

You might also like