Reading a sequence between 2 and a given value of x

Asked

Viewed 51 times

1

Good afternoon, I would like to know, how can I create a vector that stores a sequence, that is, if I want the numbers between 2 and x and the user between x = 10, the values within the vector are as vector[2 , 10] = 2,3,4,5,6,7,8,9,10 ??

1 answer

3


Whereas x is the number provided by the user:

j = 0;  //variavel que será responsável pelo indice do vetor

for (int i=2; i<=x; i++) {
    vetor[j] = i;
    j++;
}

Thus, as the loop is executed, the vector will be organized as follows for x = 4, for example:

vector[0] = 2; vector[1] = 3; vector[2] = 4;

Browser other questions tagged

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