Arrays
In computer programming, an array is a data structure that stores a fixed-size sequence of elements of the same type. It is used to organize and manipulate collections of data in a systematic way.
Arrays can be one-dimensional, two-dimensional (also known as matrices), or multidimensional, depending on the number of indices required to access elements. In a one-dimensional array, each element is accessed using a single index. In a two-dimensional array, elements are arranged in rows and columns and accessed using two indices—one for the row and one for the column.
Arrays are commonly used for tasks such as storing a collection of numbers, characters, or objects, iterating over the elements, and performing operations on them, such as searching, sorting, or modifying their values. They provide efficient and direct access to elements based on their index, making them a fundamental tool in programming.
In sort summary-:
• Array is a collection of similar data types grouped under one name
• It's also called vector value.
• We can access or differentiate all the elements in an array using index values.
• This concepts is supported by many programming languages.
Example :
0 | 1 | 2 | 3 | 4 |
---|
15 |
---|
int A[ 5 ] ;
0 | 1 | 2 | 3 | 4 |
---|
? | ? |
---|
int A[ 5 ] = {2,4,6,8,10} ;
0 | 1 | 2 | 3 | 4 |
---|
2 | 4 | 6 | 8 | 10 |
---|
0 | 1 | 2 | 3 | 4 |
---|
2 | 4 | 0 | 0 | 0 |
---|
0 | 1 | 2 | 3 | 4 |
---|
0 | 0 | 0 | 0 | 0 |
---|
int A[ ] = {2,4,6,8,10} ;
0 | 1 | 2 | 3 | 4 |
---|
2 | 4 | 6 | 8 | 10 |
---|
• To access all elements in an array , we can traverse through it, for example
int A[ 5 ] = {2,4,6,8};
for (i = 0; i < 5 ; i++) {
printf( “%d”, A[ i ] );
}
• The elements inside the array can be access through the subset or through the pointer
for (i = 0; i < 5 ; i++) {
printf( “%d”, A[ i ] );
printf( “%d”, A[ 2 ] );
printf( “%d”, 2[ A ] );
printf( “%d”, *(A + 2 ) );
}