Uploaded by Shoxrux Umarov

Labaratoriya ish C++

advertisement
O’zbekiston Respublikasi Axborot Texnologiyalari
va Kommunikatsiyalarini Rivojlantirish Vazirligi
Muhammad Al-Xorazmiy nomidagi
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 < row; i++)
{
for (j = 0; j < col; j++)
{
if (i+j==3||i+j==4)
{
cout << "0" << " ";
}
else
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}
2. Navbat eng katta elementi topilsin undan keyin 0 joylashtrilsin.
Dastur kodi:
#include <iostream>
#include <queue>
using namespace std;
int main()
{
queue <int> qu;
queue <int> qu2;
int n;
cout<<"Navbat elementlari sonini kiriting:";
cin >> n;
cout<<"Navbat elementlarini kiriting:";
for(int i=0, a; i<n; i++)
{
cin >> a;
qu.push(a);
qu2.push(a);
}
const int& k = qu2.front();
int min = k;
for(int i=0; i<n; i++)
{
if(qu2.front() < min) min = qu2.front();
qu2.pop();
}
cout << "min=" << min << endl;
for(int i=0; i<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<n; i++)
{
cout << qu2.front() << " "; qu2.pop();
}
}
3. Ro’yhat n-inchi elementidan keyin yangi element qo’yilsin.
Dastur kodi
#include <iostream>
using namespace std;
class Node{
public: int number;
Node* next;
};
int main()
{
Node* head = NULL;
Node* lastPtr = NULL;
short action = -1;
while (1)
{
cout<<"1. element qo‘shish\n";
cout<<"2. ro‘yhatni ko‘rish\n";
cout<<"3. ro‘yhat maksimalini topish\n";
cout<<"0. chiqish\n\n";
cout<<"tanlang: ";
cin>>action;
if (action == 0) {
system("CLS");
break;}
if (action == 1)
{
system("CLS");
Node* ptr = new Node;
int numb = -1;
cout<<"son kiriting: ";
cin>>numb;
ptr->number = numb;
ptr->next = NULL;
if (head == 0)
{
head = ptr;
lastPtr = ptr;
system("CLS");
continue;
}
lastPtr->next = ptr;
lastPtr = ptr;
system("CLS");
continue;
}
if (action == 2){
Node* ptr = NULL;
system("CLS");
if (head == 0)
{
cout<<"\t!!! ro‘yhat bo‘sh !!!\n\n";
system("PAUSE");
system("CLS");
continue;
}
cout<<"* * * * * ro‘yhat * * * * *\n\n";
ptr = head;
while (1) {
cout<<ptr->number<<" ";
if (ptr->next == 0) break;
ptr = ptr->next;
}
cout<<"\n\n";
system("PAUSE");
system("CLS");
continue;
}
if (action == 3)
{
system("CLS");
Node* p = head;
Node* q = new Node;
Node* last = new Node;
int max=p->number; q=head;
while(p){
if(max<p->number){ max=p->number;}
p=p->next;
}
system("CLS");
cout<<"max="<<max;
system("pause");
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 < row; i++)
{
for (j = 0; j < col; j++)
{
if (i+j==3||i+j==4)
{
cout << "0" << " ";
}
else
cout << matrix[i][j] << " ";
}
cout << 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 <iostream>
using namespace std;
int binarySearch(int arr[], int left, int right, int x) {
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == x) {
return mid;
} else if (arr[mid] < 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 << "Please enter 10 elements ASCENDING order" <<
endl;
// for (int i = 0; i < 10; i++) {
// cin >> myarr[i];
// }
cout << "Please enter an element to search" << endl;
cin >> num;
output = binarySearch(myarr, 0, 9, num);
if (output == -1) {
cout << "Bunday no'merdagi avtomobil topilmadi" << endl;
} else {
cout << "Avtomobil joy no'meri: " << output << 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-qadamga o‘tish.
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,
1-qadamga o‘tish. Aks holda, p=lst va 5qadamga o‘tish.
Yechim:
#include<bits/stdc++.h>
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->left = removeOutsideRange(root->left, max);
root->right = removeOutsideRange(root->right, max);
if (root->key > max)
{
node *lChild = root->left;
delete root;
return lChild;
}
return root;
}
node* newNode(int num)
{
node* temp = new node;
temp->key = num;
temp->left = temp->right = NULL;
return temp;
}
node* insert(node* root, int key)
{
if (root == NULL)
return newNode(key);
if (root->key > key)
root->left = insert(root->left, key);
else
root->right = insert(root->right, key);
return root;
}
void inorderTraversal(node* root)
{
if (root)
{
inorderTraversal( root->left );
cout << root->key << " ";
inorderTraversal( root->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 << "Inorder traversal of the given tree is: ";
inorderTraversal(root);
root = removeOutsideRange(root, 13);
cout << "\nInorder traversal of the modified tree is: ";
inorderTraversal(root);
return 0;
}
Download