2-amaliy topshiriq!


printf("Bunday element topilmadi\n"); else


Download 1.14 Mb.
bet7/16
Sana21.11.2023
Hajmi1.14 Mb.
#1790449
1   2   3   4   5   6   7   8   9   10   ...   16
Bog'liq
2-amaliy topshiriq!

printf("Bunday element topilmadi\n"); else
printf("%d indeksli element 3 ga teng\n", point);
}
getchar(); getchar(); return 0; }
Masala 3. 50 elemendan iborat massiv berilgan. Massiv elementlari tasodifiy sonlar bilan to’ldirilgan. Foydalanuvchi kiritgan qiymat massivdan qidiriladi.
#include #include
#include using namespace std;
//funksiya prototiplari
int linSearch(int arr[], int requiredKey, int size); // chiziqli qidirish void showArr(int arr[], int size); // massivni ko'rsatish
int main()
{
const int arrSize = 50; int arr[arrSize]; int requiredKey = 0; // kalit int nElement = 0; // massiv elementi nomeri srand(time(NULL));
//tasodifiy sonlar bilan massivni to'ldirish for (int i = 0; i < arrSize; i++)
{
arr[i] = 1 + rand() % 50;
}
showArr(arr, arrSize);
cout << "Qanday sonni qidirish kerak? "; cin >> requiredKey; // qidiriluvchi sonni kiritish
//qidirish
nElement = linSearch(arr, requiredKey, arrSize);
if (nElement != -1)
{
//agar element topilgan holatda
cout << requiredKey<<" qiymati " << nElement <<" nomerli indeksda joylashgan" << endl;
}
else
{
cout << "Massivda bunday element yo'q'" << endl;
}
return 0;
}
void showArr(int arr[], int arrSize)
{
for (int i = 0; i < arrSize; i++)
{
cout << setw(4) << arr[i];
if ((i + 1) % 10 == 0)
{
cout << endl;
}
}
cout << endl << endl;
}
int linSearch(int arr[], int requiredKey, int arrSize)
{
for (int i = 0; i < arrSize; i++)
{
if (arr[i] == requiredKey)
return i;
}
return -1;
}
Interpolyatsion qidirish.
Interpolyatsiya bu qidirish sohasini belgilash bo’lib, unda qidirilayotgan element va barcha sohalar orasidagi masofani hisoblash orqali element qidiriladi.
Bu qidirish usulida matematik hisoblashlar orqali massivdan element qidiriladi. Formulasi quyida keltirilgan:
X = a + ((key – arr[a]) * (b – a)) / (arr[b] – arr[a]);
Agar arr[x] < key bo’lsa u holda a = x + 1; Agar arr[x] > key bo’lsa u holda b = x -1; aks holda found = true ya’ni element topildi
Masala 4. Massivdan elementni interpolyatsion qidirish usulida qidirish.
#include using namespace std;
int main()
{
//Qidirish massivi
int MyArray[] { 1, 2, 4, 6, 7, 89, 123, 231, 1000, 1235 };
int x = 0; //Qidirilayotgan element solishtiriladigan joriy indeks int a = 0; //Qidirish sohasi chap chegarasi int b = 9; //Qidirish sohasi o'ng chegarasi
int WhatFind = 123; //Qidirilayotgan element
bool found; //Flag, agar element topilsa qiymati True ga teng
/************ Intepolyatsiyani boshlash *******************************/
//Massivda element topilgunicha sikl davom etadi
//yoki qidirish sohasi mavjud holatda
for (found = false; (MyArray[a] < WhatFind) && (MyArray[b] > WhatFind) &&
!found; )
{
//Interpolyatsiyani hisoblash
x = a + ((WhatFind - MyArray[a]) * (b - a)) / (MyArray[b] - MyArray[a]);
//agar element topilmasa yangi soha chegaralari belgilanadi if (MyArray[x] < WhatFind) a = x + 1;
else if (MyArray[x] > WhatFind) b = x - 1; else found = true;
}
/************** Interpolyatsiya tugatish ***************************/
//Natijani chiqarish if (MyArray[a] == WhatFind)

Download 1.14 Mb.

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




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