Advertisement
JmihPodvalbniy

Untitled

Dec 19th, 2024
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | Software | 0 0
  1. // Дз от 09.12.2024г.
  2.  
  3. // 1)
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. int main() {
  8.     int n;
  9.     cout << "Введите размер массива: ";
  10.     cin >> n;
  11.  
  12.     int *array = new int[n];
  13.     cout << "Введите элементы массива:\n";
  14.     for (int i = 0; i < n; ++i) {
  15.         cin >> array[i];
  16.     }
  17.  
  18.     int searchValue;
  19.     cout << "Введите значение для поиска: ";
  20.     cin >> searchValue;
  21.  
  22.     int foundIndex = -1;
  23.     for (int i = 0; i < n; ++i) {
  24.         if (array[i] == searchValue) {
  25.             foundIndex = i;
  26.             break;
  27.         }
  28.     }
  29.  
  30.     if (foundIndex != -1) {
  31.         cout << "Индекс первого вхождения элемента: " << foundIndex << endl;
  32.     } else {
  33.         cout << "Элемент не найден в массиве." << endl;
  34.     }
  35.  
  36.     delete[] array;
  37.     return 0;
  38. }
  39.  
  40. // 2)
  41. #include <iostream>
  42. using namespace std;
  43.  
  44. int main() {
  45.     int n;
  46.     cout << "Введите размер массива: ";
  47.     cin >> n;
  48.  
  49.     int *array = new int[n];
  50.     cout << "Введите элементы массива:\n";
  51.     for (int i = 0; i < n; ++i) {
  52.         cin >> array[i];
  53.     }
  54.  
  55.     int newElement;
  56.     cout << "Введите новый элемент: ";
  57.     cin >> newElement;
  58.  
  59.     int insertIndex;
  60.     cout << "Введите индекс для вставки: ";
  61.     cin >> insertIndex;
  62.  
  63.     // Проверка корректности индекса
  64.     if (insertIndex >= n || insertIndex < 0) {
  65.         cout << "Некорректный индекс. Индекс должен быть в пределах от 0 до " << n - 1 << "." << endl;
  66.         return 1;
  67.     }
  68.  
  69.     // Сдвиг элементов на одну позицию вправо
  70.     for (int i = n; i > insertIndex; --i) {
  71.         array[i] = array[i - 1];
  72.     }
  73.  
  74.     // Вставка нового элемента
  75.     array[insertIndex] = newElement;
  76.  
  77.     cout << "Измененный массив:\n";
  78.     for (int i = 0; i < n + 1; ++i) {
  79.         cout << (i == insertIndex ? newElement : array[i]) << " ";
  80.     }
  81.     cout << endl;
  82.  
  83.     delete[] array;
  84.     return 0;
  85. }
  86.  
  87. // 3)
  88. #include <iostream>
  89. using namespace std;
  90.  
  91. int main() {
  92.     int n;
  93.     cout << "Введите размер массива: ";
  94.     cin >> n;
  95.  
  96.     int *array = new int[n];
  97.     cout << "Введите элементы массива:\n";
  98.     for (int i = 0; i < n; ++i) {
  99.         cin >> array[i];
  100.     }
  101.  
  102.     int deleteIndex;
  103.     cout << "Введите индекс для удаления: ";
  104.     cin >> deleteIndex;
  105.  
  106.     // Проверка корректности индекса
  107.     if (deleteIndex >= n || deleteIndex < 0) {
  108.         cout << "Некорректный индекс. Индекс должен быть в пределах от 0 до " << n - 1 << "." << endl;
  109.         return 1;
  110.     }
  111.  
  112.     // Сдвиг элементов на одну позицию влево
  113.     for (int i = deleteIndex; i < n - 1; ++i) {
  114.         array[i] = array[i + 1];
  115.     }
  116.  
  117.     cout << "Измененный массив:\n";
  118.     for (int i = 0; i < n - 1; ++i) {
  119.         cout << array[i] << " ";
  120.     }
  121.     cout << endl;
  122.  
  123.     delete[] array;
  124.     return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement