SHARE
TWEET

agregar vector

a guest Aug 17th, 2019 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int* agregar(int* pV, int &n, int v) {
  5.     int* pAux = new int[n+1];
  6.     for (int i = 0; i < n; i++)//Pasando todos los elementos del vector antiguo al nuevo
  7.         pAux[i] = pV[i];
  8.     pAux[n] = v;//Aumentando el nuevo elemento al vector
  9.     n++;
  10.     delete pV;
  11.     return pAux;
  12. }
  13.  
  14. void mostrar(int* pV, int n) {
  15.     system("cls");
  16.     for (short i = 0; i < n; i++)
  17.         cout << pV[i] << " ";
  18.     system("pause>0");
  19. }
  20.  
  21. int main() {
  22.     int* pVec;
  23.     int cant = 0, valor;
  24.     while (true) {
  25.         cout << "Ingrese un valor: "; cin >> valor;
  26.         pVec = agregar(pVec, cant, valor);
  27.         mostrar(pVec, cant);
  28.     }
  29.     return 0;
  30. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top