program AveragePrice;
uses crt;
const
N = 20;
MIN_PRICE = 100;
MAX_PRICE = 200;
var
prices: array[1..N] of integer;
total: integer;
average: real;
i: integer;
function CalculateAverage(var arr: array of integer; count: integer): real;
var
sum: integer;
j: integer;
begin
sum := 0;
for j := Low(arr) to High(arr) do
sum := sum + arr[j];
if count > 0 then
Result := sum / count
else
Result := 0;
end;
begin
randomize;
writeln('-----------------------------');
writeln(' Программа Средней Цены ');
writeln('-----------------------------');
total := 0;
try
writeln('Стоимость товаров:');
for i := 1 to N do
begin
prices[i] := random(MAX_PRICE - MIN_PRICE + 1) + MIN_PRICE;
if (prices[i] < MIN_PRICE) or (prices[i] > MAX_PRICE) then
raise Exception.Create('Ошибка: Сгенерированное значение вне допустимого диапазона.');
write(prices[i]:4);
if (total > MaxInt - prices[i]) then
raise Exception.Create('Ошибка: Переполнение суммы цен.');
total := total + prices[i];
end;
writeln;
average := CalculateAverage(prices, N);
writeln('-----------------------------');
writeln('Средняя стоимость товаров: ', average:0:2);
except
on E: Exception do
writeln('Ошибка: ', E.Message);
end;
writeln('-----------------------------');
readln;
end.