Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Дз от 09.12.2024г.
- // 1)
- #include <iostream>
- using namespace std;
- int main() {
- int n;
- cout << "Введите размер массива: ";
- cin >> n;
- int *array = new int[n];
- cout << "Введите элементы массива:\n";
- for (int i = 0; i < n; ++i) {
- cin >> array[i];
- }
- int searchValue;
- cout << "Введите значение для поиска: ";
- cin >> searchValue;
- int foundIndex = -1;
- for (int i = 0; i < n; ++i) {
- if (array[i] == searchValue) {
- foundIndex = i;
- break;
- }
- }
- if (foundIndex != -1) {
- cout << "Индекс первого вхождения элемента: " << foundIndex << endl;
- } else {
- cout << "Элемент не найден в массиве." << endl;
- }
- delete[] array;
- return 0;
- }
- // 2)
- #include <iostream>
- using namespace std;
- int main() {
- int n;
- cout << "Введите размер массива: ";
- cin >> n;
- int *array = new int[n];
- cout << "Введите элементы массива:\n";
- for (int i = 0; i < n; ++i) {
- cin >> array[i];
- }
- int newElement;
- cout << "Введите новый элемент: ";
- cin >> newElement;
- int insertIndex;
- cout << "Введите индекс для вставки: ";
- cin >> insertIndex;
- // Проверка корректности индекса
- if (insertIndex >= n || insertIndex < 0) {
- cout << "Некорректный индекс. Индекс должен быть в пределах от 0 до " << n - 1 << "." << endl;
- return 1;
- }
- // Сдвиг элементов на одну позицию вправо
- for (int i = n; i > insertIndex; --i) {
- array[i] = array[i - 1];
- }
- // Вставка нового элемента
- array[insertIndex] = newElement;
- cout << "Измененный массив:\n";
- for (int i = 0; i < n + 1; ++i) {
- cout << (i == insertIndex ? newElement : array[i]) << " ";
- }
- cout << endl;
- delete[] array;
- return 0;
- }
- // 3)
- #include <iostream>
- using namespace std;
- int main() {
- int n;
- cout << "Введите размер массива: ";
- cin >> n;
- int *array = new int[n];
- cout << "Введите элементы массива:\n";
- for (int i = 0; i < n; ++i) {
- cin >> array[i];
- }
- int deleteIndex;
- cout << "Введите индекс для удаления: ";
- cin >> deleteIndex;
- // Проверка корректности индекса
- if (deleteIndex >= n || deleteIndex < 0) {
- cout << "Некорректный индекс. Индекс должен быть в пределах от 0 до " << n - 1 << "." << endl;
- return 1;
- }
- // Сдвиг элементов на одну позицию влево
- for (int i = deleteIndex; i < n - 1; ++i) {
- array[i] = array[i + 1];
- }
- cout << "Измененный массив:\n";
- for (int i = 0; i < n - 1; ++i) {
- cout << array[i] << " ";
- }
- cout << endl;
- delete[] array;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement