Tất cả Các thao tác trong mảng
- Code:
{In Mang}
var A:array[1..100] of integer;
tong,j,min,max,maxd,csmaxd,i,csmin,csmax,tg,n,tongle,tongchan,demam,demduong,demchan,demle:integer;
tb:real;
begin
writeln('nhap so phan tu: ');readln(n);
for i:=1 to n do
begin
writeln('gia tri phan tu thu ',i,':= ');
readln(a[i]);
end;
readln;
for i:=1 to n do
write(a[i]:4);
readln;
{Day Tang Dan}
{
for i:=1 to (n-1) do
for j:=i+1 to n do
if a[i]>a[j] then
begin
tg:=a[i];
a[i]:=a[j];
a[j]:=tg;
end;
for i:=1 to n do
write(a[i]:4);
readln; }
{Day Giam Dan}
{
for i:=1 to (n-1) do
for j:=i+1 to n do
if a[i]<a[j] then
begin
tg:=a[i];
a[i]:=a[j];
a[j]:=tg;
end;
for i:=1 to n do
write(a[i]:4);
readln; }
{Gia Tri Lon Nhat-Nho Nhat}
max:=a[1];
csmax:=1;
for i:=1 to n do
if max<a[i] then
begin
max:=a[i];
csmax:=i;
end;
writeln('phan tu lon nhat la: ',max,', chi so la: ',csmax);
min:=a[1];
csmin:=1;
for i:=1 to n do
if min>a[i] then
begin
min:=a[i];
csmin:=i;
end;
writeln('phan tu be nhat la: ',min,', chi so la: ',csmin);
readln;
{Tinh Tong-Trung Binh Cong}
tong:=0;
for i:=1 to n do
begin
tong:=tong+a[i];
tb:=tong/n;
end;
writeln('tong cua cac phan tu la: ',tong:6);
writeln('trung binh cong cua cac phan tu la: ',tb:6:2);
readln;
{Tong cac so chan-le}
tongchan:=0;
tongle:=0;
for i:=1 to n do
begin
if a[i]>0 then
tongchan:=tongchan+a[i];
if a[i]<0 then
tongle:=tongle+a[i];
end;
writeln('tong cac so chan:= ',tongchan);
writeln('tong cac so le:= ',tongle);
readln;
{Tong Cac so am-duong}
demduong:=0;
demam:=0;
for i:=1 to n do
begin
if a[i]>0 then demduong:=demduong+1;
if a[i]<0 then demam:=demam+1;
end;
writeln('co tat ca ',demduong,' so duong');
writeln('co tat ca ',demam,' so am');
readln;
{Co bao nhieu so chan-le}
demchan:=0;
demle:=0;
for i:=1 to n do
begin
if (a[i] mod 2=0) and (a[i]<>0) then demchan:=demchan+1;
if (a[i] mod 2=1) or (a[i] mod 2=-1) then demle:=demle+1;
end;
writeln('co tat ca ',demchan,' so chan');
writeln('co tat ca ',demle,' so le');
readln;
{Phan tu duong dau tien-cuoi cung}
i:=1;
while (i<=n) and (a[i]<0) do i:=i+1;
if i>n then
writeln('khong co phan tu duong dau tien') else
writeln('phan tu duong dau tien la',a[i]:4,', chi so la: ',i:2);
i:=n;
while (i>=1) and (a[i]<0) do i:=i-1;
if i<1 then
writeln('khong co phan tu duong cuoi cung') else
writeln('phan tu duong cuoi cung la: ',a[i]:4,', chi so la: ',i:2);
readln;
{Phan tu am dau tien-cuoi cung}
i:=1;
while (i<=n) and (a[i]>=0) do i:=i+1;
if i>n then
writeln('khong co phan tu am dau tien') else
writeln('phan tu am dau tien la',a[i]:4,', chi so la: ',i:2);
i:=n;
while (i>=1) and (a[i]>=0) do i:=i-1;
if i<1 then
writeln('khong co phan tu am cuoi cung') else
writeln('phan tu am cuoi cung la: ',a[i]:4,', chi so la: ',i:2);
readln;
{Phan tu chan dau tien-cuoi cung}
i:=1;
while (i<=n) and (a[i] mod 2=1) do i:=i+1;
if (i>n) or (i=0) then
writeln('khong co phan tu chan dau tien') else
writeln('phan tu chan dau tien la: ',a[i]:4,', chi so la: ',i:2);
i:=n;
while (i>=1) and (a[i] mod 2=1) do i:=i-1;
if i<1 then
writeln('khong co phan tu chan cuoi cung') else
writeln('phan tu chan cuoi cung la: ',a[i]:4,', chi so la: ',i:2);
readln;
{Phan tu le dau tien-cuoi cung}
i:=1;
while (i<=n) and (a[i] mod 2=0) do i:=i+1;
if i>n then
writeln('khong co phan tu le dau tien') else
writeln('phan tu le dau tien la: ',a[i]:4,', chi so la: ',i:2);
i:=n;
while (i>=1) and (a[i] mod 2=0) do i:=i-1;
if i<1 then
writeln('khong co phan tu le cuoi cung') else
writeln('phan tu le cuoi cung la: ',a[i]:4,', chi so la: ',i:2);
readln;
{Tim so duong be nhat}
i:=1;
while(i<=n) and (a[i]<=0) do i:=i+1;
if i>n then write('khong co phan tu duong be nhat') else
begin
if a[i]>0 then
maxd:=a[i];
csmaxd:=i;
for i:=csmaxd to n do
if (a[i]<=maxd) and (a[i]>0) then
begin
maxd:=a[i];
csmaxd:=i;
end;
write('co so duong be nhat: ',maxd,', chi so: ',csmaxd);
end;
readln;
writeln;
writeln('nhan enter de tro lai');
readln;