This commit is contained in:
Ingy döt Net 2013-04-10 16:57:12 -07:00
parent 518da4a923
commit 764da6cbbb
6144 changed files with 83610 additions and 11 deletions

View file

@ -0,0 +1,4 @@
// Qt
QVector<int> myArray4(10);
myArray4.push_back(1);
myArray4.push_back(2);

View file

@ -0,0 +1,4 @@
// MFC
CArray<int,int> myArray5(10);
myArray5.Add(1);
myArray5.Add(2);

View file

@ -0,0 +1,6 @@
int myArray[2];
myArray[0] = 1;
myArray[1] = 3;
cout << myArray[1] << endl;

View file

@ -0,0 +1,7 @@
int* myArray = new int[10];
myArray[0] = 1;
myArray[1] = 3;
cout << myArray[1] << endl;
delete [] myArray;

View file

@ -0,0 +1,8 @@
vector<int> myArray2;
myArray2.push_back(1);
myArray2.push_back(3);
myArray2[0] = 2;
cout << myArray2[0] << endl;

View file

@ -0,0 +1,25 @@
#include <iostream>
using std::cout;
using std::endl;
#include <boost/Array.hpp>
#include <algorithm>
#include <boost/foreach.hpp>
int main()
{
boost::array<int, 3> A;
A[0] = 5; // not bounds checked
A[1] = 2;
A.at(2) = 3; // this is bounds checked.
cout << A[0] << endl; // not bounds checked
for (int i =0; i < 3; ++i) {
cout << A.at(i) << endl; // this is bounds checked
}
// use it as you would any STL ordered container.
std::reverse(A.begin(),A.end());
BOOST_FOREACH(int i, A){cout << i << endl;}
}

View file

@ -0,0 +1 @@
int[] numbers = new int[10];

View file

@ -0,0 +1,2 @@
array :: {Real}
array = createArray 10 3.1415

View file

@ -0,0 +1,2 @@
array :: {Int}
array = {x \\ x <- [1 .. 10]}

View file

@ -0,0 +1,2 @@
array :: {!Int}
array = {x \\ x <- [1 .. 10]}

View file

@ -0,0 +1,2 @@
array :: {#Char}
array = {x \\ x <- ['a' .. 'z']}

View file

@ -0,0 +1 @@
string[] words = { "these", "are", "arrays" };

View file

@ -0,0 +1 @@
int[] more_numbers = new int[3]{ 21, 14 ,63 };

View file

@ -0,0 +1 @@
int[,] number_matrix = new int[3,2];

View file

@ -0,0 +1 @@
string[,] string_matrix = { {"I","swam"}, {"in","the"}, {"freezing","water"} };

View file

@ -0,0 +1 @@
string[,] funny_matrix = new string[2,2]{ {"clowns", "are"} , {"not", "funny"} };

View file

@ -0,0 +1,6 @@
int[] array = new int[10];
array[0] = 1;
array[1] = 3;
Console.WriteLine(array[0]);

View file

@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
List<int> list = new List<int>();
list.Add(1);
list.Add(3);
list[0] = 2;
Console.WriteLine(list[0]);

View file

@ -0,0 +1,2 @@
array :: {String}
array = {"Hello", "World"}

View file

@ -0,0 +1,4 @@
(let ((array (make-array 10)))
(setf (aref array 0) 1
(aref array 1) 3)
(print array))

View file

@ -0,0 +1,5 @@
(let ((array (make-array 0 :adjustable t :fill-pointer 0)))
(vector-push-extend 1 array)
(vector-push-extend 3 array)
(setf (aref array 0) 2)
(print array))

View file

@ -0,0 +1 @@
(make-array 10)

View file

@ -0,0 +1 @@
(make-array '(10 20))

View file

@ -0,0 +1,4 @@
; Makes an array of 20 objects initialized to nil
(make-array 20 :initial-element nil)
; Makes an integer array of 4 elements containing 1 2 3 and 4 initially which can be resized
(make-array 4 :element-type 'integer :initial-contents '(1 2 3 4) :adjustable t)

View file

@ -0,0 +1,27 @@
MODULE TestArray;
(* Implemented in BlackBox Component Builder *)
IMPORT Out;
(* Static array *)
PROCEDURE DoOneDim*;
CONST M = 5;
VAR a: ARRAY M OF INTEGER;
BEGIN
a[0] := 100; (* set first element's value of array a to 100 *)
a[M-1] := -100; (* set M-th element's value of array a to -100 *)
Out.Int(a[0], 0); Out.Ln;
Out.Int(a[M-1], 0); Out.Ln;
END DoOneDim;
PROCEDURE DoTwoDim*;
VAR b: ARRAY 5, 4 OF INTEGER;
BEGIN
b[1, 2] := 100; (* second row, third column element *)
b[4, 3] := -100; (* fifth row, fourth column element *)
Out.Int(b[1, 2], 0); Out.Ln;
Out.Int(b[4, 3], 0); Out.Ln;
END DoTwoDim;
END TestArray.

41
Task/Arrays/D/arrays.d Normal file
View file

@ -0,0 +1,41 @@
// All D arrays are capable of bounds checks.
import std.stdio, core.stdc.stdlib;
import std.container: Array;
void main() {
// GC-managed heap allocated dynamic array:
auto array1 = new int[1];
array1[0] = 1;
array1 ~= 3; // append a second item
// array1[10] = 4; // run-time error
writeln("A) Element 0: ", array1[0]);
writeln("A) Element 1: ", array1[1]);
// Stack-allocated fixed-size array:
int[5] array2;
array2[0] = 1;
array2[1] = 3;
// array2[2] = 4; // compile-time error
writeln("B) Element 0: ", array2[0]);
writeln("B) Element 1: ", array2[1]);
// Stack-allocated dynamic fixed-sized array,
// length known only at run-time:
int n = 2;
int[] array3 = (cast(int*)alloca(n * int.sizeof))[0 .. n];
array3[0] = 1;
array3[1] = 3;
// array3[10] = 4; // run-time error
writeln("C) Element 0: ", array3[0]);
writeln("C) Element 1: ", array3[1]);
// Phobos-defined heap allocated not GC-managed array:
Array!int array4;
array4.length = 2;
array4[0] = 1;
array4[1] = 3;
// array4[10] = 4; // run-time exception
writeln("D) Element 0: ", array4[0]);
writeln("D) Element 1: ", array4[1]);
}

View file

@ -0,0 +1,14 @@
// dynamic array, extensible, this a reference type
var d : array of Integer;
d.Add(1); // has various methods to add, delete, etc.
d.Add(2, 3);
// read and write elements by index
item := d[5];
d[6] := item+1;
// static, fixed-size array, arbitrary lower-bound, this is a value type
var s : array [2..4] of Integer;
// inline array constructor, works for both static and dynamic arrays
s := [1, 2, 3];

View file

@ -0,0 +1,10 @@
# use [] to create numeric arrays of int, float, double or complex types:
a = [ 1, 2, 3 ] # a vector
b = [ 1, 2; 3, 4 ] # a 2X2 matrix
# use {} to create normal arrays of any types:
c = { 1, 2, 'abc' }
d = a[1]
e = b[0,1] # first row, second column
f = c[1]

View file

@ -0,0 +1,30 @@
procedure TForm1.Button1Click(Sender: TObject);
var
StaticArray: array[0..9] of Integer;
DynamicArray: array of Integer;
StaticArrayText,
DynamicArrayText: string;
lcv: Integer;
begin
// Setting the length of the dynamic array the same as the static one
SetLength(DynamicArray, Length(StaticArray));
// Asking random numbers storing into the static array
for lcv := 0 to Pred(Length(StaticArray)) do
begin
StaticArray[lcv] := StrToInt(
InputBox('Random number',
'Enter a random number for position',
IntToStr(Succ(lcv))));
end;
// Storing entered numbers of the static array in reverse order into the dynamic
for lcv := 0 to Pred(Length(StaticArray)) do
DynamicArray[Pred(Length(DynamicArray)) - lcv] := StaticArray[lcv];
// Concatenating the static and dynamic array into a single string variable
for lcv := 0 to Pred(Length(StaticArray)) do
begin
StaticArrayText := StaticArrayText + IntToStr(StaticArray[lcv]);
DynamicArrayText := DynamicArrayText + IntToStr(DynamicArray[lcv]);
end;
// Displaying both arrays (#13#10 = Carriage Return/Line Feed)
ShowMessage(StaticArrayText + #13#10 + DynamicArrayText);
end;

11
Task/Arrays/E/arrays-1.e Normal file
View file

@ -0,0 +1,11 @@
? def empty := []
# value: []
? def numbers := [1,2,3,4,5]
# value: [1, 2, 3, 4, 5]
? numbers.with(6)
# value: [1, 2, 3, 4, 5, 6]
? numbers + [4,3,2,1]
# value: [1, 2, 3, 4, 5, 4, 3, 2, 1]

8
Task/Arrays/E/arrays-2.e Normal file
View file

@ -0,0 +1,8 @@
? var numbers := []
# value: []
? numbers := numbers.with(1)
# value: [1]
? numbers with= 2 # shorthand for same
# value: [1, 2]

12
Task/Arrays/E/arrays-3.e Normal file
View file

@ -0,0 +1,12 @@
? def flex := numbers.diverge()
# value: [1, 2].diverge()
? flex.push(-3)
? flex
# value: [1, 2, -3].diverge()
? numbers
# value: [1, 2]
? flex.snapshot()
# value: [1, 2, -3]

1
Task/Arrays/E/arrays-4.e Normal file
View file

@ -0,0 +1 @@
([0] * 100).diverge(int) # contains 100 zeroes, can only contain integers

View file

@ -0,0 +1,3 @@
array int[10]; //optionally, add a braced list of values. E.g. array int[10]{1, 2, 3};
array[1] = 42;
SysLib.writeStdout(array[1]);

View file

@ -0,0 +1,3 @@
#var anArray := (1, 2, 3).
'program'Output << anArray@1.

View file

@ -0,0 +1,4 @@
#var anArray := basic'NewArray::3.
anArray@0 content'set:2.
'program'Output << anArray@0.

View file

@ -0,0 +1,29 @@
--Arrays task for Rosetta Code wiki
--User:Lnettnay
atom dummy
--Arrays are sequences
-- single dimensioned array of 10 elements
sequence xarray = repeat(0,10)
xarray[5] = 5
dummy = xarray[5]
? dummy
--2 dimensional array
--5 sequences of 10 elements each
sequence xyarray = repeat(repeat(0,10),5)
xyarray[3][6] = 12
dummy = xyarray[3][6]
? dummy
--dynamic array use (all sequences can be modified at any time)
sequence dynarray = {}
for x = 1 to 10 do
dynarray = append(dynarray, x * x)
end for
? dynarray
for x = 1 to 10 do
dynarray = prepend(dynarray, x)
end for
? dynarray