Académique Documents
Professionnel Documents
Culture Documents
What Is An Array-
The entire array is stored contiguously in memory (that is, there are no
gaps between elements).
Arrays can have more than one dimension. A one-dimensional array is called
a vector ; a two-dimensional array is called a matrix.
Create Arrays-
Ahmed Al-Adeli
This is called a single-dimensional array. The arraySize must be an integer
constant greater than zero and type can be any valid C data type. For example,
now to declare a 10-element array called number of type int, use this
statement:
int number[10];
Here, number is a variable array, which is sufficient to hold up to 10 integer
numbers.
To store a data item in an array, the element that the data will be stored in needs
to be referenced. Using an array literal is the easiest way to create a JavaScript
Array.
Initializing Arrays-
You can initialize an array in C either one by one or using a single statement as
follows
int number[5] = {10, 20, 30, 40, 50};
The number of values between braces { } cannot be larger than the number of
elements that we declare for the array between square brackets [ ].
If you omit the size of the array, an array just big enough to hold the initialization
is created. Therefore, if you write
int number[] = {10, 20, 30, 40, 50};
You will create exactly the same array as you did in the previous example.
Following is an example to assign a single element of the array
number[4] = 50;
The above statement assigns element number 5th in the array with a value of
50. All arrays have 0 as the index of their first element which is also called the
base index and the last index of an array will be the total size of the array minus
1.
Ahmed Al-Adeli
A variable holds a single item of data. There may be a situation where lots of
variables are needed to hold similar and related data. In this situation, using an
array can simplify a program by storing all related data under one name. This
means that a program can be written to search through an array of data much
more quickly than having to write a new line of code for every variable. This
reduces the complexity and length of the program which makes it easier to find
and debug errors.
While we don't think about these things much in our fancy managed languages
today, they are built on the same foundation, so let's look at how memory is
managed in C.
Before I dive in, a quick explanation of what the term "pointer" means. A pointer
is simply a variable that "points" to a location in memory. It doesn't contain the
actual value at this area of memory, it contains the memory address to it. Think
of a block of memory as a mailbox. The pointer would be the address to that
mailbox.
In C, an array is simply a pointer with an offset, the offset specifies how far in
memory to look. This provides O(1) access time.
MyArray [5]
^
^
Pointer Offset
All other data structures either build upon this, or do not use adjacent memory
for storage, resulting in poor random access look up time (Though there are
other benefits to not using sequential memory).
For example, let's say we have an array with 6 numbers (6,4,2,3,1,5) in it, in
memory it would look like this:
=====================================
| 6 | 4 | 2 | 3 | 1 | 5 |
=====================================
In an array, we know that each element is next to each other in memory. A C
array (Called MyArray here) is simply a pointer to the first element:
=====================================
| 6 | 4 | 2 | 3 | 1 | 5 |
=====================================
^
MyArray
If we wanted to look up MyArray[4], internally it would be accessed like this:
0
1
2
3
4
=====================================
| 6 | 4 | 2 | 3 | 1 | 5 |
=====================================
Ahmed Al-Adeli
^
MyArray + 4 ---------------/
(Pointer + Offset)
Because we can directly access any element in the array by adding the offset to
the pointer, we can look up any element in the same amount of time, regardless
of the size of the array. This means that getting MyArray[1000] would take the
same amount of time as getting MyArray[5].
An alternative data structure is a linked list. This is a linear list of pointers, each
pointing to the next node
======== ======== ======== ========
| Data | | Data | | Data | | Data | | Data |
|
| -> |
| -> |
| -> |
| -> |
|
| P1 | | P2 | | P3 | | P4 | | P5 |
======== ======== ======== ========
========
========
Ahmed Al-Adeli
Remember up above where I said that sometimes using a non-sequential data
structure can have advantages? Searching for data is one of these advantages
and one of the best examples is the Binary Tree.
A Binary Tree is a data structure similar to a linked list, however instead of linking
to a single node, each node can link to two children nodes.
==========
| Root |
==========
/
\
=========
=========
| Child |
| Child |
=========
=========
/
\
========= =========
| Child | | Child |
========= =========
Assume that each connector is really a Pointer
When data is inserted into a binary tree, it uses several rules to decide where to
place the new node. The basic concept is that if the new value is greater than
the parents, it inserts it to the left, if it is lower, it inserts it to the right.
This means that the values in a binary tree could look like this:
==========
| 100 |
==========
/
\
=========
=========
| 200 |
| 50 |
=========
=========
/
\
========= =========
| 75 | | 25 |
========= =========
When searching a binary tree for the value of 75, we only need to visit 3 nodes
( O(log N) ) because of this structure:
Ahmed Al-Adeli
That is where arrays get beat, they provide a constant O(N) search time, despite
O(1) access time.
Referenceshttp://www.bbc.co.uk/education/guides/zy9thyc/revision/2
http://www.tutorialspoint.com/computer_programming/computer_programming_a
rrays.htm