Program Sort;
Const Size=5;
Var i,j,min,k,buf: Integer;
a: Array[1..Size] of Integer;
Begin
Writeln (‘Massivni puzirek(kupikcha) usulida tartiblashtirish’);
Write (Size:3,’ta massiv elementini kiriting’);
For k:=1 to Size Do Read(a[k]);
Writeln (‘Tartiblatirish’);
For i:=1 to Size-1 Do
Begin
For k:=1 to Size-1 Do
Begin
If a[k]>a[k+1] then
Begin
buf:=a[k]; a[k]:=a[k+1]; a[k+1]:=buf;
End;
End;
For k:=1 to Size Do Write (a[k],’ ‘);
Writeln;
End;
Writeln(‘Massiv tartiblashtirildi.’);
End.
Dastur natijasi: Massivni puzirek usulida tartiblashtirish 5 ta massiv elementini kiriting
3 2 4 1 5
Tartiblashtirish
2 3 4 1 5
2 3 1 4 5
2 1 3 4 5
1 2 3 4 5
Massiv tartiblashtirildi.
Massivda eng kichik yoki eng katta elementni izlash algoritmi ma’lumki birinchi element eng kichik (katta) deb olinib keyin boshqa elementlar bilan ketma-ket solishtirilib chiqiladi. Solishtirilish oxirgi elementgacha bajariladi. Quyida bu algoritm dastursi keltirilgan:
Program MinMax;
Var i,min: Integer;
a: Array[1..10] of Integer;
Begin
Writeln (‘Massivdan eng kichik elementni izlash’);
Write (’ 10-ta massiv elementini kiriting’);
For i:=1 to 10 Do Read(a[i]);
min:=1;
For i:=2 to 10 Do
If a[i]
Writeln(‘Izlanayotgan eng kichik element:’,a[min]);
Writeln(‘Element nomeri’,min);
End.
Massivlar ustida amallar
Dinamik massiv ta’riflanganda uning uzunligini ko’rsatish shart emas. Massiv uzunligini o’rnatish uchun SetLength funktsiyasidan foydalanish mumkin. Uning ikki parametri mavjud:
1. Dinamik massiv tipidagi o’zgaruvchi. 2. Massiv uzunligi. High(r) funksiyasi massiv elementlari sonini qaytaradi.
Misol:
Var r: array of integer; i:Integer;
begin
SetLength(r,10);
for i:=0 to High(r)-1 do
begin
r[i]:=i*i;
writeln (IntToStr(i)+' kvadrati ='+IntToStr(r[i]));
end;
IntToStr funktsiyasi sonni satrga aylantiradi. Endi mаssivlаr ustidа tipik аmаllаr bаjаruvchi bir nеchtа prоgrаmmа bilаn tаnishib chiqаylik.
1. Bir o’lchаmli n tа hаdli (n=30) mаssiv hаdlаrini yig’ish:
const n=30;
var i: integer; x: array [1..n] of real; S: real;
begin
for i: =1 to n do readln (x[i]); { mаssiv хаdlаrini kiritish}
S: =0;
for i: =1 to n do S: =S+x[i];
writeln (‘nаtijа=’, S)
end;
2. Bir o’lchаmli, n tа hаdli (n=30) mаssiv hаdlаrining eng kаttаsini tоpish vа uning jоylаshgаn jоyini аniqlаsh.
const n=30;
type gran = 1..30; vector = array [gran] of real;
var x: vector; S: real; l: byte; k: integer;
begin
writeln (‘ х - mаssivi хаdlаrini kiriting’);
for i: =1 to n do readln (x[i]);
S: =x[1]; k: =1;
for i: =2 to n do
if x[i] > S then
begin
S: =x[i]; k: =i
end;
writeln (‘х mаssivininig eng kаttа хаdi’);
writeln (S);
writeln (‘max(x) ning urni’, k)
end;
Do'stlaringiz bilan baham: |