Uploaded by Shodlikbek Zaripboyev

Amaliy ishlar

advertisement
1-Amaliy ish
1-Topshiriq
6. Q 
2 a b  c 2  3 x 2
a  b 2

2 x 2  7a 2b 3  c 2
a 2  8b 3  c 2
#include <iostream>
#include <cmath>
double calculateQ(double a, double b, double c, double x) {
double numerator = pow(2, a + b) + pow(c, 2) + (3 * sqrt(pow(x, 2)));
double denominator = pow((a - b), 2);
double firstPart = numerator / denominator;
double secondPart = (2 * pow(x, 2) - 7 * pow(a, 2) * pow(b, 3)) / (pow(a, 2) + 8
* pow(b, 3) + pow(c, 2));
return firstPart - secondPart;
}
int main() {
std::cout<<"Kongirotov Ilhomjon ";
double a, b, c, x;
std::cout << "a, b, c va x qiymatlarini kiriting: ";
std::cin >> a >> b >> c >> x;
double result = calculateQ(a, b, c, x);
std::cout << "Q = " << result << std::endl;
return 0;
}
2-topshiriq.
6. Asoslari a va b, balandligi h bo`lgan g`ola yuzini toping.
#include <iostream>
double calculateTrapezoidArea(double a, double b, double h) {
return (a + b) * h / 2;
}
int main() {
std::cout<<"Kongirotov Ilhomjon ";
double a, b, h;
std::cout << "Asoslar a va b, balandlik h ni kiriting: ";
std::cin >> a >> b >> h;
double area = calculateTrapezoidArea(a, b, h);
std::cout << "G'ola yuzasi S = " << area << std::endl;
return 0;
}
2-topshiriq
6. Bir o`lchamli sonli massiv elеmеntlarini kvadratlarini yigindisi хisоblansin?
#include <iostream>
#include <vector>
int main() {
std::cout<<"Kongirotov Ilhomjon ";
int n;
std::cout << "Massivning elementlari sonini kiriting: ";
std::cin >> n;
std::vector<int> array(n);
int sumOfSquares = 0;
std::cout << "Massiv elementlarini kiriting: ";
for (int i = 0; i < n; ++i) {
std::cin >> array[i];
sumOfSquares += array[i] * array[i];
}
std::cout << "Massiv elementlarining kvadratlari yig'indisi: " << sumOfSquares
<< std::endl;
return 0;
}
2-Amaliy ish
1-Topshiriq.
6. Arifmetik progressiyani birinchi hadi va ayirmasi berilgan. N ta
hadini yig’indisini hisoblovchi rekursiv funksiya
Kiritish:a = 2, d = 3, n = 5
Natija:40
Izoh:Progressiya hadlari yig’indisi: 2 + 5 + 8 + 11 + 14 = 40.
#include <iostream>
int arithmeticSum(int a, int d, int n) {
if (n == 0) {
return 0;
}
return a + arithmeticSum(a + d, d, n - 1);
}
int main() {
std::cout<<"Kongirotov Ilhomjon ";
int a = 2;
int d = 3;
int n = 5;
int result = arithmeticSum(a, d, n);
std::cout << "Arifmetik progressiya hadlari yig'indisi: " << result << std::endl;
return 0;
}
2-topshiriq.
Keltirilgan topshiriqlarni stek,dek,queue dan foydalangan holda yeching
Berilgan sonlar ketma-ketligida M dan kichik elementlarini
6
kvadratlarini ko’paytmasi hisoblansin.
#include <iostream>
#include <stack>
#include <vector>
int main() {
std::cout<<"Kongirotov Ilhomjon ";
std::stack<int> numbersStack;
int M, n;
std::cout << "Sonlar sonini kiriting: ";
std::cin >> n;
std::cout << "M ni kiriting: ";
std::cin >> M;
std::cout << "Sonlarni kiriting: ";
for (int i = 0; i < n; ++i) {
int num;
std::cin >> num;
numbersStack.push(num);
}
long long product = 1;
bool found = false;
while (!numbersStack.empty()) {
int current = numbersStack.top();
numbersStack.pop();
if (current < M) {
product *= (current * current);
found = true;
}
}
if (found) {
std::cout << "Kvadratlarning ko'paytmasi: " << product << std::endl;
} else {
std::cout << "M dan kichik elementlar topilmadi." << std::endl;
}
return 0;
}
3-Amaliy ish
1-Topshiriq
6. Daraxtda tugunlarni sanash
 Masala: Berilgan daraxtdagi umumiy tugunlar sonini hisoblang.
 Kiritish: [10, 5, 15]
 Chiqish: 3
#include <iostream>
struct TreeNode {
int value;
TreeNode* left;
TreeNode* right;
TreeNode(int val) : value(val), left(nullptr), right(nullptr) {}
};
TreeNode* insert(TreeNode* root, int value) {
if (root == nullptr) {
return new TreeNode(value);
}
if (value < root->value) {
root->left = insert(root->left, value);
} else {
root->right = insert(root->right, value);
}
return root;
}
int countNodes(TreeNode* root) {
if (root == nullptr) {
return 0;
}
return 1 + countNodes(root->left) + countNodes(root->right);
}
int main() {
std::cout<<"Kongirotov Ilhomjon ";
TreeNode* root = nullptr;
int values[] = {10, 5, 15};
for (int value : values) {
root = insert(root, value);
}
int totalNodes = countNodes(root);
std::cout << "Umumiy tugunlar soni: " << totalNodes << std::endl;
return 0;
}
2 – topshiriq
Har bir talaba oʻziga tegishli variantni tanlab uni graf koʻrinishiga oʻtirishi kerak,
hosil boʻlgan grafda oʻzaro eng uzoqda joylashgan tugunlar orasida eng qisqa
yoʻlni Dijkstra usullarida topishi va natijani chiqarishi kerak .
Variant6
070250
703205
030505
225001
500002
055120
#include <iostream>
#include <vector>
#include <climits>
#define V 6
void dijkstra(const std::vector<std::vector<int>>& graph, int start,
std::vector<int>& distances) {
std::vector<bool> visited(V, false);
distances[start] = 0;
for (int count = 0; count < V - 1; ++count) {
int minIndex = -1;
int minValue = INT_MAX;
for (int v = 0; v < V; ++v) {
if (!visited[v] && distances[v] < minValue) {
minValue = distances[v];
minIndex = v;
}
}
visited[minIndex] = true;
for (int v = 0; v < V; ++v) {
if (!visited[v] && graph[minIndex][v] && distances[minIndex] !=
INT_MAX
&& distances[minIndex] + graph[minIndex][v] < distances[v]) {
distances[v] = distances[minIndex] + graph[minIndex][v];
}
}
}
}
void findLongestShortestPath(const std::vector<std::vector<int>>& graph) {
int maxDistance = -1;
int startNode = -1;
int endNode = -1;
for (int i = 0; i < V; ++i) {
std::vector<int> distances(V, INT_MAX);
dijkstra(graph, i, distances);
for (int j = 0; j < V; ++j) {
if (i != j && distances[j] > maxDistance) {
maxDistance = distances[j];
startNode = i;
endNode = j;
}
}
}
std::cout << "Eng uzoqda joylashgan tugunlar: " << startNode << " dan " <<
endNode << " gacha\n";
std::cout << "Eng qisqa yo'lning uzunligi: " << maxDistance << std::endl;
}
int main() {
std::cout<<"Kongirotov Ilhomjon ";
std::vector<std::vector<int>> graph = {
{0, 7, 0, 2, 5, 0},
{7, 0, 3, 2, 0, 5},
{0, 3, 0, 5, 0, 5},
{2, 2, 5, 0, 0, 1},
{5, 0, 0, 0, 0, 2},
{0, 5, 5, 1, 2, 0}
};
findLongestShortestPath(graph);
return 0;
}
Download