An array is like a row of boxes, each holding a different item.
Each box has a number, starting from 0, to identify its position.
You can quickly find and change the items by using their box
numbers.
Arrays are helpful when you want to keep things organized. For
example, you can use an array to store a list of numbers or
names. It's a simple way to store and access data in a structured
manner. However, remember that arrays have a fixed size, so you
need to know the maximum number of items in advance, and you
can't easily change the size later on.
LINEAR ARRAY MEMORY REPRESENTATION
. In a linear array memory representation, the elements of the
array are stored one after another in a straight line in the
computer's memory.
For example, let's consider an array [10, 20, 30]. Each element
occupies a fixed amount of memory space (let's say 4 bytes).
The memory representation would look like this:
Memory Address | Value
--------------------------------
1000 | 10
1004 | 20
1008 | 30
In this representation, the first element (10) is stored at memory
address 1000, the second element (20) is at address 1004 (4
bytes higher), and the third element (30) is at address 1008
(another 4 bytes higher).
, By using the index of an element, you can calculate its memory
address using the formula:
Memory Address = Starting Address + (Index * Size of Each
Element)
For instance, to find the second element (20), we would calculate:
Memory Address = 1000 + (1 * 4) = 1004
So, the value 20 is stored at memory address 1004.
This linear array memory representation enables us to quickly
access any element by computing its memory address based on
its index.
Syntax
DataType arrayName[arraySize];
DataType: The data type of the elements in the array (e.g., int,
float, char).
arrayName: The name given to the array.
arraySize: The number of elements that the array can hold.
INSERTION AND DELETION OPERATION
Insertion and deletion are common operations performed on
arrays. Here's a simple explanation of these operations:
Insertion:
1. To insert an element at a specific position in an array, we need
to shift the existing elements to make room for the new element.
2. If we want to insert an element at the beginning of the array,
we shift all the existing elements one position to the right.