class myQueue { private: int size, *head, *tail, *data; public: myQueue(int size) // конструктор : size(size) { data = new int[size]; head = data; tail = data + size - 1; } ~myQueue() // деструктор { delete[]data; } void input(int el, int& i) { data[i] = el; } void maxEl() { int max = *data; while (head != tail) { head++; if (*head > max) { max = *head; } } cout << max; } }; int main() { setlocale(LC_ALL, "rus"); myQueue q(5); int n = 0, el; while (n < 5) { cin >> el; q.input(el, n); n++; } q.maxEl(); system("pause"); return 0; } 

There is a class queue in which to find the maximum element. But the maximum element is always the first element. How to fix?

    2 answers 2

    In fact, your code displays the maximum value correctly ...
    But the code itself raises a lot of questions.

    For example, in the constructor, you get an array of garbage . You are not initializing the array ...

    Secondly, why is the input index passed by reference? And anyway, what is the queue if you insert elements into it in an arbitrary place? A queue should have two functions - inserting an element at the end, and removing an element from the beginning. And you?

    In maxEl you change the value of head , so after that all your functions for working with the queue can no longer work ...

    And yet - the function should not do disparate things - for example, search for the maximum element and output it. I think that your function should return the maximum element, and displays it, let it be main , for example.

      1. First, this is not a queue - because the queue must support the operation of extracting / inserting elements, but I did not see it here.
      2. Secondly, your turn becomes one-time, after the maxEl method is maxEl - you change the head value - when you call again, the bypass will start from the last element. To be precise, it will not start at all.