4-amaliy mashg’ulot Dusmatov Sardor
Download 21.58 Kb.
|
Sardor 4
4-amaliy mashg’ulot Dusmatov Sardor 4-variant Ro‘yhat yarating, Ro‘yhatga N ta haqiqiy son kiriting. Ro‘yhatning minimum elementini o’chirich dasturini tuzing. #include #include using namespace std; void ochir(vector if (royhat.empty()) { cout << "Royhat bosh!\n"; return; } int engkichik = royhat[0]; int indeks = 0; for (int i = 1; i < royhat.size(); ++i) { if (royhat[i] < engkichik) { engkichik = royhat[i]; indeks = i; } } royhat.erase(royhat.begin() + indeks); cout << "Eng kichik element ochirildi!\n"; } int main() { int N; cout << "Royhat hajmini kiriting: "; cin >> N; vector cout << "Royhat elementlarini kiriting:\n"; for (int i = 0; i < N; ++i) { cin >> royhat[i]; } ochir(royhat); cout << "Yangilangan royhat:\n"; for (int i = 0; i < royhat.size(); ++i) { cout << royhat[i] << " "; } cout << endl; return 0; } Class bilan: #include #include class Royhat { private: std::vector public:
void elementQosh(int element) { elements.push_back(element); } // Ro'yhatni chiqarish metod void royhatChiqarish() { std::cout << "Ro'yhat: "; for (int element : elements) { std::cout << element << " "; } std::cout << std::endl; } // Minimum elementni o'chirish metod void minElementOchirish() { if (elements.empty()) { std::cout << "Ro'yhat bo'sh!" << std::endl; return; } int minIndex = 0; int minElement = elements[0]; for (unsigned int i = 1; i < elements.size(); ++i) { if (elements[i] < minElement) { minElement = elements[i]; minIndex = i; } } elements.erase(elements.begin() + minIndex); std::cout << "Minimum element (" << minElement << ") o'chirildi." << std::endl; } }; int main() { Royhat list; // Ro'yhatga element qo'shish list.elementQosh(5); list.elementQosh(3); list.elementQosh(8); list.elementQosh(1); list.elementQosh(6); // Ro'yhatni chiqarish list.royhatChiqarish(); // Minimum elementni o'chirish va yangi ro'yhatni chiqarish list.minElementOchirish(); list.royhatChiqarish(); return 0; } Download 21.58 Kb. Do'stlaringiz bilan baham: |
ma'muriyatiga murojaat qiling