
Найдите ошибку в программе Pascal Задание: Дано:a,b,c,d. Найти:среднее значение чисел
принадлежащих интервалу(1;30) и кратных 3. program zaschita; var s,k,a,b,c,d:integer; sr:real; begin write ('a='); readln(a); write ('b='); readln(b); write ('c='); readln(c); write ('d='); readln(d); s:=0; k:=0; a:=1; while (a<=30) do begin if (a mod 3=0) then s:=s+a; k:=k+1; end; b:=1; while (b<=30) do begin if (b mod 3=0) then s:=s+b; k:=k+1; end; c:=1; while (c<=30) do begin if (c mod 3=0) then s:=s+c; k:=k+1; end; d:=1; while (d<=30) do begin if (d mod 3=0) then s:=s+d; k:=k+1; end; sr:=s/k; writeln ('среднее значение=',sr); end.

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

2. Циклы while не нужны, они зацикливают программу.
3. Так как увеличение значения k и суммы нужно выполнять одновременно, то есть при выполнении условия, то нужно было эти команды заключить в операторные скобки begin ... end
Примерный вариант программы:
program z;
var a,b,c,d,k,s:integer;
begin
write('a='); readln(a);
write('b='); readln(b);
write('c='); readln(c);
write('d='); readln(d);
k:=0; s:=0;
if (a>1) and (a<30) and (a mod 3=0) then begin k:=k+1; s:=s+a; end;
if (b>1) and (b<30) and (b mod 3=0) then begin k:=k+1; s:=s+b; end;
if (c>1) and (c<30) and (c mod 3=0) then begin k:=k+1; s:=s+c; end;
if (d>1) and (d<30) and (d mod 3=0) then begin k:=k+1; s:=s+d; end;
if k=0 then writeln(Not solution') else writeln(s/k);
end.



There are several issues in the given Pascal program:
The variable
a
,b
,c
, andd
are being overwritten and used as loop counters. These variables should be separate from the loop counters.The loops are iterating from 1 to 30 for each variable a, b, c, and d, which is not necessary. Instead, you should iterate once from 1 to 30 and check for the conditions inside the loop.
The loop counters (
a
,b
,c
,d
) are not incremented, so the loops will run indefinitely.The calculation of the average is not correct. The sum should be divided by the number of elements that meet the conditions.
Here's the corrected version of the program:
pascalprogram zaschita; var a, b, c, d: integer; s, k: integer; sr: real; begin write('a='); readln(a); write('b='); readln(b); write('c='); readln(c); write('d='); readln(d); s := 0; k := 0; for a := 1 to 30 do begin if (a mod 3 = 0) then begin s := s + a; k := k + 1; end; end; for b := 1 to 30 do begin if (b mod 3 = 0) then begin s := s + b; k := k + 1; end; end; for c := 1 to 30 do begin if (c mod 3 = 0) then begin s := s + c; k := k + 1; end; end; for d := 1 to 30 do begin if (d mod 3 = 0) then begin s := s + d; k := k + 1; end; end; sr := s / k; writeln('среднее значение=', sr); end.
In this corrected version, we use separate variables (a
, b
, c
, d
) to store user input values, and we iterate once from 1 to 30 to find numbers that are multiples of 3 and calculate their sum (s
) and count (k
). Then, we calculate the average (sr
) correctly by dividing the sum by the count.


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