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

No output, as it is just a declaration.

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

No output, as it is an initialization example.

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

First element: 10

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

Enter 5 elements: 1 2 3 5 4 You entered: 1 2 3 5 4

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

Enter the size of the array: 5 Enter 5 elements: 1 2 3 4 5 The sum of the array elements is: 15

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

Enter the size of the array: 5 Enter 5 elements: 1 8 3 4 7 The largest element is: 8

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;
}

Output

Enter the size of the array: 5 Enter 5 elements: 1 2 3 4 5 The reversed array is: 5 4 3 2 1