Massiv bu nima?


Download 268.37 Kb.
bet3/3
Sana24.01.2023
Hajmi268.37 Kb.
#1115572
1   2   3
Bog'liq
Massivlar — копия

# bajarildi A[2]
# bajarildi A[3]
# bajarildi A[4]
1) agar N (1000, 1000000) dan katta bo’lsa?
2) N ni o’zgartirganda dastur o’zgarmasligi kerak!
?

Massiv elementlarini qanday bajarish mumkin?


O’zgaruchi bilan:
i = 0
# bajarildi A[i]
i += 1
# bajarildai A[i]
i += 1
# bajarildai A[i]
i += 1
# bajarildai A[i]
i += 1
# bajarildai A[i]
i += 1
Sikl bilan:
i = 0
while i < N:
# bajarildai A[i]
i += 1
O’zgaruvchili sikl:
for i in range(N):
# bajarildai A[i]

Elementlarning saralash


Umumiy sxema (o’zgartirish mumkin A[i]):
for i in range(N):
... # nimadir qilish A[i]
O’zgartirmaslik ucun nima qilish kerak? A[i]:
for x in A:
... # nimadir qilish x
for i in range(N):
A[i] += 1
x = A[0], A[1], ..., A[N-1]
for x in A:
print ( x )

Dastur nima ish qiladi?


A = [2, 3, 1, 4, 6, 5]
print( A[3] )
print( A[0]+2*A[5] )
A[1] = A[0] + A[5]
print( 3*A[1]+A[4] )
# 4
# 12
# 7
# 27
A[2] = A[1]*A[4]
print( 2*A[1]+A[2] )
# 18
# 24
for k in range(6):
A[k] += 2
print( 2*A[3]+3*A[4] )
# 36
# [4,5,3,6,8,7]

Keraksiz elementlarni hisoblash


Vazifa. Massivda basketbolistlarni o’sishi yozilgan. Bulardan qanchasi ko’p o’sgan : 180 см, lekin kami 190 см?
count = 0
for x in A:
if 180 < x and x < 190:
count += 1
Qanday echilishi kerak?
?

Elementlarning saralash


summa = 0
for x in A:
if x % 2 == 0:
summa += x
print ( summa )
B = [x for x in A
if x % 2 == 0]
print ( sum(B) )
yoki:
Vazifa. Massivdagi juft sonlar elementini toping?
Element juft ekanligini qanda topish kerak?
?
B massiv yig’indisi

Sikl qanday ishlaydi?


summa = 0
for x in A:
if x % 2 == 0:
summa += x

4

3

6

8

1

A
x

summa

4
3
6
8
1
0
4
10
18

O’rtach arifmetik qiymatlar


count = 0
summa = 0
for x in A:
if x % 10 == 5:
count += 1
summa += x
print ( summa/count )
O’rtacha arifmetik qiymat
Yoki bunday:
B = [ x for x in A ]
if x % 10 == 5]
print ( sum(B)/len(B) )
Kerakligini tanlab olamiz
5 ga tugashini qanday topsa bo’ladi?
?
Vazifa. Massiv elementlari orasidagi 5 ga tugaydigan o’rtacha arifmetik qiymatini toping?

Massivdan izlash


X ga teng qiymatni izlash:
i = 0
while A[i] != X:
i += 1
print ( "A[", i, "]=", X, sep = "" )
nimasi yaxshi emas?
?
i = 0
while i < N and A[i] != X:
i += 1
if i < N:
print ( "A[", i, "]=", X, sep = "" )
else:
print ( “Topilmadi!" )
agar bundayi bo’lmasa?
?
i < N

Massivdan izlash


nX = -1
for i in range ( N ):
if A[i] == X:
nX = i
break
if nX >= 0:
print ( "A[", nX, "]=", X, sep = "" )
else:
print ( “Topilmadi!" )
Favqulotda chiqish:
break
Favqulotda sikldan chiqish
Topilgan element nomi

Massivdan izlash


for i in range ( N ):
if A[i] == X:
print ( "A[", i, "]=", X, sep = "" )
break
else:
print ( “Topilmadi!" )
Python stili:
Agar sikldan favqulotda chiqilmasa?
if X in A:
nX = A.index(X)
print ( "A[", nX, "]=", X, sep = "" )
else:
print ( “Topilmadi!" )

Maksimal elementlar


Vazifa: Massivdagi maksimal elementni topish..
Algoritm:
Yechilishi:
  • Birinch element maksimum
  • Qolgan massiv elementlarini ko’rib chiqish: agar navbatdagi element > M, unda A[i] da M
  • qiymatni chqarish M

Maksimal element


M = A[0]
for i in range(1,N):
if A[i] > M:
M = A[i]
print ( M )
M = A[0]
for x in A:
if x > M:
M = x
uni raqamini qanday topish mumkin?
?
Pythonda:
M = max ( A )
agar range(N)?
?

Maksimal elment va uning raqami


M = A[0]; nMax = 0
for i in range(1,N):
if A[i] > M:
M = A[i]
nMax = i
print ( "A[", nMax, "]=", M, sep = "" )
nMax = 0
nMax = i
nima qilmoq kerak?
?
element raqami bo’yicha uni qiymatini toping!
!
nMax = 0
for i in range(1,N):
if A[i] > A[nMax]:
nMax = i
print ( "A[", nMax, "]=", A[nMax], sep = "" )
A[nMax]
A[nMax]
Download 268.37 Kb.

Do'stlaringiz bilan baham:
1   2   3




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