Advertisement
allocator

Untitled

Oct 27th, 2014
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5.     cout << "Vvedite n: ";
  6.     int n;
  7.     cin >> n;
  8.     int array[n+1];
  9.     cout << endl;
  10.     for (int i = 0; i < n; i++) {
  11.         cout << "Vvedite element massiva: ";
  12.         cin >> array[i];
  13.     }
  14.     cout << "\nVvedite chislo: ";
  15.     int newNumber;
  16.     cin >> newNumber;
  17.     cout << "\nIshodniy massiv:\n";
  18.     for (int i = 0; i < n; i++)
  19.         cout << array[i] << " ";
  20.    
  21.     // Find right position
  22.  
  23.     int *newPos = &array[0];
  24.  
  25.     while (newPos != &array[n] && *newPos < newNumber)
  26.         ++newPos;
  27.  
  28.     // Shift emenents
  29.  
  30.     for (int *i = &array[n]; i != newPos; --i)
  31.         *i = *(i - 1);
  32.  
  33.     // Insert new value
  34.  
  35.     *newPos = newNumber;
  36.  
  37.     cout << "\nIzmenenniy massiv:\n";
  38.     for (int i = 0; i < n+1; i++)
  39.         cout << array[i] << " ";
  40.     cout << endl;
  41.     system("pause");
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement