Index
Array
An array is a collection of elements of the same data type, stored in contiguous memory locations. Arrays are useful for managing large amounts of data, as they allow you to group related values together and access them efficiently using an index.
Why use array?
Imagine you need to store the marks of 5 students. Without arrays, you’d create separate variables:
int mark1, mark2, mark3, mark4, mark5;
This is cumbersome, especially if the number of elements is large. Arrays simplify this:
int marks[5]; // Declares an array of 5 integers
Syntax for Declaring an Array
data_type array_name[array_size];
- data_type: The type of data the array will hold (e.g.,
int
,float
,char
). - array_name: The name of the array.
- array_size: The number of elements the array can hold.
Example 1:
Imagine you have a group of 5 students, and you want to store their exam marks. Instead of creating separate variables for each student’s mark, you use an array named marks
to store all their marks.
#include<stdio.h>
int main(){
int marks[5] = {90, 85, 88, 92, 78};
// Accessing array elements
printf("First student's mark: %d\n", marks[0]); // Output: 90
// Modifying array elements
marks[2] = 95; // Changes the third student's mark to 95
printf("Updated third student's mark: %d\n", marks[2]); // Output: 95
return 0;
}
Output:
First student's mark: 90
Updated third student's mark: 95
Explanation:
- Array initialization: You can initialize an array at the time of declaration:
int marks[5] = {90, 85, 88, 92, 78};
2. Accessing Elements: Use the array name with an index.
printf("First student's mark: %d\n", marks[0]);
3. Modifying Array Elements: Change the student marks.
marks[2] = 95; // Changes the third student's mark to 95
printf("Updated third student's mark: %d\n", marks[2]); // Output: 95
Example 2:
#include <stdio.h>
int main() {
int numbers[5]; // Declare an array of integers with size 5
// Initialize the array
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
// Print the elements of the array
for (int i = 0; i < 5; i++) {
printf("Element %d: %d\n", i, numbers[i]);
}
return 0;
}
Output:
Element 0: 10
Element 1: 20
Element 2: 30
Element 3: 40
Element 4: 50
Explanation:
- Declaration:
int numbers[5];
declares an array namednumbers
that can hold 5 integers. - Initialization: Each element of the array is assigned a value using its index (e.g.,
numbers[0] = 10;
). - Accessing Elements: A
for
loop is used to iterate through the array, printing each element along with its index.
Initialization at Declaration
You can also initialize an array at the time of declaration:
int numbers[5] = {10, 20, 30, 40, 50}; // Declaration and initialization
Multidimensional Arrays
C also supports multidimensional arrays, such as 2D arrays (arrays of arrays), which can be visualized as tables or grids.
Example: Declaring a 2D Array
#include <stdio.h>
int main() {
int matrix[3][3] = { // Declare a 3x3 matrix
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Print the elements of the 2D array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n"); // Move to the next line after each row
}
return 0;
}
Output:
1 2 3
4 5 6
7 8 9
Explanation of the 2D Array Example:
- Declaration:
int matrix[3][3];
declares a 2D array (matrix) with 3 rows and 3 columns. - Initialization: The matrix is initialized with values in a nested manner.
- Nested Loops: Two
for
loops are used to access and print each element of the 2D array.