Вопрос задан 17.02.2019 в 17:39. Предмет Информатика. Спрашивает Чугунов Иван.

Срочно надо сдать!Написать программу на языке С++ Дана последовательность n различных целых чисел.

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

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

Внимание! Ответы на вопросы дают живые люди. Они могут содержать ошибочную информацию, заблуждения, а также ответы могут быть сгенерированы нейросетями. Будьте внимательны. Если вы уверены, что ответ неверный, нажмите кнопку "Пожаловаться" под ответом.
Отвечает Сафрошкин Клим.
#include <iostream>
using namespace std;
int main()
{
 int n,max, min, pos1, pos2, mem, sum;
cin >> n;
pos1 = 0;
pos2 = 0;
int *A = new int[n];
for (int i = 0; i < n; i++)
{
 cin >> A[i];
}
min = A[0];
max = A[0];
for (int i = 0; i < n; i++)
 {
 if (A[i] > max)
 {
  max = A[i];
  pos1 = i;
 }
 if (A[i] < min)
 {
  min = A[i];
  pos2 = i;
 }
}
if (pos2 > pos1)
{
 mem = pos2;
 pos2 = pos1;
 pos1 = mem;
}
sum = 0;
for (pos2; pos2 <= pos1; pos2++)
{
 sum = sum + A[pos2];
}
cout << sum;
system("pause");
return 0;
}
0 0
Отвечает нейросеть ChatGpt. Будьте внимательны, ответы нейросети могут содержать ошибочные или выдуманные данные.

Program in C++ to Find the Sum of Elements Between the Maximum and Minimum Values in a Sequence

Here's a program in C++ that finds the sum of elements located between the maximum and minimum values in a sequence, including both the maximum and minimum values.

```cpp #include #include using namespace std;

int main() { int n; cout << "Enter the number of elements in the sequence: "; cin >> n;

int arr[n]; cout << "Enter the elements of the sequence: "; for (int i = 0; i < n; i++) { cin >> arr[i]; }

int max_val = INT_MIN; int min_val = INT_MAX; int max_index, min_index;

for (int i = 0; i < n; i++) { if (arr[i] > max_val) { max_val = arr[i]; max_index = i; } if (arr[i] < min_val) { min_val = arr[i]; min_index = i; } }

int sum = 0; if (max_index < min_index) { for (int i = max_index; i <= min_index; i++) { sum += arr[i]; } } else { for (int i = min_index; i <= max_index; i++) { sum += arr[i]; } }

cout << "The sum of elements between the maximum and minimum values is: " << sum << endl;

return 0; } ```

This program first takes the number of elements in the sequence as input and then the elements themselves. It then finds the maximum and minimum values along with their indices in the sequence. After that, it calculates the sum of the elements between the maximum and minimum values, including the maximum and minimum values themselves.

Note: This program assumes that the input sequence contains distinct integers.

Let me know if you need further assistance!

0 0

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

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

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