4
Guys, I made a simple code, but I have a problem. I made a function "increase" to increase the size of a vector; the function receives the vector and passes the values of that vector to an auxiliary vector. So I can delete the vector, allocate the new size and recover the values of it pq were saved in the aux vector. The problem is that it ta printing trash. If I print the vector inside the function comes out all right but the main goes wrong. Can anyone help? Follow the code:
#include<iostream>
using namespace std;
void aumentar(int *vetor, int tam){
int *aux, i;
aux= new int[tam];
for(i=0; i<tam; i++){
aux[i]=vetor[i];
}
cout<<"aux:"<<endl;
for(i=0; i<tam; i++){
cout<<aux[i]<<" ";
}
cout<<endl;
delete []vetor;
vetor = new int[11];
for(i=0; i<tam; i++){
vetor[i]=aux[i];
}
vetor[tam]=10;
for(i=0; i<11; i++){
cout<<vetor[i]<<" ";
}
cout<<endl;
}
int main(){
int *vetor, i;
vetor = new int[10];
for(i=0; i<10; i++){
vetor[i]=i;
}
for(i=0; i<10; i++){
cout<<vetor[i]<<" ";
}
cout<<endl;
aumentar(vetor,10);
cout<<"Resultado"<<endl;
for(i=0; i<11; i++){
cout<<vetor[i]<<" ";
}
cout<<endl;
}
that’s right! Thank you!
– Gabriel Castro