What is the function of the vector (array)?

Asked

Viewed 1,609 times

14

I’m studying algorithms with Portugol and I’m having difficulty understanding the function of the vector in the code. I already know how to use this structure.

  • 2

    I’m not sure if you have a doubt about a function called "vector", or about using vectors themselves. However, this may help you: http://www.dicasdeprogramacao.com.br/o-que-sao-vetores-e-matrizes-arrays/

  • 1

    I’m having doubts in the explanatory part, what it’s for, how it can help in the code. Got it ?

  • 2

    Store a data collection in one place only. Like, you can pull the registration data of 50 clients in just one vector and then go through it to display what you want without having to do a new query to the database for example. That is the question?

  • 1

    Oh yes, I get it. Thanks for the help.

3 answers

23


A vector is usually a variable that can contain various values (can be only one 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 all are integers, or all are of the character type (if they are different they need to be pro reference, otherwise there is no way to be a vector).

Technically each of the elements of the vector ends up being a variable as well. So we can say that a vector is a variable collection. These variables are called indexes.

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

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

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

The exact working may vary from language to language. Some are more flexible. Some may experience a loss of performance. In Portugol this is not important.

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

Think you got 50 student grades, you could do this:

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

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

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

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

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

This way 50 lines turned into 3. It does the same automated thing. Use math and flow control to get the same result in a simple, streamlined code.

  • 1

    I can use vector to store students' grades in a program that calculates students' medias and then displays them ?

  • 1

    Yes, it can, it’s one of his duties.

  • 1

    Thanks for the help. Is that this part of vectors I found a little boring to understand.

  • 5

    It’s pure mathematics. There’s nothing new in computing.

1

Simply put, a vector is much like an array.

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

int meVetor[10];

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

On the other hand, an array is equal to the vector, but without defined size, you can create an array, and add new positions, manipulate them, delete etc.

From the usage point of view, the vector is for when you are sure you will only use 'X' positions, and the array is for when you are unsure of the amount of positions you will need.

0

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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.