Qidiruv usullarini tadqiq qilish


Download 205.71 Kb.
Sana28.07.2023
Hajmi205.71 Kb.
#1663245
Bog'liq
Ma‘lumotlar tuzilmasi va algoritmlar Laboraoriya


O‘zbekiston Respublikasi Raqamli texnologiyalari vazirligi
Muhammad al-Xorazmiy nomidagi Toshkent axborot texnologiyalari universiteti



MAVZU: QIDIRUV USULLARINI TADQIQ QILISH. MA’LUMOTLARNI SARALASH USULLARINI TADQIQ QILISH


2-3-Amaliy topshiriq


Fan: Ma‘lumotlar tuzilmasi va algoritmlar
Bajardi: Suyunov Firdavs
Tekshirdi: Abduvaliyeva Zebiniso


Toshkent 2023-yil
2-tajriba ishi. QIDIRUV USULLARINI TADQIQ QILISH
Variant-21
21.Berilgan ro‘yhatda qidirilayotgan element transpozitsiya usuli bilan qancha murojaatda ro‘yhat boshiga kelishini aniqlash dasturini tuzing.

Dastur kodi:
#include
#include
using namespace std;
pair findElementIndex(const vector>& matrix, int element) {
// Check if the given matrix is empty
if (matrix.empty() || matrix[0].empty()) {
return make_pair(-1, -1);
}

int rows = matrix.size();
int cols = matrix[0].size();

// Iterate through the matrix to find the index of the element
for (int col = 0; col < cols; ++col) {
for (int row = 0; row < rows; ++row) {
if (matrix[row][col] == element) {
return make_pair(row, col);
}
}
}

return make_pair(-1, -1); // Element not found
}

int main() {
// Test the function with a sample matrix
vector> sampleMatrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

int elementToFind = 5;
pair result = findElementIndex(sampleMatrix, elementToFind);

if (result.first != -1 && result.second != -1) {
cout << "The element " << elementToFind << " is found at row " << result.first << " and column " << result.second << ".\n";
} else {
cout << "The element " << elementToFind << " is not found in the matrix.\n";
}

return 0;
}
Dastur natijasi:




3-tajriba ishi. MA’LUMOTLARNI SARALASH USULLARINI TADQIQ QILISH
Variant-21
21.A massivning eng katta (eng kichik) elementini ekranga chiqarish dasturini tuzing.

Dastur kodi:
#include
using namespace std;

void bubbleSort(int a[], int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (a[j] > a[j+1]) {
swap(a[j], a[j+1]);
}
}
}
}

int findLargest(int a[], int n) {
bubbleSort(a, n);
return a[n-1];
}

int findSmallest(int a[], int n) {
bubbleSort(a, n);
return a[0];
}

int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
cout << "Largest element: " << findLargest(arr, n) << endl;
cout << "Smallest element: " << findSmallest(arr, n) << endl;
return 0;
}


Dastur natijasi:

Download 205.71 Kb.

Do'stlaringiz bilan baham:




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