ToniDev

Inserare 1 element in vector la final

Dec 9th, 2021
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #define MAX_SIZE 70
  5.  
  6. int main()
  7. {
  8.     int v[MAX_SIZE];
  9.     int n;
  10.  
  11.     cout << "Introdu n (mai mic decat " << MAX_SIZE << ": ";
  12.     cin >> n;
  13.  
  14.     int a;
  15.  
  16.     //Inserare
  17.     for (int i = 0; i < n; i++)
  18.     {
  19.         cout << "Introdu valoare: ";
  20.         cin >> a;
  21.  
  22.         v[i] = a;
  23.     }
  24.  
  25.  
  26.     //Afisare
  27.     for (int i = 0; i < n; i++)
  28.     {
  29.         cout << v[i] << " ";
  30.     }
  31.  
  32.     cout << endl;
  33.     cout << "Mai introdu un numar: ";
  34.     cin >> a;
  35.  
  36.     v[n] = a; // Inseram pe ultima pozitie
  37.     n++; //Incrementam numarul de elemente imediat dupa
  38.      
  39.     //Afisare
  40.     for (int i = 0; i < n; i++)
  41.     {
  42.         cout << v[i] << " ";
  43.     }
  44.  
  45.     return 0;
  46. }
  47.  
  48.  
Advertisement
Add Comment
Please, Sign In to add comment