5-amaliy ish Bajardi: Erkinov Oybek Tekshirdi: Shokirov Shodmon Toshkent 2023


Download 137.86 Kb.
Sana16.06.2023
Hajmi137.86 Kb.
#1516763
Bog'liq
Dasturlash 2 5- mustaqil ish


O‘ZBEKISTON RESPUBLIKASI AXBOROT
TEXNOLOGIYALARI VA
KOMMUNIKATSIYALARINI RIVOJLANTIRISH VAZIRLIGI
MUHAMMAD AL-XORAZMIY NOMIDAGI
TOSHKENT AXBOROT TEXNOLOGIYALARI UNIVERSITETI

Dasturlash 2 fanidan
5-amaliy ish





Bajardi: Erkinov Oybek


Tekshirdi: Shokirov Shodmon


Toshkent 2023.



  1. LABORATORIYA ISHI


Mavzu: Standart algoritmlar va itеratorlar. Standart algoritmlar. Itеratorlar va ularning qo`llanilishi. Xotirani taqsimlovchilar va ularga qo`yilgan talablar. Standart bo`yicha taqsimlovchi.

Variant raqami - 7.

a) Yangi konteynerlar hosil qiling, uni standart funksiyalar yordamida


elementlar bilan to’lating move funksiyasi yordamida amallar bajaring;

Dastur Kodi:

#include <iostream>
#include <deque>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
using namespace std;
int main()
{
srand(time(NULL));
deque<int> dek;
deque<int> dek2;
int n;
cout << "\n Deque konteynorining o'lchamini kiriting: ";
cin >> n;
int number;
int j = 0;
while (j < n)
{
number = rand() % 40 + 1;
dek.push_front(number);
dek2.push_back(j);
j++;
}
j = 0;
cout << "\n Birinchi to'plamning elementlari:\n";
for (auto &&it : dek)
{
cout << setw(3) << j++ << ". " << setw(4) << it << endl;
}
j = 0;
cout << "\n Ikkinchi to'plamning elementlari:\n";
for (auto &&it : dek2)
{
cout << setw(3) << j++ << ". " << setw(4) << it << endl;
}
int k, t;
cout << "\n Birinchi toplamni nechanchi pozitsatsiyasidan 2 chi to'plamga ko'chiraylik: ";
cin >> k;
cout << "\n 2 chi to'plamni nechanchi pozitsiyasidan boshlab qoshmoqchisiz: ";
cin >> t;
move(dek.begin() + k, dek.begin() + dek2.size(), dek2.begin() + t);
j = 0;
cout << "\n 2 chi To'plamning yangi elementlari:\n\n";
for (auto &&it : dek2)
{
cout << setw(3) << j++ << ". " << setw(4) << it << endl;
}

cout << endl;
system("pause");
return 0;
}

Dastur Natijasi:

b) Iteratorlardan foydalanib vector konteyneri ustida amallar bajaring;



Dastur Kodi:


#include <iostream>
#include <vector>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
#include <cmath>
using namespace std;
bool isTub(int d)
{
bool tub_ekan = true;
for (int i = 2; i <= sqrt(d); i++)
{
if (d % i == 0)
{
tub_ekan = false;
break;
}
}
return tub_ekan;
}
int main()
{
srand(time(NULL));
vector<int> vec;
vector<int>::iterator iter_vec1;
int n;
cout << "\n Vectorning elementlari sonini kiriting: ";
cin >> n;
int number;
for (int i = 0; i < n; i++)
{
number = rand() % 90 + 10;
vec.push_back(number);
}
iter_vec1 = vec.begin();
int urin = 0;
cout << "\n Vectorning " << n << " ta elementi 10 dan 99 gacha oraliqdagi sonlar bilan to'ldirildi:\n";
while (iter_vec1 != vec.end())
{
cout << setw(3) << ++urin << ". " << setw(4) << *iter_vec1++ << endl;
}
iter_vec1 = vec.begin();
while (iter_vec1 != vec.end())
{
if (!isTub(*iter_vec1))
{
*iter_vec1 = 0;
}
iter_vec1++;
}
cout << "\n Vectorning elementlari orasidan tub bo'lmaganlarining qiymatlari 0 ga o'zgartirildi:\n";
iter_vec1 = vec.begin();
urin = 1;
while (iter_vec1 != vec.end())
{
cout << setw(3) << urin++ << ". " << *iter_vec1++ << endl;
}
cout << endl;
system("pause");
return 0;
}


Dastur Natijasi:

c) free () funksiyasidan foydalanib xotirani dinamik taqsimlashni amalga
oshiring .

Dastur Kodi:


#include <iostream>
#include <malloc.h>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
using namespace std;
int main()
{
srand(time(NULL));
int *cl;
int xotira;
cout << "\n Xotiradan nechta dinamik joy ajratmoqchisiz: ";
cin >> xotira;
cl = (int *)calloc(xotira, sizeof(int));
if (cl == NULL)
{
cout << "\n Xotira dinamik taqsimlanmadi !!!" << endl;
exit(0);
}
else
{
cout << "\n Xotiradan " << xotira << " ta joy ajratish amalga oshdi\n " << endl;
cout << "\n Dastlabki elementlar (0 bilan to'ldirilgan xolda chunki joy ajratish calloc orqali amalga oshirilgan):\n " << endl;
for (int i = 0; i < xotira; i++)
{
cout << setw(3) << i + 1 << ". " << setw(4) << *(cl + i) << endl;
}
cout << "\n Xotira randomda 1 dan 100 gacha oraliqdagi sonlar bilan to'ldirildi:\n " << endl;
for (int i = 0; i < xotira; i++)
{
cl[i] = rand() % 100 + 1;
cout << setw(3) << i + 1 << ". " << setw(4) << cl[i] << endl;
}
free(cl);
cout << "\n Xotirani \"free\" funksiyasi orqali tag tugi bilan tozaladik:\n " << endl;
for (int i = 0; i < xotira; i++)
{
cout << setw(3) << i + 1 << ". " << setw(4) << *(cl + i) << endl;
}
cout << "\n Xotirani yana randomda 1 dan 100 gacha oraliqdagi sonlar bilan to'ldiridik:\n " << endl;
for (int i = 0; i < xotira; i++)
{
cl[i] = rand() % 100 + 1;
cout << setw(3) << i + 1 << ". " << setw(4) << cl[i] << endl;
}
for (int i = 0; i < xotira - 1; i++)
{
for (int j = i + 1; j < xotira; j++)
{
if (cl[i] > cl[j])
{
int temp = cl[i];
cl[i] = cl[j];
cl[j] = temp;
}
}
}
cout << "\n Elementlar tartiblandi:\n " << endl;
for (int i = 0; i < xotira; i++)
{
cout << setw(3) << i + 1 << ". " << setw(4) << *(cl + i) << endl;
}
free(cl);
}
exit(0);
cout << endl;
system("pause");
return 0;
}

Dastur Natijasi:


Download 137.86 Kb.

Do'stlaringiz bilan baham:




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