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

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

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.



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!


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