Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Document preview thumbnail
Preview 2 out of 5 pages
Other

Array Representation: Concepts and Techniques

Document preview thumbnail
Preview 2 out of 5 pages

This document explores array representation, including how arrays are indexed and stored in memory. Learn about linear representation, multi-dimensional arrays, and how these structures are used in different applications.

Content preview

Array Representation
1. Memory Representation of Arrays
Arrays are stored in contiguous blocks of memory, which means that all elements
of an array are placed next to each other in memory.

 Contiguous Memory Allocation:
o Arrays allocate a block of memory large enough to hold all of its
elements.
o The memory addresses of array elements are contiguous (i.e., they
are next to each other in memory).
o In many programming languages (like C, C++, and Python), arrays can
be accessed via an index, where each element's position is
determined by its index relative to the base address of the array.

Example (in C):

int arr[5] = {10, 20, 30, 40, 50};

In this case, the memory layout would look like this:

| 10 | 20 | 30 | 40 | 50 |

Each number is stored in a consecutive memory location, and the array's base
address (the address of arr[0]) points to the first element.



2. Accessing Array Elements
Because arrays are stored contiguously, accessing an element is done by
calculating its position in memory based on the index. The address of an element
at index i can be calculated as:

Address of arr[i] = base address of arr + i * size_of_element

, This formula allows for constant time O(1) access to any element in the array,
making arrays very efficient for random access.

For example, if an integer array has elements stored in a contiguous block of
memory and each integer takes 4 bytes, then the element at index i can be
accessed as:

Address of arr[i] = Address of arr[0] + i * 4


3. Array vs Linked List
 Array: Fixed-size block of memory, elements are contiguous, and access is
O(1).
 Linked List: Each node contains a reference (pointer) to the next node, and
the nodes are scattered throughout memory, which means accessing an
element is slower (O(n)) because you have to traverse the list.



4. Row-Major and Column-Major Order
For multi-dimensional arrays (like 2D arrays), there are two common ways to
store them in memory:

 Row-Major Order (used by C, C++):
o In row-major order, the rows of the array are stored contiguously in
memory.
o For example, for a 2x3 matrix:


|1 2 3|
|4 5 6|

The elements will be stored in memory as: 1, 2, 3, 4, 5, 6.

 Column-Major Order (used by languages like Fortran and MATLAB):
o In column-major order, the columns of the array are stored
contiguously in memory.

Document information

Uploaded on
January 28, 2025
Number of pages
5
Written in
2024/2025
Type
Other
Person
Unknown
$5.19

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Sold
0
Followers
0
Items
252
Last sold
-




Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions