Muhammad al-Xorazmiy nomidagi Toshkent Axborot Texnologiyalari Universiteti swd010-L2 guruh talabasi O’ktamov Ahmadjonning Dasturlash II danidan 5-laboratoriya ishi
Muhammad al-Xorazmiy nomidagi Toshkent Axborot Texnologiyalari Universiteti SWD010-L2 guruh talabasi O’ktamov Ahmadjonning Dasturlash II danidan 5-laboratoriya ishi
18-variant. a)Yangi konteynerlar hosil qiling, uni standart funksiyalar yordamida elementlar bilan to’lating merge funksiyasi yordamida amallar bajaring.
#include using namespace std;
int gener()
{
return rand() % 100 + 1;
}
int main()
{
srand(time(0));
array ar1;
generate(ar1.begin(), ar1.end(), gener);
cout << "\n1-array elementlari:\n ";
for (int i = 0; i < ar1.size(); i++)
{
cout << " " << ar1[i];
}
cout << endl;
array ar2;
generate(ar2.begin(), ar2.end(), gener);
cout << "\n2-array to'plamininng elementlari:\n ";
for (int i = 0; i < ar2.size(); i++)
{
cout << " " << ar2[i];
}
cout << endl;
array ar3;
merge(ar1.begin(), ar1.end(), ar2.begin(), ar2.end(), ar3.begin());
cout << "\nMerge fumksiyasi ishlatilib 2 array qo'shildi:\n ";
array::iterator it = ar3.begin();
while (it != ar3.end())
{
cout << " " << *it++;
}
cout << "\n\n";
system("pause");
return 0;
}
b) Iteratorlardan foydalani multimap konteyneri ustida amallar bajaring.
#include using namespace std;
int main()
{
multimap translete;
multimap::iterator iter;
int n;
cout << "Ingliz lug'atdagi so'zlarning sonini kiriting -> ";
cin >> n;
cout << n << " ta so'zni o'zi va tarjimasini yozing:\n";
string suz, tarjima;
for (int i = 0; i < n; i++)
{
cout << "\n Ingilizcha:-> ";
if (i == 0)
getline(cin, suz);
getline(cin, suz);
cout << " O'zbekcha:-> ";
getline(cin, tarjima);
translete.insert(make_pair(suz, tarjima));
}
int urin = 1;
cout << "\n Lug'at:\n";
for (iter = translete.begin(); iter != translete.end(); iter++)
{
cout << setw(3) << urin++ << " |. " << setw(4) << iter->first << " ----> " << setw(4) << iter->second << endl;
}
string search;
bool reverse;
cout << "\n Tilni tanlang:\n 1 - Ingliz\n 0 - O'zbek\n";
cin >> reverse;
if (reverse)
{
cout << "\n Tarjima qilinayotgan Ingilizcha so'zni kiriting -> ";
getline(cin, search);
getline(cin, search);
iter = translete.find(search);
cout << endl;
cout << setw(4) << iter->first << " ----> " << setw(4) << iter->second << endl;
}
else
{
cout << "\n Tarjima qilinayotgan O'zbekcha so'zni kiriting -> ";
getline(cin, search);
getline(cin, search);
iter = translete.begin();
cout << endl;
while (iter != translete.end())
{
if (search == iter->second)
{
cout << setw(4) << iter->second << " ----> " << setw(4) << iter->first << endl;
}
iter++;
}
}
cout << endl;
system("pause");
return 0;
}
c) malloc() funksiyasidan foydalanib xotirani dinamik taqsimlashni amalga oshiring .
#include using namespace std;
void show(int a[], int n)
{
int s = 1;
for (int i = 0; i < n; i++)
{
cout << s++ << ". " << a[i] << endl;
}
}
bool tub(int a)
{
for (int i = 2; i <= sqrt(a); i++)
{
if (a % i == 0)
return false;
}
return true;
}
bool mukammal(int n)
{
int sum = 0;
for (int i = 1; i < n; i++)
{
if (n % i == 0)
{
sum += i;
}
}
if (sum == n)
return true;
else
return false;
}
int main()
{
srand(time(0));
int *massiv;
int *top1;
int *top2;
int n;
cout << "\n \"malloc\" Xotiradan nechta dinamik joy ajratmoqchisiz: ";
cin >> n;
massiv = (int *)malloc(n * 4);
top1 = (int *)malloc(n * 4);
top2 = (int *)malloc(n * 4);
if (!massiv)
{
cout << "\n Xotira dinamik taqsimlanmadi !!!" << endl;
}
else
{
cout << "\n Xotiradan " << n << " ta dinamik joy ajratish muvofaqiyatli amalga oshirildi endi bu xotiradan massiv sifatida foydalansangiz bo'ladi:\n";
}
for (int i = 0; i < n; i++)
{
massiv[i] = rand() % 30 + 1;
}
cout << "\n Massivning dastlabki elementlari:\n";
show(massiv, n);
int s = 1;
int t = 0, m = 0;
for (int i = 0; i < n; i++)
{
if (tub(massiv[i]))
{
top1[t++] = massiv[i];
}
if (mukammal(massiv[i]))
{
top2[m++] = massiv[i];
}
}