Labaratoriya ish
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;
}
Navbat eng katta elementi topilsin undan keyin 0 joylashtrilsin.
Ro’yhat n-inchi elementidan keyin yangi element qo’yilsin.
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;
}
Mashina raqamlari ro’yhati berilgan 345, 368, 876, 945, 564, 387, 230, Binar qidiruvdan foydalanib berilgan raqamli mashina qaysi joyda turganini toping.
Yechim:
#include
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;
}
Daraxt tugunlari haqiqiy sonlar bo’lsin, Yozuv berilgan kalit qiymatidan katta bo’lgan daraxt tugunlarini o’chiruvchi dastur tuzing.
Yechim:
#include
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;
}
Do'stlaringiz bilan baham: |