An Array is a Data Structure which holds a sequential set of data.

  • Size: how many items are stored in the array
  • Index: the location of a piece of data in the array

Operations that can be done on an Array:

  • Read: Look up a piece of data at a particular spot in the array

  • Search: Find if a particular piece of data exists in the array and it’s location (index)

  • Insert: Adding a piece of data to our array, at an index

  • Delete: Remove a piece of data from our array - usually we give index of value to delete

    Arrays can be statoc sized or dynamic. As arrays are a fixed in memory, the properties of this in RAM is fast constant access to indexes to read or write. If you want to insert and that changes size of the array then it gets more expensive.

OperationBig O
Read or Write to specific indexO(1)
Insert at endO(1)
Insert at start or middleO(n)

In C Language you need to specify the size of the array in advance or statically declare it.

block-beta
	columns 5
	block:VM0
		columns 1
		v0["4"]
		i0["0"]
	end
	block:VM1
		columns 1
		v1["5"]
		i1["1"]
	end
	block:VM2
		columns 1
		v2["6"]
		i2["2"]
	end
    block:VM3
		columns 1
		v3["7"]
		i3["3"]
	end
	block:VM4
		columns 1
		v4["8"]
		i4["4"]
	end

Here is both approaches:

    #include <stdio.h>
    
    int main() {
    
        printf("\n");
    
        int a[] = {1,2,3,4,5};
        int b[5];
    
        b[0] = 6;
        b[1] = 7;
        b[2] = 8;
        b[3] = 9;
        b[4] = 10;
    
    
        for(int i=0; i<=4; i++){
            printf("a[%d] = %d\n", i, a[i]);
        }
    
        printf("\n");
    
        for(int i=0; i<=4; i++){
            printf("b[%d] = %d\n", i, b[i]);
        }
    
        return 0;
    }
   
a[0]=1
a[1]=2
a[2]=3
a[3]=4
a[4]=5
   
b[0]=6
b[1]=7
b[2]=8
b[3]=9
b[4]=10

In languages like Python, they use dynamic arrays, so we don’t have to fix the size in advance:

arr = [1, 2, 3, 4, 5]
arr[1] = 3
arr.append(9)

for a in arr:
    print(a)

None

In this case, we can use arr.append() method to increase the size of the array without having to specify a new larger array like we would have to do in C.