Dynamic Array

Examples

C

#include <stdio.h>
#include <stdlib.h>

#define ARRAY_INITIAL_SIZE 100;

typedef struct {
    int size;
    int max;
    int *data;
} Dynamic_array;

void array_initialize(Dynamic_array* array) {
    array->size = 0;
    array->max = ARRAY_INITIAL_SIZE;
    array->data = malloc(sizeof(int) * array->max);
}

void array_append(Dynamic_array* array, int val) {
    if (array->size >= array->max) {
        array->max *= 2;
        array->data = realloc(array->data, sizeof(int) * array->max);
    }
    array->data[array->size++] = val;
}

void array_set(Dynamic_array* array, int index, int val) {
    while (index >= array->size) {
        array_append(array, 0);
    }
    array->data[index] = val;
}

int array_get(Dynamic_array( array, int index) {
    if (index >= array->size || index < 0) {
        printf("Error: Index %d out of bounds for array of size%d\n", index, array->size);
        exit(1);
    }
    return array->data[index];
}

void array_free(Dynamic_array* array) {
    free(array->data);
}

int main(int argc, char *argv[]) {
    Dynamic_array array;
    int i;
    array_initialize(&array);
    for (i = 0; i < 200; i++) {
        array_append(&array, i * 2);
    }
    array_set(&array, 300, 600);
    array_free(&array);
    return 0;
}

C

#define VECTOR_INITIAL_CAPACITY 100

typedef struct {
    int size;
    int capacity;
    int *data;
} Vector;

void vector_init(Vector *vector);
void vector_append(Vector *vector, int value);
int vector_get(Vector *vector, int index);
void vector_set(Vector *vector, int index, in value);
void vector_double_capacity_if_full(Vector *vector);
void vector_free(Vector *vector);
#include <stdio.h>
#include <stdlib.h>
#include "vector.h"

void vector_init(Vector *vector) {
    vector->size = 0;
    vector->capacity = VECTOR_INITIAL_CAPACITY;
    vector->data = malloc(sizeof(int) * vector->capacity);
}

void vector_append(Vector *vector, int value) {
    vector_double_capacity_if_full(vector);
    vector->data[vector->size++] = value;
}

int vector_get(Vector *vector, int index) {
    if (index >= vector->size || index < 0) {
        printf("Index %d out of bounds for vector of size %d\n", index, vector->size);
        exit(1);
    }
    return vector->data[index];
}

void vector_set(Vector *vector, int index, int value) {
    while (index >= vector->size) {
        vector_append(vector, 0);
    }
    vector->data[index] = value;
}

void vector_double_capacity_if_full(Vector *vector) {
    if (vector->size >= vector->capacity) {
        vector->capacity *= 2;
        vector->data = realloc(vector->data, sizeof(int) * vector->capacity);
    }
}

void vector_free(Vector *vector) {
    free(vector->data);
}

C++

class DynamicArray {
public:
    DynamicArray();
    DynamicArray(int initSize);
    DynamicArray(const DynamicArray &original);
    ~DynamicArray();

    void insert(int position, int value);
    int get(int position);
    int getSize();
    void resize(int newSize);
    int &operator[](int index);
    bool operator==(DynamicArray);
    bool operator!=(DynamicArray);

private:
    int size;
    int *array;
};
#include "dynamicarray.h"

using namespace std;

DynamicArray::DynamicArray() {
    DynamicArray::DynamicArray(5);
}

DynamicArray::DynamicArray(int initSize) {
    size = initSize;
    array = new int[size];
    for (int i = 0; i < size; i++) {
        array[i] = 0;
    }
}

DynamicArray::DynamicArray(const DynamicArray &original) {
    size = original.size;
    array = new int[size];
    for (int i = 0; i < size; i++) {
        array[i] = original.array[i];
    }
}

DynamicArray::~DynamicArray() {
    delete[] array;
}

void DynamicArray::insert(int position, int value) {
    array[position] = value;
}

int DynamicArray::get(int position) {
    return array[position];
}

int DynamicArray::getSize() {
    return size;
}

void DynamicArray::resize(int newSize) {
    int *temp;
    temp = new int[newSize];
    for (int i = 0; i < (newSize); i++) {
        temp[i] = array[i];
    }
    delete[] array;
    array = temp;
    size = newSize;
}

bool DynamicArray::operator==(DynamicArray a) {
    if (a.size != size) return false;
    for (int i = 0; i < (a.size); i++) {
        if (a[i] != array[i]) return false;
    }
}

bool DynamicArray::operator!=(DynamicArray a) {
    if (a.size != size) return true;
    for (int i = 0; i < (a.size); i++) {
        if (a[i] != array[i]) return true;
    }
}

int &DynamicArray::operator[](int index) {
    if ((index - 1) > size) {
        resize(index + 1);
    }
    return array[index];
}
#include <iostream>
#include "dynamicarray.h"

using namespace std;

int main(int argc, char **argv) {
    DynamicArray array = DynamicArray(4);
    array.insert(1, 7);
    array.insert(3, 2);
    array[2] = 4;
    array[10] = 4

    DynamicArray array2 = DynamicArray(array);

    return 0;
}