4-amaliy mashg’ulot Dusmatov Sardor


Download 21.58 Kb.
Sana03.06.2024
Hajmi21.58 Kb.
#1841728
Bog'liq
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 &royhat) {


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 royhat(N);


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 elements;

public:
// Ro'yhatga element qo'shish metod


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'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling