Arrays

================

Definition


An array is an ordered collection of elements of the same data type stored in contiguous memory locations. It is a data structure that allows for efficient storage and manipulation of collections of objects or values.

Syntax


The basic syntax of an array is as follows:

type arr_name[element_count];
  • type is the data type of the elements in the array.
  • arr_name is a name given to the array.
  • element_count is the number of elements in the array.

Characteristics


Array Length

The length of an array is determined by its declaration. An array can have a fixed or dynamic length, which means it can be resized at runtime if necessary.

Fixed-Length Arrays

A fixed-length array has a specific maximum size that must be specified when it’s declared:

int myArray[10];  // declares an array of size 10 with type int

In this case, the maximum number of elements that can fit in myArray is 10.

Dynamic-Size Arrays

A dynamic-size array has a variable length, which means it can be resized at runtime:

int* myArray = new int[5];  // declares an array of size 5 with type int

In this case, the maximum number of elements that myArray can hold is 5.

Operations


Array Indexing

Array indexing allows you to access individual elements of the array using their index:

int myArray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
printf("%d", myArray[0]);  // prints 1

Array Manipulation

You can modify elements of an array using its index:

int myArray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
myArray[0] = 10;  // modifies the first element to be 10

Array Sorting

You can sort an array using various algorithms:

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

// bubble sort algorithm
for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5 - i - 1; j++) {
        if (myArray[j] > myArray[j + 1]) {
            int temp = myArray[j];
            myArray[j] = myArray[j + 1];
            myArray[j + 1] = temp;
        }
    }
}

// selection sort algorithm
int sortedArray[10] = {3, 1, 4, 2, 5};
for (int i = 0; i < 5; i++) {
    int minIndex = i;
    for (int j = i + 1; j < 10; j++) {
        if (myArray[j] < myArray[minIndex]) {
            minIndex = j;
        }
    }
    int temp = myArray[i];
    myArray[i] = myArray[minIndex];
    myArray[minIndex] = temp;
}

// insertion sort algorithm
int sortedArray[10] = {3, 1, 4, 2, 5};
for (int i = 1; i < 10; i++) {
    int key = myArray[i];
    int j = i - 1;
    while (j >= 0 && myArray[j] > key) {
        myArray[j + 1] = myArray[j];
        j--;
    }
    myArray[j + 1] = key;
}

Data Structures


Arrays

Arrays are a fundamental data structure that provide efficient storage and manipulation of collections of objects or values.

Uses of Arrays

  • Storing large datasets in memory
  • Performing element-wise operations on arrays
  • Implementing dynamic algorithms using arrays

Conclusion


In conclusion, arrays are a powerful data structure that provide efficient storage and manipulation of collections of objects or values. Their syntax is simple to understand, and they offer various operations for indexing, modification, sorting, and data structures.

Example Use Cases:

  • Storing customer information in a database
  • Representing coordinates on a 2D plane
  • Implementing matrix multiplication algorithms

References


  • “Arrays” by the C++ Standard Library
  • “Data Structures and Algorithms” by George V. Williams, David A. Patterson, John L. Hennessy, et al.
  • “The C Programming Language” by Brian W. Kernighan and Dennis Ritchie