Vous êtes sur la page 1sur 6

Ahmed Al-Adeli

What Is An Array-

A variable is a memory location that can store a value. It can be thought of as a


box in which values are stored. The value held in the box can change, or vary.
But each variable can only hold one item of data. An array is a series of memory
locations or boxes each of which holds a single item of data, but with each box
sharing the same name. All data in an array must be of the same data type.It
was simple, because we had to store just five integer numbers. Now let's assume
we have to store 5000 integer numbers. Are we going to use 5000 variables? To
handle such situations, almost all the programming languages provide a concept
called array. An array is a data structure, which can store a fixed-size collection
of elements of the same data type. An array is used to store a collection of data,
but it is often more useful to think of an array as a collection of variables of the
same type.Instead of declaring individual variables, such as number1,
number2, ..., number99, you just declare one array variable number of integer
type and use number1[0], number1[1], and ..., number1[99] to represent
individual variables. Here, 0, 1, 2, .....99 are index associated with var variable
and they are being used to represent individual elements available in the
array. All arrays consist of contiguous memory locations. The lowest address
corresponds to the first element and the highest address to the last
element. In programming, a series of objects all of which are the same size and
type. Each object in an array is called an array element. For example, you could
have an array of integers or an array of characters or an array of anything that
has a defined data type. The important characteristics of an array are:
Each element has the same data type (although they may have different
values).

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-

To create an array variable in C, a programmer specifies the type of the elements


and the number of elements to be stored in that array. Given below is a simple
syntax to create an array in C programming:
type arrayName [ arraySize ];

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.

Why use arrays?-

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 |
======== ======== ======== ========

========

========

P(X) stands for Pointer to next node.


Note that I made each "node" into its own block. This is because they are not
guaranteed to be (and most likely won't be) adjacent in memory.
If I want to access P3, I can't directly access it, because I don't know where it is
in memory. All I know is where the root (P1) is, so instead I have to start at P1,
and follow each pointer to the desired node.
This is a O(N) look up time (The look up cost increases as each element is
added). It is much more expensive to get to P1000 compared to getting to P4.
Higher level data structures, such as hash tables, stacks and queues, all may use
an array (or multiple arrays) internally, while Linked Lists and Binary Trees
usually use nodes and pointers.
You might wonder why anyone would use a data structure that requires linear
traversal to look up a value instead of just using an array, but they have their
uses.
Take our array again. This time, I want to find the array element that holds the
value '5'.
=====================================
| 6 | 4 | 2 | 3 | 1 | 5 |
=====================================
^
^
^
^
^ FOUND!
In this situation, I don't know what offset to add to the pointer to find it, so I have
to start at 0, and work my way up until I find it. This means I have to perform 6
checks.
Because of this, searching for a value in an array is considered O(N). The cost of
searching increases as the array gets larger.

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:

Is 75 less than 100? Look at Right Node

Is 75 greater than 50? Look at Left Node

There is the 75!


Even though there are 5 nodes in our tree, we did not need to look at the
remaining two, because we knew that they (and their children) could not possibly
contain the value we were looking for. This gives us a search time that at worst
case means we have to visit every node, but in the best case we only have to
visit a small portion of the nodes.

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

Vous aimerez peut-être aussi