For sikli yordamida amalga oshirish mumkin. Elementni massiv boshiga (birinchi o’ringa) ko’chirish uchun prosedura


Download 36.98 Kb.
bet6/16
Sana01.04.2023
Hajmi36.98 Kb.
#1318578
1   2   3   4   5   6   7   8   9   ...   16
Bog'liq
masala 1

var
i : integer;
begin
for i := n + 1 downto k + 1 do a[i] := a[i - 1]
end;
Uning ishlash jarayonini o’zingiz o’rganib chiqing.
Dastur
Program L194;
uses Crt;
const
n = 20;
type
t = array[1..n + 1] of integer;
var
a : t; 
i, p, b : integer;
{----------------------------------------------------------------------------------------}
Procedure quick_search(a : t; n, b : integer; var p : integer);
var
q, s : integer;
begin
p := 1;
q := n + 1;
while p < q do
begin
s := (p + q) div 2;
if a[s] < b then p := s + 1 else q := s
end
end;
{---------------------------------------------------------------------------------------}
Procedure movement_end(n, k : integer; var a : t);
var
i : integer;
begin
for i := n + 1 downto k + 1 do a[i] := a[i - 1]
end;
{---------------------------------------------------------------------------------------}
begin
write('O’sish bo’yicha tartiblangan a massiv');
writeln('elementlarni kiriting');
for i := 1 to do
begin
write(i, 'chi elementni kiriting'); readln(a[i])
end;
writeln('Tartiblangan butun sonli massiv');
for i := 1 to do write(a[i], ' '); writeln;
write('Kiritish kerak bo’lgan sonni kiriting'); readln(b);
quick_search(a, n, b, p);
movement_end(n + 1, p, a);
a[p] := b;
writeln('Sonni kiritgandan so’ng hosil bo’lgan yangi massiv’, b);
for i := 1 to n + 1 do write(a[i], ' ');
writeln
end.
Dasturda yana bitta detal paydo bo’ldi. “O’ng” chegara, elementi nomerining boshlang’ich qiymati q ga massiv elementlari soniga qaraganda 1 taga kattaroq n+1 ga teng qiymat beriladi.
Nega? Agar bu dasturni q := n o’zgaruvchili qiymatda bir necha marta bajarsak, u holda dastur tomonidan quyidagi holda noto’g’ri natija chiqariladi:
O’sish bo’yicha tartiblangan massiv berilgan bo’lsin:
-92 -40 -26 -12 -7 -4 0 6 9 25 34 38 46 52 56 67 71 78 86 93.
Siz bu massivga tartiblangan massivning eng katta sonidan ham kattaroq bo’lgan 125 sonini kiritmoqchi bo’ldingiz. Shunda, dasturning ishlash jarayonida 125 soni oxiridan bitta oldingi o’ringa joylashtiriladi, ya’ni ekranda quyidagi natija hosil bo’ladi:
-92 -40 -26 -12 -7 -4 0 6 9 25 34 38 46 52 56 67 71 78 86 125 93
Bu kamchilik juda oson yo’l bilan bartaraf etiladi. Buning uchun q := n + 1 operatoriga 1 ni qo’shish yetarli. (Sababini tushuntiring?)
195-misol. O’sish bo’yicha tartiblangan massivga tartib qoidasini buzmagan holda berilgan sonni kiriting. Oldingi masaladagi dasturni shunday o’zgartiringki, tartiblangan massivga boshqa sonli massiv qo’shilsin, yangi massiv esa tartiblangan bo’lsin.

Download 36.98 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8   9   ...   16




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling