Вопрос задан 19.07.2023 в 01:13. Предмет Информатика. Спрашивает Борозенец Юлия.

C или С++. (укр)Масив містить довільні значення. Переписати у зворотньому порядку значення

елементів масиву, які розміщені між мінімальним та максимальним значенням масиву. (рус)Массив содержит произвольные значения. Переписать в обратном порядке значения элементов массива, расположенных между минимальным и максимальным значением массива.
0 0
Перейти к ответам

Ответы на вопрос

Внимание! Ответы на вопросы дают живые люди. Они могут содержать ошибочную информацию, заблуждения, а также ответы могут быть сгенерированы нейросетями. Будьте внимательны. Если вы уверены, что ответ неверный, нажмите кнопку "Пожаловаться" под ответом.
Отвечает Акетай Аружан.

#define UPPERLIMIT 1000

#define LOWERLIMIT 0

#include <iostream>

#include <vector>

#include <cstddef>

#include <algorithm>

#include <ctime>

using namespace std;

class Massive {

public:

   virtual void fill() = 0;

   virtual int get_max() = 0;

   virtual int get_min() = 0;

   virtual void reverse(int start, int end) = 0;

   virtual void out() = 0;

};

class MassiveHandler : public Massive {

private:

   std::vector<int>::iterator result;

public:

   std::vector<int> array;

   MassiveHandler(std::size_t n) : array(n) {

       this->fill();

   }

   void fill() override {

       srand(time(0));

       for (int i = 0; i < this->array.size(); ++i)

           this->array[i] = rand() % (UPPERLIMIT - LOWERLIMIT + 1) + LOWERLIMIT;

   }

   void routine() {

       this->reverse(this->get_min(), this->get_max());

   }

   void reverse(int start_v, int end_v) override {

       if (start_v > end_v)

           swap(start_v, end_v);

       for (int i = start_v, j = end_v; i < j; i++, j--)

           swap(this->array[i], this->array[j]);

   }

   int get_max() override {

       this->result = std::max_element(this->array.begin(), this->array.end());

       return std::distance(this->array.begin(), result);

   }

   int get_min() override {

       this->result = std::min_element(this->array.begin(), this->array.end());

       return std::distance(this->array.begin(), result);

   }

   void out() override {

       for (int i = 0; i < this->array.size(); ++i)

           cout << this->array[i] << " ";

       cout << endl;

   }

};

int main() {

   int n;

   cin >> n;

   MassiveHandler *MH = new MassiveHandler(n);

   cout << "MAX: " << MH->array[MH->get_max()] << endl;

   cout << "MIN: " << MH->array[MH->get_min()] << endl;

   cout << endl;

   cout << "Before: ";

   MH->out();

   MH->routine();

   cout << "After:  ";

   MH->out();

}

0 0
Отвечает Тюш Ксюша.

#include <iostream>

#include <vector>

#include <algorithm>

#include <ctime>

using namespace std;

int main()

{

srand(time(NULL));

int n, max = 0, index1, min = 0, index2;

cin >> n;

cout << "RANDOM ARRAY:\n";

vector <int> a(n);

for (int i = 0; i < n; i++)

{

 a[i] = rand() % 101;

 if (a[i] > max) {

  max = a[i]; index1 = i;

 }

 if (a[i] < min || min == 0) {

  min = a[i]; index2 = i;

 }

 cout << a[i] << " ";

}

cout << "\nARRAY:\n";

if (index2 < index1)

{

 int x;

 x = index2;

 index2 = index1;

 index1 = x;

}

reverse(a.begin() + index1, a.begin() + index2);

for (int i = 0; i < n; i++)

{

 cout << a[i] << " ";

}

return 0;

}

0 0
Отвечает нейросеть ChatGpt. Будьте внимательны, ответы нейросети могут содержать ошибочные или выдуманные данные.

Для розв'язання цього завдання можна написати програму на мові програмування C++.

Основний план дій:

  1. Знайти мінімальне та максимальне значення в масиві.
  2. Знайти індекси цих елементів в масиві.
  3. Переписати у зворотньому порядку значення елементів масиву, які знаходяться між цими індексами.

Ось програмний код на C++, що реалізує цей алгоритм:

cpp
#include <iostream> void reverseArray(int arr[], int start, int end) { while (start < end) { std::swap(arr[start], arr[end]); start++; end--; } } int main() { const int size = 10; // Замініть це значення на розмір вашого масиву int arr[size] = {3, 5, 1, 8, 2, 7, 4, 6, 9, 10}; // Замініть це значення на ваш масив // Знайдемо мінімальне та максимальне значення в масиві int minVal = arr[0]; int maxVal = arr[0]; int minIndex = 0; int maxIndex = 0; for (int i = 1; i < size; i++) { if (arr[i] < minVal) { minVal = arr[i]; minIndex = i; } if (arr[i] > maxVal) { maxVal = arr[i]; maxIndex = i; } } // Перепишемо у зворотньому порядку значення елементів масиву, які знаходяться між minIndex та maxIndex if (minIndex < maxIndex) { reverseArray(arr, minIndex + 1, maxIndex - 1); } else { reverseArray(arr, maxIndex + 1, minIndex - 1); } // Виведемо змінений масив std::cout << "Масив після переписування: "; for (int i = 0; i < size; i++) { std::cout << arr[i] << " "; } std::cout << std::endl; return 0; }

Будь ласка, замініть значення масиву arr та розмір size на ваші власні дані, щоб протестувати програму з вашими вхідними даними.

0 0

Похожие вопросы

Топ вопросов за вчера в категории Информатика

Последние заданные вопросы в категории Информатика

Задать вопрос