One-Dimensional Arrays
Definition and Usage
A one-dimensional array is a linear data structure that stores multiple elements of the same data type, arranged in contiguous memory locations. Each element in the array is accessed by its index, starting from 0 up to the size of the array minus one. One-dimensional arrays are widely used when there is a need to manage a list of similar items in a structured way, such as storing a list of student marks, temperatures recorded over a week, or daily sales figures. Arrays are useful for tasks that involve repetitive data handling, as they allow access and manipulation of data items in a structured manner through indexed positions.
Declaration and Initialization
Arrays need to be declared and can optionally be initialized when declared. This allows reserving space in memory to store a defined number of elements.
Declaration:
The syntax for declaring an array specifies the data type, the array name, and the number of elements it can hold (size):
data_type array_name[size];
Example:3
#include <stdio.h>
int main() {
int marks[5]; // Declaration of an integer array of size 5
return 0;
}
Output
Initialization:
Arrays can also be initialized with specific values at the time of declaration. This step assigns predefined values to each element in the array.
Example:2
#include <stdio.h>
int main() {
int marks[5] = {10, 20, 30, 40, 50}; // Initializing array with values
return 0;
}
Output
Accessing Array Elements:
Each element in an array can be accessed by using its index number within square brackets. Array indices start from 0, so the first element is at index 0, the second at index 1, and so on.
Example:3
#include <stdio.h>
int main() {
int marks[5] = {10, 20, 30, 40, 50};
printf("First element: %d\n", marks[0]); // Accessing the first element
return 0;
}
Output
-
Indexing:
- Arrays are zero-indexed.
- The first element is accessed using index
0
, the second using1
, and so on. - Example:
array_name[0]
accesses the first element.
-
Initialization:
- Static Initialization:
int arr[3] = {10, 20, 30};
-
Dynamic Initialization: Declare the array, then assign values later.
int arr[3];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
- Static Initialization:
-
Accessing Elements:
- Use the subscript operator
[]
. - Example:
printf("%d", arr[2]);
prints the third element.
- Use the subscript operator
-
Uninitialized Arrays:
- If not initialized, an array contains garbage values.
-
Fixed Size:
- The size of an array is fixed at the time of declaration.
Example 4: Taking Input in an Array
#include <stdio.h>
int main() {
int n = 5, arr[5];
printf("Enter %d elements:n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("You entered:n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output
Common Operations on Arrays
1: Finding Sum Of array elements
#include <stdio.h>
int main() {
int size, sum = 0;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]); // Input array elements
}
// Calculate the sum of array elements
for (int i = 0; i < size; i++) {
sum += arr[i];
}
printf("The sum of the array elements is: %dn", sum);
return 0;
}
Output
2: Finding the Largest Element in Array
#include <stdio.h>
int main() {
int size, max;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements:n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]); // Input array elements
}
// Find the largest element
max = arr[0];
for (int i = 1; i < size; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
printf("The largest element is: %d\n", max);
return 0;
}
Output
3: Reversing the Array
#include <stdio.h>
int main() {
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
int arr[size];
printf("Enter %d elements:n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]); // Input array elements
}
// Print the array in reverse order
printf("The reversed array is:n");
for (int i = size - 1; i >= 0; i--) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}