7. DSA with C++ - Dynamic Arrays

Dynamic Arrays in C++

Dynamic Array Creation: To create a dynamic array, you need to use the `new` operator to allocate memory on the heap. The size of the array can be determined at runtime.

dataType* arrayName = new dataType[size];
    
For example, to create a dynamic integer array of size 5:

int* numbers = new int[5];
    
Dynamic Array Initialization: You can initialize the elements of a dynamic array similar to a static array by using curly braces {}.

dataType* arrayName = new dataType[size]{val1, val2, ..., valN};
    
For example:

int* numbers = new int[5]{1, 2, 3, 4, 5};
    
Dynamic Array Access and Modification: You can access and modify elements of a dynamic array using the array index notation [].

arrayName[index] = newValue; // Modification
dataType element = arrayName[index]; // Access
    
For example:

numbers[2] = 10; // Modifying the value of the third element to 10
int x = numbers[2]; // Accessing the third element of the array
    
Dynamic Array Deallocation: After using a dynamic array, it is important to deallocate the memory to prevent memory leaks. Memory deallocation is done using the `delete[]` operator.

delete[] arrayName;
    
For example:

delete[] numbers;
    
Dynamic Array Resizing: Dynamic arrays can be resized by allocating a new array of the desired size and copying the elements from the old array to the new one. The `new` and `delete[]` operations are used for resizing.

// Create a new dynamic array of newSize
dataType* newArray = new dataType[newSize];
// Copy elements from the old array to the new array
for (int i = 0; i < min(size, newSize); i++) {
 newArray[i] = arrayName[i];
}
// Deallocate the old array
delete[] arrayName;
// Update the array pointer
arrayName = newArray;
    
Dynamic Array Size: Since dynamic arrays can be resized, it's essential to keep track of the current size of the array separately.

Dynamic Array Operations:

  1. Resizing the Dynamic Array
  2. 
    int* resizeArray(int* array, int currentSize, int newSize) {
        int* newArray = new int[newSize];
        int elementsToCopy = min(currentSize, newSize);
        for (int i = 0; i < elementsToCopy; i++) {
            newArray[i] = array[i];
        }
        delete[] array;
        return newArray;
    }
    
      
  3. Appending an Element to the Dynamic Array
  4. 
    int* appendElement(int* array, int& size, int element) {
        int* newArray = new int[size + 1];
        for (int i = 0; i < size; i++) {
            newArray[i] = array[i];
        }
        newArray[size] = element;
        delete[] array;
        size++;
        return newArray;
    }
    
        
  5. Inserting an Element at a Specific Index in the Dynamic Array
  6. 
    int* insertElement(int* array, int& size, int index, int element) {
        int* newArray = new int[size + 1];
        for (int
    
     i = 0; i < index; i++) {
            newArray[i] = array[i];
        }
        newArray[index] = element;
        for (int i = index; i < size; i++) {
            newArray[i + 1] = array[i];
        }
        delete[] array;
        size++;
        return newArray;
    }
    
          
  7. Deleting an Element at a Specific Index from the Dynamic Array
  8. 
    int* deleteElement(int* array, int& size, int index) {
        int* newArray = new int[size - 1];
        for (int i = 0; i < index; i++) {
            newArray[i] = array[i];
        }
        for (int i = index + 1; i < size; i++) {
            newArray[i - 1] = array[i];
        }
        delete[] array;
        size--;
        return newArray;
    }
    
            
  9. Finding the Sum of Array Elements
  10. 
    int sumElements(int* array, int size) {
        int sum = 0;
        for (int i = 0; i < size; i++) {
            sum += array[i];
        }
        return sum;
    }
    
              
  11. Finding the Average of Array Elements
  12. 
    double averageElements(int* array, int size) {
        int sum = sumElements(array, size);
        double average = static_cast(sum) / size;
        return average;
    }
    
                
  13. Finding the Maximum Element in the Array
  14. 
    int maxElement(int* array, int size) {
        int max = array[0];
        for (int i = 1; i < size; i++) {
            if (array[i] > max) {
                max = array[i];
            }
        }
        return max;
    }
    
                  
  15. Finding the Minimum Element in the Array
  16. 
    int minElement(int* array, int size) {
        int min = array[0];
        for (int i = 1; i < size; i++) {
            if (array[i] < min) {
                min = array[i];
            }
        }
        return min;
    }
    
    
  17. Counting the Occurrences of a Specific Element in the Array
  18. 
    int countOccurrences(int* array, int size, int target) {
        int count = 0;
        for (int i = 0; i < size; i++) {
            if (array[i] == target) {
                count++;
            }
        }
        return count;
    }
    
    
  19. Reversing the Elements in the Array
  20. 
    void reverseArray(int* array, int size) {
        int start = 0;
        int end = size - 1;
        while (start < end) {
            // Swap array[start] and array[end]
            int temp = array[start];
            array[start] = array[end];
            array[end] = temp;
            start++;
            end--;
        }
    }