In C programming, an array is a collection of elements of the same data type, stored in contiguous memory locations. Arrays allow you to store multiple values in a single variable, making it easier to manage and manipulate groups of related data. Each element in an array can be accessed using an index, which is an integer value that represents the position of the element within the array.
Key Characteristics of Arrays in C
- Fixed Size: The size of an array must be defined at the time of declaration and cannot be changed during runtime.
- Homogeneous Elements: All elements in an array must be of the same data type (e.g., all integers, all floats).
- Zero-Based Indexing: Array indexing in C starts from 0. The first element is accessed with index 0, the second with index 1, and so on.
Declaring and Initializing Arrays
To declare an array in C, you specify the data type, followed by the array name and the size in square brackets. Here are some examples:
1. Declaration of an Array
2. Initialization of an Array
You can initialize an array at the time of declaration:
int numbers[5] = {1, 2, 3, 4, 5}; // Initializes the array with values
If you do not specify the size, the compiler will determine it based on the number of initializers:
int numbers[] = {1, 2, 3, 4, 5}; // Size is automatically set to 5
Accessing Array Elements
You can access and modify elements in an array using their index:
#include <stdio.h>
int main() {
int numbers[5] = {10, 20, 30, 40, 50};
// Accessing elements
printf("First element: %d\n", numbers[0]); // Output: 10
printf("Second element: %d\n", numbers[1]); // Output: 20
// Modifying an element
numbers[2] = 100; // Change the third element to 100
printf("Modified third element: %d\n", numbers[2]); // Output: 100
return 0;
}
Example: Using Arrays in C
Here’s a complete example that demonstrates how to declare, initialize, and manipulate an array in C:
#include <stdio.h>
int main() {
// Declare and initialize an array of integers
int scores[5] = {85, 90, 78, 92, 88};
int sum = 0;
// Calculate the sum of the scores
for (int i = 0; i < 5; i++) {
sum += scores[i]; // Add each score to sum
}
// Calculate the average
float average = sum / 5.0;
// Print the results
printf("Sum of scores: %d\n", sum);
printf("Average score: %.2f\n", average);
return 0;
}
Explanation of the Example
Declaration and Initialization: The array
scores
is declared and initialized with five integer values representing scores.Looping Through the Array: A
for
loop is used to iterate through the array elements. The loop variablei
serves as the index to access each element.Calculating the Sum: Each score is added to the
sum
variable.Calculating the Average: The average is calculated by dividing the total sum by the number of elements (5).
Output: The program prints the sum and average of the scores.
Multidimensional Arrays
C also supports multidimensional arrays, which are arrays of arrays. The most common type is the two-dimensional array, which can be thought of as a table or matrix.
No comments:
Post a Comment
If you have any doubts, please let me know