# Labaratoriya ish C++

```O’zbekiston Respublikasi Axborot Texnologiyalari
va Kommunikatsiyalarini Rivojlantirish Vazirligi
Toshkent Axborot Texnologiyalari Universiteti
LABARATORIYA
ISHI
Guruh: 031-19
Bajardi: Umarov.Sh
Tekshirdi: Muminov. S
Labaratoriya ish
1. nxn matritasning pastki o’ng burchagidagi elementlaridan vector
hosil qiling.
Yechim:
int main() {
int matrix[3][3] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
int row = 3, col = 3;
int i, j;
for (i = 0; i &lt; row; i++)
{
for (j = 0; j &lt; col; j++)
{
if (i+j==3||i+j==4)
{
cout &lt;&lt; &quot;0&quot; &lt;&lt; &quot; &quot;;
}
else
cout &lt;&lt; matrix[i][j] &lt;&lt; &quot; &quot;;
}
cout &lt;&lt; endl;
}
return 0;
}
2. Navbat eng katta elementi topilsin undan keyin 0 joylashtrilsin.
Dastur kodi:
#include &lt;iostream&gt;
#include &lt;queue&gt;
using namespace std;
int main()
{
queue &lt;int&gt; qu;
queue &lt;int&gt; qu2;
int n;
cout&lt;&lt;&quot;Navbat elementlari sonini kiriting:&quot;;
cin &gt;&gt; n;
cout&lt;&lt;&quot;Navbat elementlarini kiriting:&quot;;
for(int i=0, a; i&lt;n; i++)
{
cin &gt;&gt; a;
qu.push(a);
qu2.push(a);
}
const int&amp; k = qu2.front();
int min = k;
for(int i=0; i&lt;n; i++)
{
if(qu2.front() &lt; min) min = qu2.front();
qu2.pop();
}
cout &lt;&lt; &quot;min=&quot; &lt;&lt; min &lt;&lt; endl;
for(int i=0; i&lt;n; i++)
{
if(qu.front() == min)
{
qu2.push(qu.front());
qu2.push(0);
qu.pop();
continue;
}
qu2.push(qu.front());
qu.pop();
}
n = qu2.size();
for(int i=0; i&lt;n; i++)
{
cout &lt;&lt; qu2.front() &lt;&lt; &quot; &quot;; qu2.pop();
}
}
3. Ro’yhat n-inchi elementidan keyin yangi element qo’yilsin.
Dastur kodi
#include &lt;iostream&gt;
using namespace std;
class Node{
public: int number;
Node* next;
};
int main()
{
Node* lastPtr = NULL;
short action = -1;
while (1)
{
cout&lt;&lt;&quot;1. element qo‘shish\n&quot;;
cout&lt;&lt;&quot;2. ro‘yhatni ko‘rish\n&quot;;
cout&lt;&lt;&quot;3. ro‘yhat maksimalini topish\n&quot;;
cout&lt;&lt;&quot;0. chiqish\n\n&quot;;
cout&lt;&lt;&quot;tanlang: &quot;;
cin&gt;&gt;action;
if (action == 0) {
system(&quot;CLS&quot;);
break;}
if (action == 1)
{
system(&quot;CLS&quot;);
Node* ptr = new Node;
int numb = -1;
cout&lt;&lt;&quot;son kiriting: &quot;;
cin&gt;&gt;numb;
ptr-&gt;number = numb;
ptr-&gt;next = NULL;
{
lastPtr = ptr;
system(&quot;CLS&quot;);
continue;
}
lastPtr-&gt;next = ptr;
lastPtr = ptr;
system(&quot;CLS&quot;);
continue;
}
if (action == 2){
Node* ptr = NULL;
system(&quot;CLS&quot;);
{
cout&lt;&lt;&quot;\t!!! ro‘yhat bo‘sh !!!\n\n&quot;;
system(&quot;PAUSE&quot;);
system(&quot;CLS&quot;);
continue;
}
cout&lt;&lt;&quot;* * * * * ro‘yhat * * * * *\n\n&quot;;
while (1) {
cout&lt;&lt;ptr-&gt;number&lt;&lt;&quot; &quot;;
if (ptr-&gt;next == 0) break;
ptr = ptr-&gt;next;
}
cout&lt;&lt;&quot;\n\n&quot;;
system(&quot;PAUSE&quot;);
system(&quot;CLS&quot;);
continue;
}
if (action == 3)
{
system(&quot;CLS&quot;);
Node* q = new Node;
Node* last = new Node;
while(p){
if(max&lt;p-&gt;number){ max=p-&gt;number;}
p=p-&gt;next;
}
system(&quot;CLS&quot;);
cout&lt;&lt;&quot;max=&quot;&lt;&lt;max;
system(&quot;pause&quot;);
continue;
}
}}
4. nxn matritasning pastki o’ng burchagidagi elementlaridan vector
hosil qiling.
Yechim:
int main() {
int matrix[3][3] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
int row = 3, col = 3;
int i, j;
for (i = 0; i &lt; row; i++)
{
for (j = 0; j &lt; col; j++)
{
if (i+j==3||i+j==4)
{
cout &lt;&lt; &quot;0&quot; &lt;&lt; &quot; &quot;;
}
else
cout &lt;&lt; matrix[i][j] &lt;&lt; &quot; &quot;;
}
cout &lt;&lt; endl;
}
return 0;
}
5. Mashina raqamlari ro’yhati berilgan 345, 368, 876, 945, 564,
387, 230, Binar qidiruvdan foydalanib berilgan raqamli mashina
qaysi joyda turganini toping.
Yechim:
#include &lt;iostream&gt;
using namespace std;
int binarySearch(int arr[], int left, int right, int x) {
while (left &lt;= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == x) {
return mid;
} else if (arr[mid] &lt; x) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
int main() {
int myarr[10];
int num;
int output;
myarr = [345, 368, 876, 945, 564, 387, 230]
// cout &lt;&lt; &quot;Please enter 10 elements ASCENDING order&quot; &lt;&lt;
endl;
// for (int i = 0; i &lt; 10; i++) {
// cin &gt;&gt; myarr[i];
// }
cout &lt;&lt; &quot;Please enter an element to search&quot; &lt;&lt; endl;
cin &gt;&gt; num;
output = binarySearch(myarr, 0, 9, num);
if (output == -1) {
cout &lt;&lt; &quot;Bunday no'merdagi avtomobil topilmadi&quot; &lt;&lt; endl;
} else {
cout &lt;&lt; &quot;Avtomobil joy no'meri: &quot; &lt;&lt; output &lt;&lt; endl;
}
return 0;
}
6. Daraxt tugunlari haqiqiy sonlar bo’lsin, Yozuv berilgan kalit
qiymatidan katta bo’lgan daraxt tugunlarini o’chiruvchi dastur
tuzing.
Algoritm
1. Ekranga menyu chiqaramiz: 1 - element qo‘shish; 2 ro‘yhatni ko‘rish;
3 - ro‘yhat maksimalini topish; 0 chiqish; tanlash uchun tanla o‘zgaruvchisiga qiymat so‘raymiz.
2. Agar tanla=1 bo‘lsa, 3-qadamga, 2 ga teng bo‘lsa, 4qadamga, 3 tanlansa, 6-qadamga o‘tish, 0 tanlansa dasturni
yakunlash.
3. Navbatdagi elementni yaratish p; (p ning info maydoniga
qiymat so‘rab olib yozish va ptr maydoniga NULL yozish) Agar
ro‘yhat boshi ko‘rsatkichi lst=NULL bo‘lsa, lst=p va last=p; aks
holda last – ro‘yhat oxirgi elementi ptr maydoniga p ni yozib, p
elementni last qilib belgilaymiz. 1-qadamga o‘tamiz.
4. Agar lst NULL ga teng bo‘lsa, ro‘yhat bo‘shligini ekranga
chiqarib,
Yechim:
#include&lt;bits/stdc++.h&gt;
using namespace std;
struct node
{
int key;
struct node *left;
struct node *right;
};
node* removeOutsideRange(node *root, int max)
{
if (root == NULL)
return NULL;
root-&gt;left = removeOutsideRange(root-&gt;left, max);
root-&gt;right = removeOutsideRange(root-&gt;right, max);
if (root-&gt;key &gt; max)
{
node *lChild = root-&gt;left;
delete root;
return lChild;
}
return root;
}
node* newNode(int num)
{
node* temp = new node;
temp-&gt;key = num;
temp-&gt;left = temp-&gt;right = NULL;
return temp;
}
node* insert(node* root, int key)
{
if (root == NULL)
return newNode(key);
if (root-&gt;key &gt; key)
root-&gt;left = insert(root-&gt;left, key);
else
root-&gt;right = insert(root-&gt;right, key);
return root;
}
void inorderTraversal(node* root)
{
if (root)
{
inorderTraversal( root-&gt;left );
cout &lt;&lt; root-&gt;key &lt;&lt; &quot; &quot;;
inorderTraversal( root-&gt;right );
}
}
int main()
{
node* root = NULL;
root = insert(root, 6);
root = insert(root, 14);
root = insert(root, 8);
root = insert(root, 15);
root = insert(root, 13);
root = insert(root, 7);
cout &lt;&lt; &quot;Inorder traversal of the given tree is: &quot;;
inorderTraversal(root);
root = removeOutsideRange(root, 13);
cout &lt;&lt; &quot;\nInorder traversal of the modified tree is: &quot;;
inorderTraversal(root);
return 0;
}
```