1-amaliyot ishi


Download 28.29 Kb.
bet3/4
Sana22.04.2023
Hajmi28.29 Kb.
#1377296
1   2   3   4
Bog'liq
Asad dasturlash 2

2-amaliyot ishi.
1-topshiriq
Vector M dan kata elementlarini ko`paytmalarini logarifmi hisoblansin.

#include


#include
#include

using namespace std;

int main() {
vector M = {1.0, 2.0, 3.0, 4.0, 5.0};
vector result;

for (int i = 0; i < M.size(); i++) {


double value = log10(M[i]);
result.push_back(value);
}

cout << "M: ";


for (double element : M) {
cout << element << " ";
}

cout << endl << "Result: ";


for (double element : result) {
cout << element << " ";
}

cout << endl;

return 0;
}

Bu kodda, vector tipida M yaratilgan va 1.0, 2.0, 3.0, 4.0, 5.0 qiymatlarini o'z ichiga olgan. M ning har bir elementining logarifmini hisoblash uchun for loop dan foydalanilgan va natijalar vector tipidagi result ning ohiriga qo'shilgan. Natijalar ekranga chiqarilgan.



2-topshiriq
a Vektorning juft qiymatli elementlarini b Vektorga, toq qiymatli elementlarini с Vektorga joylashtiring.

#include


#include

using namespace std;

int main() {
vector a = {1, 2, 3, 4, 5};
vector b;
vector c;

for (int i = 0; i < a.size(); i++) {


if (a[i] % 2 == 0) {
b.push_back(a[i]);
} else {
c.push_back(a[i]);
}
}

cout << "a: ";


for (int element : a) {
cout << element << " ";
}

cout << endl << "b: ";


for (int element : b) {
cout << element << " ";
}

cout << endl << "c: ";


for (int element : c) {
cout << element << " ";
}

cout << endl;

return 0;
}

3-topshiriq
Vektor max elementi bilan k chi elementi o‘rnini almashtiring.

#include


#include
#include

using namespace std;


int main() {


vector a = {10, 20, 30, 40, 50};
int k = 2;

auto it = max_element(a.begin(), a.end());


int max_element_index = distance(a.begin(), it);

if (max_element_index >= k) {


swap(a[max_element_index], a[k]);
}

cout << "a: ";


for (int element : a) {
cout << element << " ";
}

cout << endl;


return 0;


}

Bu kodda, vector tipida a yaratilgan va 10, 20, 30, 40, 50 qiymatlarini o'z ichiga olgan. k esa almashtirish kerak bo'lgan elementning indexini ifodalaydi. max_element funktsiyasi yordamida a ning eng katta elementining indexi aniqlanadi va distance funksiyasi orqali o'rnini topadi. Agar max_element_index k dan katta yoki teng bo'lsa, max_element_index elementi k elementi bilan almashtiriladi. Natijalar ekranga chiqarilgan.





Download 28.29 Kb.

Do'stlaringiz bilan baham:
1   2   3   4




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