Retrieving an Element of an Array: Difference between revisions
mNo edit summary |
(Undo revision 6159 by Special:Contributions/Bp4Avl (User talk:Bp4Avl)) |
||
Line 1: | Line 1: | ||
{{task}} |
|||
[http://people.msoe.edu/~millerni/forums.php?show=topic&id=114&forum=13 meridia] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=157&forum=13 free wwe ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=128&forum=13 cheap phentermine] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=158&forum=13 cheap xanax] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=106&forum=13 hydrocodone online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=160&forum=13 cheap zanaflex] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=102&forum=13 free ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=97&forum=13 buy didrex] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=135&forum=13 real ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=100&forum=13 fioricet online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=96&forum=13 buy diazepam] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=136&forum=13 rivotril online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=87&forum=13 ativan online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=144&forum=13 sonyericsson ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=137&forum=13 free sagem ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=152&forum=13 free verizon ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=139&forum=13 sharp ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=125&forum=13 cheap ortho] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=156&forum=13 wellbutrin online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=151&forum=13 valium] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=121&forum=13 nexium online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=147&forum=13 free tracfone ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=142&forum=13 free sony ericsson ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=94&forum=13 free cool ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=161&forum=13 but zoloft] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=117&forum=13 motorola ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=133&forum=13 qwest ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=138&forum=13 samsung ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=122&forum=13 free nextel ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=89&forum=13 celexa online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=99&forum=13 free ericsson ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=115&forum=13 midi ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=146&forum=13 tenuate online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=109&forum=13 levitra online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=82&forum=13 cheap adipex] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=132&forum=13 free punk ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=130&forum=13 propecia online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=83&forum=13 cheap albuterol] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=85&forum=13 order alprazolam] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=153&forum=13 viagra online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=148&forum=13 order tramadol] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=143&forum=13 sony ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=86&forum=13 cheap ambien] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=103&forum=13 free funny ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=104&forum=13 hgh online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=129&forum=13 free polyphonic ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=90&forum=13 cialis online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=123&forum=13 nokia ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=101&forum=13 but flexeril] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=124&forum=13 online norco] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=149&forum=13 cheap ultracet] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=88&forum=13 cheap carisoprodol] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=108&forum=13 kyocera ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=93&forum=13 clonazepam online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=127&forum=13 cheap pharmacy online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=95&forum=13 cyclobenzaprine online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=150&forum=13 ultram online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=111&forum=13 lisinopril online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=159&forum=13 cheap xenical] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=162&forum=13 zyban online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=116&forum=13 mono ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=112&forum=13 lorazepam online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=145&forum=13 sprint ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=110&forum=13 cheap lipitor] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=131&forum=13 cheap prozac] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=120&forum=13 music ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=105&forum=13 hoodia online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=155&forum=13 online vigrx] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=98&forum=13 cheap diethylpropion] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=113&forum=13 cheap lortab] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=154&forum=13 vicodin online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=119&forum=13 mtv ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=84&forum=13 alltel ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=141&forum=13 soma online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=107&forum=13 free jazz ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=140&forum=13 cheap sildenafil] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=126&forum=13 paxil online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=118&forum=13 free mp3 ringtones] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=92&forum=13 clomid online] [http://people.msoe.edu/~millerni/forums.php?show=topic&id=91&forum=13 cingular ringtones] {{task}} |
|||
In this task, the goal is to retrieve an element of an [[array]]. |
In this task, the goal is to retrieve an element of an [[array]]. |
||
Line 15: | Line 15: | ||
mov esi, array_offset |
mov esi, array_offset |
||
mov ebx, 2 |
mov ebx, 2 |
||
mov eax, [esi |
mov eax, [esi+ebx*4] |
||
==[[ActionScript]]== |
==[[ActionScript]]== |
||
Line 54: | Line 54: | ||
} |
} |
||
==[[C plus plus|C |
==[[C plus plus|C++]]== |
||
[[Category:C plus plus|C |
[[Category:C plus plus|C++]] |
||
template<typename T> |
template<typename T> |
||
Line 139: | Line 139: | ||
Forth does not have special syntax for array access. Address arithmetic is used to access contiguous memory. |
Forth does not have special syntax for array access. Address arithmetic is used to access contiguous memory. |
||
create array 1 , 2 , 3 , 4 , |
create array 1 , 2 , 3 , 4 , |
||
array 2 cells |
array 2 cells + @ . \ 3 |
||
==[[IDL]]== |
==[[IDL]]== |
Revision as of 06:23, 1 July 2007
You are encouraged to solve this task according to the task description, using any language you may know.
In this task, the goal is to retrieve an element of an array.
4D
` first element $elem:=$array{1}
X86 assembly
Assembler: nasm
mov esi, array_offset mov ebx, 2 mov eax, [esi+ebx*4]
ActionScript
var arr:Array = new Array(1,2,3); var myVar:Number = arr[1]; // the value of myVar is: 2
Ada
Array indexed by an enumerated type. Ada enumerated types are discrete non-numeric types.
type Days is (Mon, Tue, Wed, Thu, Fri, Sat, Sun); type Daily_Counts is array(Days) of Natural; This_week : Daily_Counts := (200, 212, 175 220, 201, 120, 0); Monday_Sales : Natural;
Monday_Sales := This_Week(Mon);
Monday_Sales is assigned 200
AppleScript
on getArrayValue(array, location) -- very important -- The list index starts at 1 not 0 return item location in array end getArrayValue
C
int array_index(int array[], int index) { return array[index]; }
C#
int getArrayValue( int values[], int index ) { return values[index]; }
C++
template<typename T> T array_index(T array[], size_t index) { return array[index]; }
ColdFusion
<cfset arr = ArrayNew(1)> <cfset arr[1] = "one"> <cfset arr[2] = "2"> <cfset arr[3] = 3> <cfset var = arr[1]>
The value of var is "one"
ColdFusion Arrays are NOT zero-based, their index begins at 1
Common Lisp
(defun array-value (array index) (aref array index))
Delphi/Object Pascal/Turbo Pascal/Standard Pascal
Array's in all the flavors of pascal can be of any valid base type, or user defined type ( which are all made up of base types ) and are multi-dimensional. With Delphi dynamic arrays were defined but had been used in pascal since its inseption.
A Static array defintion:
foo : array[1..10] of integer; { The base index is ONE }
The base index can be freely chosen:
foo: array[7 .. 16] of integer; { The base index is 7 }
Indeed, the "1 .. 10" resp. "7 .. 16" are actually types: they are integer subrange types. Arrays can also be indexed by enumeration types or enumeration subrange types:
type rainbowcolor = (red, orange, yellow, green, blue, violet); var foo: array[rainbowcolor] of integer; bar: array[yellow .. blue] of integer; i: integer begin i := foo[red]; { allowed indices are red, orange, yellow, green, blue, violet } i := bar[green]; { allowed indices are yellow, green, blue } end;
A Dynamic Array type in Delphi:
foo : array of integer ; // The base index is ZERO
An "old school" dynamic array in the various flavors of pascal
foo : array[0..0] of integer; // The base index is ZERO
A dynamic array in Extended Pascal:
type intarray(n: integer) = array[1 .. n] of integer; { base index 1 } var foo: ^intarray; begin new(foo, 10); { foo now has index 1 to 10 } i := foo[2]; dispose(foo); { get rid of the array } end;
In the case of the static array, the compiler generates the code to allocate the required memory to hold 10 integers.
In the Delphi style ---dynamic--- array you must set its length:
SetLength(foo,10); // this array will no hold 10 integers
In the "old school" style of dynamic arrays, you created a point to the zero length declaration and then allocated memory to it with GetMem
pFoo : ^Foo ; Foo : array[0..0] of integer ;
All arrays are accessed the same way regardless of declaration method.
i : integer ; i := foo[n] ;
where n is the array index who's base is either 1 or 0 depending on how it was declared.
Erlang
Erlang lists are 1-based which means that the index must be within the bounds (1 .. length(List)):
Value = lists:nth(Index, List).
Forth
Forth does not have special syntax for array access. Address arithmetic is used to access contiguous memory.
create array 1 , 2 , 3 , 4 , array 2 cells + @ . \ 3
IDL
; this is allowed: result = arr(5) ; but this is preferred: result = arr[5]
The form with square brackets is preferred as it unambiguously constitutes array access, while the version with round ones can conflict with a function call if there are both a function and an array with the same name arr.
Java
public Object getArrayElem(Object[] array, int pos) { return array[pos]; }
JavaScript
var element = array[index];
mIRC
Interpeter: mIRC Script Editor
Library: mArray Snippet
alias readmyarray { echo -a $array_read(MyArray, 2, 3) }
Perl
Interpreter: Perl 5.8.8
$elem = $array[0];
PHP
$array = array('php', 'has', 'arrays'); // First element $elem = $array[0];
Pop11
lvars ar = {1 two 'three'}; lvars elem; ;;; Access second element and assign to variable elem ar(2) -> elem;
This example uses the simplest possible array (a vector). Pop11 has more general arrays, but in all cases access follows the same pattern, and look the same as procedure (function) call.
Python
Interpreter: Python 2.5
The item is an element in a list at a given index
item = aList[index]
or
To use a list like a stack be it FIFO/LIFO
aList.pop() # Pop last item in a list aList.pop(0) # Pop first item in a list
Note: When using the pop() method, the element is removed from the list.
Ruby
ary = ['Ruby','rules','big','time'] #the first element element = ary[0] #or element = ary.first # => element = 'Ruby'
#the last element element = ary[-1] #or element = ary.last # => element = 'time'
#retrieving different values at once elements = ary.values_at(0,2,3) # => elements = ['Ruby','big','time']
#select the first element of length 3 element = ary.find{|el|el.length==3} # => element = "big"
Smalltalk
"Retrieve second element of an array" index := 2 element := anArray at: index
Tcl
All arrays in Tcl are associative. If "key" is the variable that holds the key of the element to be retrieved, then
set result $array($key)
Toka
This retrieves the value 20 from the second item in the array:
3 cells is-array table
( Populate the array ) 10 0 table put-element 20 1 table put-element 30 2 table put-element table 1 get-element