Geschreven door studenten die geslaagd zijn Direct beschikbaar na je betaling Online lezen of als PDF Verkeerd document? Gratis ruilen 4,6 TrustPilot
logo-home
Document preview thumbnail
Voorbeeld 2 van de 5 pagina's
Overig

Array Representation: Concepts and Techniques

Document preview thumbnail
Voorbeeld 2 van de 5 pagina's

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.

Voorbeeld van de inhoud

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.

Documentinformatie

Geüpload op
28 januari 2025
Aantal pagina's
5
Geschreven in
2024/2025
Type
Overig
Persoon
Onbekend
$5.19

Verkeerd document? Gratis ruilen Binnen 14 dagen na aankoop en voor het downloaden kan je een ander document kiezen. Je kan het bedrag gewoon opnieuw besteden.
Geschreven door studenten die geslaagd zijn
Direct beschikbaar na je betaling
Online lezen of als PDF

Verkocht
0
Volgers
0
Items
252
Laatst verkocht
-




Waarom studenten kiezen voor Stuvia

Gemaakt door medestudenten, geverifieerd door reviews

Kwaliteit die je kunt vertrouwen: geschreven door studenten die slaagden en beoordeeld door anderen die dit document gebruikten.

Niet tevreden? Kies een ander document

Geen zorgen! Je kunt voor hetzelfde geld direct een ander document kiezen dat beter past bij wat je zoekt.

Betaal zoals je wilt, start meteen met leren

Geen abonnement, geen verplichtingen. Betaal zoals je gewend bent via Bancontact, iDeal of creditcard en download je PDF-document meteen.

Student with book image

“Gekocht, gedownload en geslaagd. Zo eenvoudig kan het zijn.”

Alisha Student

Bezig met je bronvermelding?

Maak nauwkeurige citaten in APA, MLA en Harvard met onze gratis bronnengenerator.

Bezig met je bronvermelding?

Veelgestelde vragen