Problem filling vector

2

I made a method to fill a vector and another one to show it, when I execute the code it shows me the vector arriving only with values 0

This is the main

int main(int argc, char** argv){

int opc, tam = 5;
vector<int> vec(tam);
llenarVector(vec, tam);
cout << "Valores del vector\n" << endl;
imprimirVector(vec, tam);
system("pause");
return 0;
}

This is the method to fill the vector

void llenarVector(vector<int> vec, int tam)
{
  int i;
  srand(time(NULL));
  for(i = 0; i< tam; i++)
  { 
     vec[i] = i+1;
  }
}

And this is to print the values

 void imprimirVector(vector<int> vec, int tam)
 {
   int i;
   for(int i=0; i< tam; i++)
    cout << vec[i] << " ";
 }

I'm doing something wrong, I still do not use the C ++ language and the use of vectors, thanks

    
asked by Camilo 16.06.2017 в 05:38
source

2 answers

2

The problem with your code is that you are passing the parameters of your functions by value and not by reference.
The difference between these two types of parameters is that in the case of the parameter by value you create a copy of that variable within the context of the function and the modifications are made to the copy and in the case of the parameter by reference what you do is pass the address of that variable as a parameter, so the modifications you make are made to the original variable.
Then, as you pass parameters by value, the change you make to the copy does not leave the function llenarVector , it stays there stagnant.
I leave the corrected code:

void llenarVector(vector<int> &vec, int tam)
{
  int i;
  srand(time(NULL));
  for(i = 0; i< tam; i++)
  {
     vec[i] = i+1;
  }
}

void imprimirVector(vector<int> &vec, int tam)
 {
   int i;
   for(int i=0; i< tam; i++)
    cout << vec[i] << " ";
 }

int main(int argc, char** argv){

int opc, tam = 5;
vector<int> vec(tam);
llenarVector(vec, tam);
cout << "Valores del vector\n" << endl;
imprimirVector(vec, tam);
system("pause");
return 0;
}
    
answered by 16.06.2017 / 06:37
source
1

Apart from what was commented by @EmmanuelLG ...

The class vector has a size() method to know the number of stored items, then the tam spare:

void llenarVector(vector<int> &vec)
{
  int i;
  srand(time(NULL));
  for(i = 0; i< vec.size(); i++)
  {
    vec[i] = i+1;
  }
}

void imprimirVector(vector<int> &vec)
{
  int i;
  for(int i=0; i< vec.size(); i++)
    cout << vec[i] << " ";
}

int main(int argc, char** argv){
  int opc;
  vector<int> vec(5);
  llenarVector(vec);
  cout << "Valores del vector\n" << endl;
  imprimirVector(vec);
  system("pause");
  return 0;
}

In addition, loops for admit to declare variables within the same ... is good practice to use this feature as it helps reduce the life of the variables and thus, errors due to carelessness:

void llenarVector(vector<int> &vec)
{
  srand(time(NULL));
  for(int i = 0; i< vec.size(); i++)
  {
    vec[i] = i+1;
  }
}

void imprimirVector(vector<int> &vec)
{
  int i; // Nota que esta declaracion te sobra.
         // esta variable es independiente de la declarada dentro del bucle
  for(int i=0; i< vec.size(); i++)
    cout << vec[i] << " ";
}

And finally, when a function receives a reference and no need to modify the object is preferable to mark such reference constant. This prevents modifications that go unnoticed, among other advantages:

void imprimirVector(vector<int> const& vec)
{
  for(int i=0; i< vec.size(); i++)
    cout << vec[i] << " ";
}
    
answered by 16.06.2017 в 09:06