next up previous contents index
Next: Matrices and vectors Up: Module TArray Previous: Module TArray   Contents   Index


Using arrays

The example below shows basic usage of arrays, creation, initialisation and arithmetic operations. Different kind of Sequence objects can be used for initialising arrays.

\begin{figure}
\framebox [40mm][c]{\mbox{\rule[-1mm]{0mm}{5mm} \bf Sequence} }$...
...m][c]{\mbox{\rule[-1mm]{0mm}{5mm} \bf EnumeratedSequence} }\\ [2mm]
\end{figure}

The example below shows basic usage of arrays:


// Creating and initialising a 1-D array of integers
TArray<int> ia(5);
EnumeratedSequence es;
es = 24, 35, 46, 57, 68;
ia = es;
cout << "Array<int> ia = " << ia;
// 2-D array of floats 
TArray<r_4> b(6,4), c(6,4);
// Initializing b with a constant
b = 2.71828;
// Filling c with random numbers  
c = RandomSequence(); 
// Arithmetic operations
TArray<r_4> d = b+0.3f*c;
cout << "Array<float> d = " << d;

The copy constructor shares the array data, while the assignment operator copies the array elements, as illustrated in the following example:


TArray<int> a1(4,3);
a1 = RegularSequence(0,2);
// Array a2 and a1 shares their data 
TArray<int> a2(a1);
// a3 and a1 have the same size and identical elements
TArray<int> a3;
a3 = a1;
// Changing one of the a2 elements
a2(1,1,0) = 555;
// a1(1,1) is also changed to 555, but not a3(1,1)
cout << "Array<int> a1 = " << a1;
cout << "Array<int> a3 = " << a3;



Reza Ansari
2001-03-07