For sikli yordamida amalga oshirish mumkin. Elementni massiv boshiga (birinchi o’ringa) ko’chirish uchun prosedura
Download 36.98 Kb.
|
masala 1
Prosedura
Procedure swp(k, m : integer); label 1; var v : integer; begin if k = m then goto 1; v := a[k]; a[k] := a[k + 1]; a[k + 1] := v; swp(k + 1, m); 1: end; U ancha oson tuzilgan. Bu yerda qo’shni elementlarni elementar nomerlarining oshish yo’nalishiga qarab ko’chirish, ya’ni o’ng chegaraga ko’chish asos qilib olingan. Bu sizga anchadan beri ma’lum uchta operatorlar yordamida amalga oshadi: v := a[k]; a[k] := a[k + 1]; a[k + 1] := v; Program L221; uses Crt; const n = 20; type t = array[1..n] of integer; var a : t; i, d : integer; {---------------------------------------------------------------------------------------} Procedure fast(q, p : integer; var a : t); var s, l, r : integer; begin l := q; r := p; s := a[l]; repeat while (a[r] >= s) and (l < r) do r := r - 1; a[l] := a[r]; while (a[l] <= s) and (l < r) do l := l + 1; a[r] := a[l] until l = r; a[l] := s; if q < l - 1 then fast(q, l - 1, a); if l + 1 < p then fast(l + 1, p, a) end; {---------------------------------------------------------------------------------------} Procedure swp(k, m : integer); label 1; var v : integer; begin if k = m then goto 1; v := a[k]; a[k] := a[k + 1]; a[k + 1] := v; swp(k + 1, m); 1: end; {---------------------------------------------------------------------------------------} begin for i := 1 to n do begin write(i, ‘chi elementni kiriting’); readln(a[i]) end; fast(1, n, a); d := 1; repeat swp(1, n - d + 1); d := d + 1 until a[d] > 0; swp(1, n - d + 1); swp(1, n - d); writeln('Oxiri nollar bilan to’ldirilgan tartiblangan massiv'); for i := 1 to n do write(a[i],' '); writeln end. 222-misol. Tasodifiy sonlar funksiyasi yordamida ikkita sonli massivni tuzuvchi, ularni tezkor tartiblash rekursiv prosedurasi yordamida tartibga soluvchi, so’ngra ularni rekursiv prosedurani yana qo’llagan holda tartiblangan alohida massivga birlashtiruvchi dastur tuzing. Tasodifiy sonlar funksiyasi yordamida massiv tuzishni va ularni tartiblashni biz oldindan bilamiz, shuning uchun bu savollarga to’xtalib o’tmaymiz-da, bizda oldindan ikkita kamaymaslik bo’yicha tartiblangan massiv bor deb faraz qilamiz: Misol uchun 10 ta elementdan tashkil topgan a va 15 ta elementdan tashkil topgan b massivlar berilgan bo’lsin. a massiv: -98 -97 -77 -47 -44 -24 11 59 75 83 b massiv: -96 -75 -69 -69 -68 -15 -5 11 37 45 47 77 85 92 93 Massiv elementlarini ketma-ket solishtirish, eng kichigini tanlab uning qiymatini yangi massiv elementiga berib qo’yish kerak degan tabiiy fikr tug’iladi. Bu g’oya amalda qanday qilib ro’yobga chiqishini ko’raylik: massivning birinchi elementi a, a[1]=-98; b massivning birinchi elemnti, b[1] = -96; a[1] < b[1] (-98 < -96), demak yangi massivdagi birinchi elementga a massivning birinchi elementi qiymatini berib qo’yamiz: c[1] := a[1], c[1] := -98. Endi a massivning ikkinchi elementini olamiz, a[2]=-97 va uni b massivning birinchi elemeti bilan solishtiramiz: a[2] < b[1], (-97 < -96), demak yangi massivning ikkinchi elementiga a massivning ikkinchi elementi qiymatini berib qo’yishimiz kerak: c[2] := a[2], c[2] := -97. Download 36.98 Kb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling