Zbekiston respublikasi raqamli texnologiyalar vazirligi toshkent axborot texnologiyalari universiteti


Download 255.9 Kb.
Sana17.06.2023
Hajmi255.9 Kb.
#1552794
Bog'liq
aynura abilkhasinova


O’ZBEKISTON RESPUBLIKASI RAQAMLI TEXNOLOGIYALAR VAZIRLIGI
TOSHKENT AXBOROT TEXNOLOGIYALARI UNIVERSITETI
NUKUS FILIALI

«Telekommunikatsiyatexnologiyalarivakasbiyta’lim »fakulteti
« Kasbiyta`lim »yo'nalishi
102-22 guruhtalabasi Abilkasinova Aynuraning
Dasturlash 2 fanidan

2 Amaliy ishi


Mavzu : Vectorlar sinifidan foydalanib, amaliy dasturlar yaratich


Bajargan: O.Kengesbaeva
Qabul qilgan: M.Artikbayev


NUKUS 2023
2 Amaliy ish


Ishning maqsadi : C++ tilida dasturlash tilida vector siniflaridan foydalanib , konteynerlardan foydalanish ko’nikmalarini shakillantirish.


Masalaning qo’yilishi :
Berilgan N ta ixtiyoriy sonni max va min lari o’rtasidagi sonlarni o’chiruvchi funksiyalar yarating.
Masalaning g’oyasi :
Bu dasturda N ta ixtiyori sonni kiritamiz , max va min lar orasidagi sonlarni o’chiramiz.


Dastur matini:

#include


#include
#include

using namespace std;


namespace Vectorlar {


vector remove_min_max_indices(vector& A) {


vector removed_indices;
if (A.empty()) {
cout << "Vector bo'sh" << endl;
return removed_indices;
}
auto min_it = min_element(A.begin(), A.end());
auto max_it = max_element(A.begin(), A.end());
int min_index = min_it - A.begin();
int max_index = max_it - A.begin();
int start_index = min_index < max_index ? min_index : max_index;
int end_index = max_index > min_index ? max_index : min_index;
for (int i = end_index - 1; i > start_index; i--) {
removed_indices.push_back(A[i]);
A.erase(A.begin() + i);
}
return removed_indices;
}
}

int main() {


cout << "Berilgan int tipidagi vector elementlari orasidagi eng katta va eng kichik elementlarni o'chirish \n n = ";
int n;
cin >> n;
vector A;
for (int i = 0; i < n; i++) {
int m;
cout << i + 1 << "-element: ";
cin >> m;
A.push_back(m);
}
cout << "Vector elementlari: ";
for (int k : A) {
cout << k << " ";
}
cout << endl;
vector removed_indices = Vectorlar::remove_min_max_indices(A);
cout << "Eng katta va eng kichik elementlar o'chirildi. O'chirilgan elementlar: ";
for (int k : removed_indices) {
cout << k << " ";
}
cout << endl;
cout << "Yangi vector elementlari: ";
for (int k : A) {
cout << k << " ";
}
cout << endl;
return 0;
}


Dastur natijasi :

Dastur kodi:


#include
#include
#include
#include
using namespace std;
// Vektordagimaksimalelementdankamroqbarchaelementlarniolibtashlashfunksiyasi
void remove_elements_less_than_max(vector& v) {
if (v.empty()) {
return;
}
auto max_it = max_element(v.begin(), v.end());
v.erase(v.begin(), find_if_not(v.begin(), v.end(), [max_val=*max_it](int x) { return x }
// Fayldanbutunsonlarnio'qishvaularnivektordasaqlashfunksiyasi
void read_from_file(vector& v, const string& filename) {
ifstream fin(filename);
if (!fin) {
cerr<< "Xato: kirishfayliniochibbo'lmadi " << filename <exit(1);
}
int x;
while (fin >> x) {
v.push_back(x);
}
fin.close();
}
// Vektordanfaylgabutunsonlarniyozishfunksiyasi
void write_to_file(const vector& v, const string& filename) {
ofstreamfout(filename);
if (!fout) {
cerr<< "Xato: chiqishfayliniochibbo'lmadi " << filename <exit(1);
}
for (int x : v) {
fout<< x <}
fout.close();
}
int main() {
vector v;
read_from_file(v, "input.txt");
cout<<"Bu yerda input fildedagimalumotlarnio'qiydivauniustidaamallarbajarib output filegajoylaydi\n";
remove_elements_less_than_max(v);
write_to_file(v, "output.txt");
return 0;
}



Download 255.9 Kb.

Do'stlaringiz bilan baham:




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