What is an Array?
An array is a collection of similar data types stored in contiguous memory locations.
Example:
int marks[5] = {90, 85, 78, 92, 88};
- Types of Arrays:
- One-Dimensional Array
- Two-Dimensional Array
- Multidimensional Array (rare in beginners)
1D Array Example:
int a[3] = {10, 20, 30};
printf(“%d”, a[1]); // Output: 20
- Looping Through an Array:
for (int i = 0; i < 5; i++) {
printf(“%d “, marks[i]);
}
- 2D Array Example:
int matrix[2][2] = {
{1, 2},
{3, 4}
};
Access element:
printf(“%d”, matrix[0][1]); // Output: 2