What is the function of the array?

I'm studying algorithms with Portugol and I'm having a hard time understanding the vector function in the code. I know how to use that structure.

Author: Maniero, 2016-04-22

3 answers

A vector is usually a variable that can contain multiple values (it can only be an object not stored in variable).

You use it when you need to store a sequence of values in memory. In general these different values represent the same thing and it is very common to be of the same type. Some languages even require them to be of the same type, so that they are all integers, or all are of the character type (if they are different they need to be pro reference, otherwise it cannot be a vector).

Technically each of the elements of the vector turns out to be a variable as well. So we can say that a vector is a collection of variables . These variables are called indices.

Each of the elements or items, as they are called, are accessed through the index, usually numeric indicating their position in the sequence. It can be accessed by a literal (a number) or by another variable that indicates the number of the position, or an expression that calculates this number.

When we declare a vector we are reserving a space in memory for the amount of elements that it will behave. So in a vector of 10 elements of an integer type it usually allocates, roughly speaking, 40 bytes (10 x 4 bytes of integer size).

A vector is a specialized form of Matrix that we all learn in mathematics. We can say that a vector is a single row or a single column of a array.

The exact operation may vary from language to language. Some are more flexible. Some may have performance loss. In Portugol this is not important.

Depending on the language there may be a more specific definition. Some differentiate the array from the vector, from the matrix, from the list, etc. Others use these terms interchangeably or use more specific terms for some variation of the vector. O Portugol usa the simplest definition, for those who are learning it is not interesting to understand all the nuances.

Think You have 50 student grades, you could do this:

leia(nota1)
leia(nota2)
leia(nota3)
.
.
.
leia(nota50)

Complicated to do this, right? It could simplify, automate repetition. How to access each of them? How to generalize all this? Using vector:

leia(nota[1])
leia(nota[2])
leia(nota[3])
.
.
.
leia(nota[50])

Now already has a vector, a simple change, but the code is still bad:

para i de 1 ate 50 faca
    leia(nota[i])
fimpara

In this way 50 lines turned into 3. He does the same automated thing. It uses math and flow control to get the same result in simple, lean code.

 23
Author: Maniero, 2020-10-21 12:48:14

Simply put, a vector is very similar to an array.

The difference is that the vector usually has the pre-defined size at the time of the declaration, for example:

INT myvetor[10];

In this case, myvetor will only have 10 positions, you can manipulate what already exists, but you can not create new positions. If you try to access the 11th position will give error.

In counter match, an array is equal to the vector, but without a set size, you can create an array, and go adding new positions, manipulating them, deleting etc.

From a usage point of view, the vector is for when you're sure you'll only use 'X' positions, and the array is for when you're not sure how many positions you'll need.

 1
Author: Gabriel Loch, 2020-01-02 05:14:39

A Vector has the function of saving certain specific information according to its type so that it can or is in index form, which allows access to all or certain data using filters in these indexes.

 0
Author: X Gamer, 2020-01-02 04:59:19