Вопрос задан 26.02.2019 в 17:30. Предмет Информатика. Спрашивает Коробкова Екатерина.

Программа в pascal. При помощи генератора случайных чисел создать линейный числовой массив

состоящий из 15 элементов интервала -10:10 посчитать количество четных элементов массива,найти среднее арифметическое нечетных элементов, заменить все положительные элементы массива нулями,новый массив вывести на экран.
0 0
Перейти к ответам

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

Внимание! Ответы на вопросы дают живые люди. Они могут содержать ошибочную информацию, заблуждения, а также ответы могут быть сгенерированы нейросетями. Будьте внимательны. Если вы уверены, что ответ неверный, нажмите кнопку "Пожаловаться" под ответом.
Отвечает Озыкан Аружан.
Program n1;
const n=15;
var a: array[1..n] of integer;
kc,sum,kn,i: integer;
begin
randomize;
writeln('исходный массив:');
kc:=0;
sum:=0;
kn:=0;
for i:=1 to n do
begin
a[i]:=random(21)-10;
write(a[i],' ');
if a[i] mod 2=0 then kc:=kc+1;
if a[i] mod 2<>0 then
begin
kn:=kn+1;
sum:=sum+a[i];
end;
if a[i]>0 then a[i]:=0;
end;
writeln;
writeln('полученный массив:');
for i:=1 to n do write(a[i],' ');
writeln;
writeln('кол-во чет: ',kc);
writeln('кол-во нечет: ',kn);
writeln('срзнач неч: ',sum/kn);
end.
0 0
Отвечает Прудникова Юля.
// PascalABC.NET 3.2, сборка 1417 от 28.03.2017
// Внимание! Если программа не работает, обновите версию!

begin
  var a:=ArrRandom(15,-10,10); a.Println; // создали и вывели значения
  Writeln('Четных :',a.Where(n->n.IsEven).Count);
  Writeln('Ср.арифм. нечетных: ',a.Where(n->n.IsOdd).Average);
  a.Transform(n->n>0?0:n); a.Println // заменили и вывели значения
end.

Пример
6 -3 -9 -9 10 1 -1 -4 -6 5 7 8 4 -8 -3
Четных :7
Ср.арифм. нечетных: -1.5
0 -3 -9 -9 0 0 -1 -4 -6 0 0 0 0 -8 -3
0 0
Отвечает нейросеть ChatGpt. Будьте внимательны, ответы нейросети могут содержать ошибочные или выдуманные данные.

Program in Pascal: Counting Even Elements, Calculating Average of Odd Elements, and Replacing Positive Elements

Here's a program in Pascal that generates a linear numeric array consisting of 15 elements within the range -10 to 10. It then counts the number of even elements in the array, calculates the average of the odd elements, and replaces all positive elements with zeros. Finally, it outputs the new array.

```pascal program ArrayOperations;

uses crt;

var arr: array[1..15] of integer; i, evenCount, oddSum, oddCount: integer; oddAverage: real;

begin randomize;

// Generate the array for i := 1 to 15 do arr[i] := random(21) - 10;

// Count even elements evenCount := 0; for i := 1 to 15 do begin if arr[i] mod 2 = 0 then evenCount := evenCount + 1; end;

// Calculate average of odd elements oddSum := 0; oddCount := 0; for i := 1 to 15 do begin if arr[i] mod 2 <> 0 then begin oddSum := oddSum + arr[i]; oddCount := oddCount + 1; end; end; oddAverage := oddSum / oddCount;

// Replace positive elements with zeros for i := 1 to 15 do begin if arr[i] > 0 then arr[i] := 0; end;

// Output the new array writeln('Original Array:'); for i := 1 to 15 do write(arr[i], ' ');

writeln; writeln('Number of even elements: ', evenCount); writeln('Average of odd elements: ', oddAverage:0:2);

writeln('New Array:'); for i := 1 to 15 do write(arr[i], ' ');

readln; end. ```

This program uses the `randomize` procedure to initialize the random number generator. It then generates an array of 15 elements using the `random` function, which generates random numbers within the range 0 to 20. By subtracting 10 from each generated number, we get a range of -10 to 10.

The program counts the number of even elements in the array by iterating through each element and checking if it is divisible by 2. It also calculates the sum of the odd elements and counts the number of odd elements to calculate the average.

Finally, the program replaces all positive elements in the array with zeros by iterating through each element and checking if it is greater than zero.

The program outputs the original array, the number of even elements, the average of odd elements, and the new array after replacing positive elements with zeros.

Please note that the program uses the `crt` unit for console input/output operations. If you're using an online Pascal compiler or IDE, you may need to remove the `uses crt;` line and replace the `writeln` and `readln` statements with appropriate alternatives.

Let me know if you have any further questions!

0 0

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

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

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