One-Dimensional Arrays are collections of elements of the same type. They can be used to store multiple data items, and all data elements can be accessed via a single index. The size of an array is fixed and cannot be changed once defined.
1. Definition and Initialization of One-Dimensional Arrays
Definition:
In C language, a one-dimensional array can be defined as follows:
type array_name[size];
- •
<span>type</span>: The data type of the array elements (e.g.,<span>int</span>,<span>float</span>,<span>char</span>, etc.). - •
<span>array_name</span>: The name of the array. - •
<span>size</span>: The number of elements in the array.
Initialization:
A one-dimensional array can be initialized at the time of definition. For example:
int arr[5] = {1, 2, 3, 4, 5}; // Initialize all elements of the array
If the array is not fully initialized, C language will automatically assign the value <span>0</span> to uninitialized elements.
int arr[5] = {1, 2}; // Initialize the first two elements, remaining elements are 0
Note: The index of the array starts from <span>0</span>, so the index range of the array <span>arr[5]</span> is from <span>arr[0]</span> to <span>arr[4]</span>.
2. Accessing Array Elements
Accessing array elements is done through indices. For example, if we define an array <span>arr[5]</span>, we can access each element via <span>arr[0]</span>, <span>arr[1]</span>, <span>arr[2]</span>, etc.
Example Code:
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
// Access array elements
printf("arr[0] = %d\n", arr[0]); // Output first element
printf("arr[1] = %d\n", arr[1]); // Output second element
printf("arr[2] = %d\n", arr[2]); // Output third element
return 0;
}
Output:
arr[0] = 10
arr[1] = 20
arr[2] = 30
3. Iterating Through the Array with Loops
Elements in the array can be iterated through using a <span>for</span> loop to print all elements.
Example Code:
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
// Use loop to iterate through the array
for (int i = 0; i < 5; i++) {
printf("arr[%d] = %d\n", i, arr[i]); // Output array elements
}
return 0;
}
Output:
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
4. Arrays as Function Parameters
Arrays can be passed as parameters to functions. Since arrays in C are passed by pointer, it is necessary to pass the size of the array to ensure the function can correctly access all elements.
Example Code:
#include <stdio.h>
// Function definition: pass array and array size
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[5] = {10, 20, 30, 40, 50};
// Call function, passing array and size
printArray(arr, 5);
return 0;
}
Output:
10 20 30 40 50
5. Common Operations on One-Dimensional Arrays
- • Sum of the Array: The sum of all elements in the array can be calculated using a loop.
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int sum = 0;
// Calculate the sum of array elements
for (int i = 0; i < 5; i++) {
sum += arr[i];
}
printf("Sum of array elements = %d\n", sum);
return 0;
}
Output:
Sum of array elements = 15
- • Maximum and Minimum Values of the Array: The maximum or minimum value can be found by iterating through the array.
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 5, 30, 15};
int max = arr[0]; // Assume the first element is the maximum
int min = arr[0]; // Assume the first element is the minimum
// Iterate through the array to find max and min
for (int i = 1; i < 5; i++) {
if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}
printf("Max value = %d\n", max);
printf("Min value = %d\n", min);
return 0;
}
Output:
Max value = 30
Min value = 5
6. Summary
- • Definition and Initialization: A one-dimensional array is defined using
<span>[]</span>and can be initialized with a fixed set of values. - • Accessing Array Elements: Elements in the array are accessed via indices, starting from
<span>0</span>. - • Iterating Through the Array: A
<span>for</span>loop can be used to iterate through the array and perform operations on each element. - • As Function Parameters: Arrays can be passed as function parameters, and it is important to pass the size of the array.
- • Common Operations: Operations such as summation, finding maximum, and minimum values can be performed by iterating through the array.