作业帮 > 综合 > 作业

PASCAL,帮忙编循环程序,

来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/11/14 13:54:55
PASCAL,帮忙编循环程序,
试编一程序,输入十个数,求最大数、最小数、最大数位置、最小数位置、平均数、总和,用WHILE循环、FOR循环做,似乎还要下标,两种都要!
PASCAL,帮忙编循环程序,
一、用for无条件循环做
var
imax, imin, max_pos, min_pos, num, sum, i: Longint;
begin
imax := -2147483647;
imin := 2147483647;
max_pos := 0;
min_pos := 0;
sum := 0;
for i := 1 to 10 do
begin
Read(num);
if num > imax then
begin
imax := num;
max_pos := i;
end;
if num < imin then
begin
imin := num;
min_pos := i;
end;
Inc(sum, num);
end;
Writeln('Max Number: ', imax, ', Position: ', max_pos);
Writeln('Min Number: ', imin, ', Position: ', min_pos);
Writeln('Average: ', sum/10);
Writeln('Summation: ', sum);
Readln;
Readln;
end.
二、用while条件循环做
var
imax, imin, max_pos, min_pos, num, sum, i: Longint;
begin
imax := -2147483647;
imin := 2147483647;
max_pos := 0;
min_pos := 0;
sum := 0;
i := 1;
while i imax then
begin
imax := num;
max_pos := i;
end;
if num < imin then
begin
imin := num;
min_pos := i;
end;
Inc(sum, num);
Inc(i);
end;
Writeln('Max Number: ', imax, ', Position: ', max_pos);
Writeln('Min Number: ', imin, ', Position: ', min_pos);
Writeln('Average: ', sum/10);
Writeln('Summation: ', sum);
Readln;
Readln;
end.