Вопрос задан 01.05.2019 в 04:28. Предмет Информатика. Спрашивает Прудник Катя.

ПОМОГИТЕ,ПОЖАЛУЙСТА!! СЕГОДНЯ НАДО!!Turbo P.Дан массив из 20 действительных чисел. Вычислить

отдельно сумму положительных элементов массива, отдельно отрицательных.
0 0
Перейти к ответам

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

Внимание! Ответы на вопросы дают живые люди. Они могут содержать ошибочную информацию, заблуждения, а также ответы могут быть сгенерированы нейросетями. Будьте внимательны. Если вы уверены, что ответ неверный, нажмите кнопку "Пожаловаться" под ответом.
Отвечает Щербина Даша.
Турбо Паскаля нет, вставьте первым оператором Uses CRT


const
  n = 20;

var
  x: array[1..n] of real;
  i: integer;
  sp, sn: real;

begin
  Randomize;
  Write('Элементы массива: ');
  sp := 0; sn := 0;
  for i := 1 to n do
  begin
    x[i] := (Random(101) - 50) / 1.33;
    Write(x[i]:0:5, ' ');
    if x[i] > 0 then sp := sp + x[i]
    else if x[i] < 0 then sn := sn + x[i]{нули тоже сюда, они не мешают}
  end;
  Writeln(#13#10, 'Сумма положительных=', sp:0:5, ', отрицательных=', sn:0:5)
end.

Тестовое решение:
Элементы массива: 3.75940 21.80451 -16.54135 9.77444 10.52632 -7.51880 32.33083 -23.30827 12.03008 -3.00752 25.56391 24.81203 24.06015 -9.02256 -11.27820 34.58647 25.56391 9.02256 -27.06767 6.01504
Сумма положительных=239.84962, отрицательных=-97.74436


0 0
Отвечает нейросеть ChatGpt. Будьте внимательны, ответы нейросети могут содержать ошибочные или выдуманные данные.

Calculating Sum of Positive and Negative Elements in an Array

To calculate the sum of positive and negative elements in an array of 20 real numbers, you can use the following approach:

1. Sum of Positive Elements: - Iterate through the array and add up all the positive elements. - Initialize a variable to store the sum of positive elements. - Loop through the array and for each positive element, add it to the sum.

2. Sum of Negative Elements: - Iterate through the array and add up all the negative elements. - Initialize a variable to store the sum of negative elements. - Loop through the array and for each negative element, add it to the sum.

Here's a simple example of how you can achieve this in Turbo Pascal:

```pascal program CalculateSum; var arr: array[1..20] of real; { Assuming the array is already populated } i: integer; sumPositive, sumNegative: real;

begin sumPositive := 0; sumNegative := 0;

{ Loop through the array to calculate the sum of positive and negative elements } for i := 1 to 20 do begin if arr[i] > 0 then sumPositive := sumPositive + arr[i] else if arr[i] < 0 then sumNegative := sumNegative + arr[i]; end;

writeln('Sum of positive elements: ', sumPositive); writeln('Sum of negative elements: ', sumNegative); end. ```

This program initializes the sums of positive and negative elements to zero and then iterates through the array, adding each positive element to the sum of positive elements and each negative element to the sum of negative elements.

Note: This is a basic example to illustrate the concept. In a real-world scenario, you would need to handle input validation, error checking, and other considerations.

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

0 0

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

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

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