MUHAMMAD AL-XORAZMIY NOMIDAGI TOSHKENT AXBOROT TEXNOLOGIYALARI
UNIVERSITETI
Amaliy ish
Bajardi: 512-22 guruh talabasi
Mirzarahimov Diyorbek
TOSHKENT 2023
2.1.1. array sinfidan foydalanib, amaliy dasturlar yaratish
Universitetlar haqida ma’lumotlar berilgan (class University). Ilmiy
salohiyati eng yuqori 3 ta universitetni qidirib topuvchi dastur tuzing.
#include
#include
#include
using namespace std;
struct University
{
string name;
double ranking;
};
bool compareByRanking(const University &a, const University &b)
{
return a.ranking > b.ranking;
}
int main()
{
int numUniversities;
cout << "Universitetlar sonini kiriting: ";
cin >> numUniversities;
vector universities(numUniversities);
for (int i = 0; i < numUniversities; i++)
{
cout << "Universitet #" << i + 1 << "ni nomini kiriting: ";
cin >> universities[i].name;
cout << "Universitet #" << i + 1 << "ning ilmiy salohiyatini kiriting (0-100): ";
cin >> universities[i].ranking;
}
sort(universities.begin(), universities.end(), compareByRanking);
cout << "Ilmiy salohiyati eng yuqori " << 3 << " ta universitetlar: " << endl;
for (int i = 0; i <= 2; i++)
{
cout << i + 1 << ". " << universities[i].name << endl;
}
return 0;
}
2.1.2. vector sinfidan foydalanib, amaliy dasturlar yaratish
Masiivni eng kichik elementidan keyingi elementlarini chiqaruvchi va
o‘chiruvchi funksiyalar yarating.
#include
#include
#include
using namespace std;
vector remove_second_smallest(vector vec)
{
if (vec.size() < 2)
{
return vec;
}
auto smallest = min_element(vec.begin(), vec.end());
auto second_smallest = *min_element(vec.begin(), vec.end(), [&](const int &a, const int &b)
{ return a != *smallest && (b == *smallest || a < b); });
vec.erase(remove(vec.begin(), vec.end(), second_smallest), vec.end());
return vec;
}
void print_second_smallest(vector vec)
{
if (vec.size() < 2)
{
cout << "Vektorda kamida ikki element bo'lishi kerak" << endl;
}
else
{
auto smallest = min_element(vec.begin(), vec.end());
auto second_smallest = *min_element(vec.begin(), vec.end(), [&](const int &a, const int &b)
{ return a != *smallest && (b == *smallest || a < b); });
cout << "Vektor ichidagi ikkinchi eng kichik element: " << second_smallest << endl;
}
}
int main()
{
vector vec = {3, 7, 12, 5, 9, 8, 4};
vec = remove_second_smallest(vec);
for (auto &x : vec)
{
cout << x << " ";
}
cout << endl;
print_second_smallest(vec);
return 0;
}
2.1.3. deque konteyneridan foydalanib, amaliy dasturlar yaratish
String turidagi To‘plam berilgan. Undagi elementlarni char turidagi A
massivga joylashtirib, so‘zlar, gaplar sonini aniqlovchi dastur tuzing.
#include
#include
using namespace std;
int main()
{
string s = "Assalomu alaykum dasturchilar.";
int count = 0;
char A[s.length()];
for (int i = 0; i < s.length(); i++)
{
if (s[i] != ' ')
{
A[count] = s[i];
count++;
}
}
cout << s << endl;
cout << endl;
cout << "Matndagi so'zlar, gaplar soni: " << count + 1 << endl;
cout << "A massivdagi elementlar: ";
for (int i = 0; i < count; i++)
{
cout << A[i] << " ";
}
cout << endl;
return 0;
}
2.1.4. forward_list konteyneridan foydalanib, amaliy dasturlar yaratish
X to‘plam berilgan. X to‘plam qiymatlarini kamayib borish tartibida joylang. Agar bir xil qiymatlilari bo‘lsa, ulardan bittasini qoldirib, qolganlarini o‘chiradigan dastur tuzing
#include
#include
using namespace std;
int main()
{
forward_list X = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
X.sort(greater());
X.unique();
for (int x : X)
{
cout << x << " ";
}
cout << endl;
return 0;
}
2.1.5. list konteyneridan foydalanib, amaliy dasturlar yaratish
list konteyneridan foydalanib X to‘plam berilgan. X to‘plam qiymatlarini kamayib borish tartibida joylang. Agar bir xil qiymatlilari bo‘lsa, ulardan bittasini qoldirib, qolganlarini o‘chiradigan dastur tuzing
#include
#include
using namespace std;
int main()
{
list X = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
X.sort(greater());
X.unique();
int maxCount = 2;
for (auto it = X.begin(); it != X.end(); ++it)
{
int count = 1;
auto nextIt = next(it);
while (nextIt != X.end() && *nextIt == *it)
{
++count;
++nextIt;
}
if (count > maxCount)
{
count = maxCount;
}
advance(it, count - 1);
X.erase(next(it), next(it, count));
}
for (int x : X)
{
cout << x << " ";
}
cout << endl;
return 0;
}
Do'stlaringiz bilan baham: |