0

почему max_element работает неправильно ?

введите сюда описание изображения

оно вывело 6 вместо наибольшего элемента 7

#include <iostream>
#include <algorithm>


using namespace std;

int main() {
    int n, mx;
    cin >> n;
    int arr[9]{};
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
        
    }
    mx = *max_element (arr, arr);
    cout << arr[mx];
}
        
3
  • 2
    mx = *max_element(arr, arr + n); cout << mx;
    – user361068
    10 ноя 2020 в 11:09
  • max = *max_element(arr, arr + n); не работает, выдаёт a value of type "int *" cannot be used to initialize an entity of type "int"
    – user410415
    10 ноя 2020 в 11:10
  • ideone.com/iIaB74
    – user361068
    10 ноя 2020 в 11:13

1 ответ 1

0
#include <iostream>
#include <algorithm>

using namespace std;

int main() {
    int n, mx;
    cin >> n;
    int *arr = new int[n];
    for (int i = 0; i < n; i++)
        cin >> arr[i];
    mx = *max_element (arr, arr+n);
    cout << mx;
    delete[] arr;
    }

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.