Operations on Array

In C programming, you can manipulate and process arrays' data using a number of operations. The following are some typical operations you can perform out on arrays:

Initialization: Using a loop or direct assignment, you can initialize an array with values at the time of declaration or at a later time.

int numbers[4] = {1, 2, 3, 4};

Accessing Elements: Using square brackets and the element's index, you can access a specific element of an array. In C, array indices begin at 0.

int fElement = numbers[0];  // Get to the first element
int tElement = numbers[2]; // Get to the third element

Modification: You can change the values of array elements by assigning new values to them with the assignment operator.

numbers[2] = 6;  // Modify the third element

Traversal: Using loops like "for" or "while," you can traverse (visit) every element in an array and use out operations on each one.

for (int j = 0; j < 6; j++) {
    printf("%d ", n[j]); // Each element should be printed.
}

Finding the Length: By dividing the array's size in bytes by the size of one element, you can calculate an array's length (number of elements).

int length = sizeof(number) / sizeof(number[2]);

Searching: By iterating through the elements of the array and comparing them, you can search for a specific value within it.

int sValue = 10;
int fd = 0;

for (int i = 0; i < length; i++) {
    if (numbers[i] == sValue) {
        fd = 1;
        break; // If the value is found, end the loop.
    }
}

if (fd) {
    printf("Value %d found in the array.\n", sValue);
} else {
    printf("Value %d not found in the array.\n", sValue);
}

Sorting: Using sorting algorithms like bubble sort, selection sort, or library functions like "qsort," you can arrange the elements of an array in either ascending or descending order.