A vector is a sequence of values, all of the same type. It's like an array in C, except that the size of a vector can be changed at run time.
vector(float) roots;
integer i := 0;
float pi := 3.1415926, e := 2.7182818, sum1 := 0;
resize(roots, 8); // new size is 8, with each element set to null
while (i < 8) {
roots[i] := e ^ ((pi * i) / 4);
i++;
}
It creates an empty vector,
resizes it with
resize,
and assigns values to elements in the
vector.
The first element of the vector has index 0,
the
second index 1,
and so forth.
You can also add new elements
to the end of a vector with the push_back
operator,
for example,
push_back(roots, e^pi).
i := 0;
while (i < size(roots)) {
sum1 := sum1 + roots[i];
i++;
}
The size operation returns the size of the
vector.
You can also loop through the elements of a vector
using a for loop:
for (root in roots) {
sum1 := sum1 + root;
}
The variable root is a new variable whose scope
is restricted to the loop body.
The first time through the
loop,
it is roots[0],
the second time
roots[1],
and so forth.
The loop stops when
roots[n] is null or there are no more elements
in roots.
roots := new vector(float);The old vector is automatically thrown away.