
Срочно надо сдать!Написать программу на языке С++ Дана последовательность n различных целых чисел.
Найти сумму ее членов,расположенных между максимальным и минимальным значениями(в сумму включить и оба этих числа).

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

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;
}



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
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!


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







Последние заданные вопросы в категории Информатика
-
Математика
-
Литература
-
Алгебра
-
Русский язык
-
Геометрия
-
Английский язык
-
Химия
-
Физика
-
Биология
-
Другие предметы
-
История
-
Обществознание
-
Окружающий мир
-
География
-
Українська мова
-
Информатика
-
Українська література
-
Қазақ тiлi
-
Экономика
-
Музыка
-
Право
-
Беларуская мова
-
Французский язык
-
Немецкий язык
-
МХК
-
ОБЖ
-
Психология
-
Физкультура и спорт
-
Астрономия
-
Кыргыз тили
-
Оʻzbek tili