Вопрос задан 20.02.2019 в 09:24. Предмет Информатика. Спрашивает Кусь Екатерина.

Ребят, хелп! Как найти максимальный элемент массива среди первых i элементов? Растолкуйте

поподробней пожалуйста. Допустим есть массив a[1..6] и как найти максимальный элемент из первых трех?
0 0
Перейти к ответам

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

Внимание! Ответы на вопросы дают живые люди. Они могут содержать ошибочную информацию, заблуждения, а также ответы могут быть сгенерированы нейросетями. Будьте внимательны. Если вы уверены, что ответ неверный, нажмите кнопку "Пожаловаться" под ответом.
Отвечает Жданков Влад.
Алгоритм примерно такой (max - максимальный элемент):
1) max:=a[1];
2) пускаем цикл от двух до n (n - третий, например)
for i:=2 to n do
if max<a[i] then max:=a[i];
Получается, что если наш максимальный элемент, который изначально равен первому элементу массива, меньше какого-либо из последующих, то мы нашему max присваиваем этот элемент
0 0
Отвечает нейросеть ChatGpt. Будьте внимательны, ответы нейросети могут содержать ошибочные или выдуманные данные.

Finding the Maximum Element in the First i Elements of an Array

To find the maximum element in the first i elements of an array, you can use a simple algorithm to iterate through the elements and keep track of the maximum value encountered so far. Here's a step-by-step explanation of how to achieve this:

1. Initialize a Variable to Store the Maximum Element: - Start by initializing a variable to store the maximum element found so far. Let's call this variable `maxElement` and set it to the value of the first element in the array.

2. Iterate Through the First i Elements: - Next, iterate through the first i elements of the array. For each element, compare it with the current value of `maxElement`. If the element is greater than `maxElement`, update the value of `maxElement` to be the value of the current element.

3. Return the Maximum Element: - After iterating through the first i elements, the variable `maxElement` will hold the maximum value among those elements.

Example Using Array a[1..6]

Let's consider an example with the array a[1..6] and the task of finding the maximum element among the first three elements.

```plaintext a = [5, 8, 3, 12, 7, 9] ```

1. Initialize maxElement: - Start by setting `maxElement` to the value of the first element in the array: `maxElement = 5`.

2. Iterate Through the First 3 Elements: - Compare each element with `maxElement` and update `maxElement` if a larger value is encountered. - Compare 8 with `maxElement` (5): 8 > 5, so update `maxElement` to 8. - Compare 3 with `maxElement` (8): 3 is not greater than 8, so no update. - After iterating through the first 3 elements, `maxElement` holds the maximum value: `maxElement = 8`.

Therefore, the maximum element among the first three elements of the array a[1..6] is 8.

I hope this explanation helps! If you have any further questions or need additional clarification, feel free to ask.

0 0

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

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

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