Вопрос задан 07.05.2019 в 01:05. Предмет Информатика. Спрашивает Галяутдинова Рената.

В данном массиве чисел найдите среднее арифметическое положительных элементов массива. Составить

программу для Pascal ABC
0 0
Перейти к ответам

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

Внимание! Ответы на вопросы дают живые люди. Они могут содержать ошибочную информацию, заблуждения, а также ответы могут быть сгенерированы нейросетями. Будьте внимательны. Если вы уверены, что ответ неверный, нажмите кнопку "Пожаловаться" под ответом.
Отвечает Уельданова Элина.
var    
     arr : array[1..99] of integer;
     N, s, k : integer;
begin
    read(N);
    for var i := 1 to N do begin
        arr[i] := random(-10, 10);
        writeln(arr[i]);
        if arr[i] > 0 then begin
            s += arr[i];
            k += 1;
        end
    end;
    writeln(s/k);
end.
0 0
Отвечает нейросеть ChatGpt. Будьте внимательны, ответы нейросети могут содержать ошибочные или выдуманные данные.

Finding the average of positive elements in an array

To find the average of positive elements in an array, we first need to define the array and its size. Let's call the array `a` and its size `n`:

```pascal const n = 10; var a: array[1..n] of integer; ```

Next, we generate random numbers to populate the array. Using the `random` function from Pascal ABC library:

```pascal for i:=1 to n do a[i]:=random(10); ```

Now, we calculate the sum of positive elements. We initialize a variable `sum` to store the running total:

```pascal var sum := 0; ```

Then we loop through the array and add elements to `sum` only if they are positive:

```pascal for i:=1 to n do if a[i]>0 then sum := sum + a[i]; ```

Finally, we calculate the average. We divide the sum by the number of positive elements. To count positives, we initialize a counter `count`:

```pascal var count := 0; ```

And increment it in the previous loop:

```pascal for i:=1 to n do if a[i]>0 then begin sum := sum + a[i]; count := count + 1; end; ```

The average is the sum divided by the count:

```pascal average := sum / count; ```

We output the result and we're done!

0 0

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

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

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