O’lchami 16 ga teng butun sonli massiv berilgan. Barcha 3 ga karrali elementlarni nol bilan almashtiring. Almashtirishlar sonini aniqlang.
#include
using namespace std;
int main() {
int arr[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
int count = 0;
for (int i = 0; i < 16; i++) {
if (arr[i] % 3 == 0) {
arr[i] = 0;
count++;
}
}
cout << count << " ta element nolga almashtirilgan" << endl;
cout << "Yangi massiv: ";
for (int i = 0; i < 16; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
O’lchami 14 ga teng butun sonli massiv berilgan. Juft o’rinda joylashgan barcha elementlar yig’indisini va qiymatlari toq bo’lgan elementlar ko’paytmasini toping.
#include
using namespace std;
int main() {
int arr[14] = {5, 2, 9, 4, 6, 8, 1, 3, 12, 14, 7, 10, 11, 13};
int sumEven = 0;
int prodOdd = 1;
for (int i = 0; i < 14; i += 2) {
sumEven += arr[i];
}
for (int i = 1; i < 14; i += 2) {
if (arr[i] % 2 != 0) {
prodOdd *= arr[i];
}
}
cout << "Juft o'rinda joylashgan elementlar yig'indisi: " << sumEven << endl;
cout << "Toq sonli elementlar ko'paytmasi: " << prodOdd << endl;
return 0;
}
O’lchami 15 ga teng butun sonli massiv berilgan.musbat elementlar ko’paytmasi va manfiy elementlar ko’paytmasining moduli orasidagi farqni toping.
#include
using namespace std;
int main() {
int arr[15] = {-5, 2, -9, 4, 0, 6, 8, -1, 3, 12, 14, -7, 10, 11, 13};
int sumPos = 0;
int sumNeg = 0;
for (int i = 0; i < 15; i++) {
if (arr[i] > 0) {
sumPos += arr[i];
} else {
sumNeg += arr[i];
}
}
int diff = abs(sumPos) - abs(sumNeg);
cout << "Musbat elementlar ko'paytmasi: " << sumPos << endl;
cout << "Manfiy elementlar ko'paytmasi: " << sumNeg << endl;
cout << "Musbat va manfiy elementlar ko'paytmasining moduli orasidagi farq: " << diff << endl;
return 0;
}
3x4 ikki o’lchamli massivning oxirgi qatori elementlarini o’sish tartibda saralang.
#include
#include
using namespace std;
int main() {
int arr[3][4] = { {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16} };
sort(arr[2], arr[2] + 4);
cout << "Saralangan oxirgi qator elementlari: ";
for (int i = 0; i < 4; i++) {
cout << arr[2][i] << " ";
}
cout << endl;
return 0;
}
7x7 ikki o’lchamli massiv berilgan. Birinchi ustundagi elementlarni kamayish tartibda saralang.
#include
#include
using namespace std;
int main() {
int arr[7][7] = {
{12, 18, 16, 9, 7, 1, 2},
{20, 15, 13, 19, 2, 8, 10},
{5, 6, 14, 17, 21, 23, 3},
{22, 14, 17, 3, 6, 11, 25},
{8, 23, 27, 1, 10, 19, 4},
{24, 26, 7, 11, 22, 5, 15},
{16, 9, 13, 20, 25, 21, 4}
};
sort(arr[0], arr[0] + 7, greater());
cout << "Saralangan birinchi ustundagi elementlar: ";
for (int i = 0; i < 7; i++) {
cout << arr[0][i] << " ";
}
cout << endl;
return 0;
}
5x6 ikki o’lchamli massiv berilgan. Har bir ustunning o’rta arifmetigi topilisin.
#include
using namespace std;
int main() {
int arr[5][6] = {
{12, 18, 16, 9, 7, 1},
{20, 15, 13, 19, 2, 8},
{5, 6, 14, 17, 21, 23},
{22, 14, 17, 3, 6, 11},
{8, 23, 27, 1, 10, 19}
};
double average[5];
for (int i = 0; i < 5; i++) {
double sum = 0;
for (int j = 0; j < 6; j++) {
sum += arr[i][j];
}
average[i] = sum / 6;
}
int rows = 5;
int cols = 1;
int newArr[rows][cols];
for (int i = 0; i < rows; i++) {
newArr[i][0] = int(average[i] + 0.5);
}
cout << "Yangi massiv:\n";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << newArr[i][j] << " ";
}
cout << endl;
}
return 0;
}
5x5 ikki o’lchamli massiv berilgan. Uning diagonallaridagi eng kichik elementni toping.
#include
using namespace std;
int main() {
int arr[5][5] = {
{5, 9, 6, 1, 3},
{8, 7, 12, 5, 2},
{4, 6, 2, 10, 18},
{14, 3, 8, 7, 9},
{19, 5, 11, 8, 4}
};
int minDiagonal = arr[0][0];
for (int i = 1; i < 5; i++) {
if (arr[i][i] < minDiagonal) {
minDiagonal = arr[i][i];
}
}
cout << "Eng kichik diagonal element: " << minDiagonal << endl;
return 0;
}
5x4 massiv berilgan. Uning oxirgi ustunini o’sish tartibda saralang.
#include
#include
using namespace std;
int main() {
int arr[5][4] = {
{5, 9, 6, 1},
{8, 7, 12, 5},
{4, 6, 2, 10},
{14, 3, 8, 7},
{19, 5, 11, 8}
};
sort(arr[0]+3, arr[0]+4);
sort(arr[1]+3, arr[1]+4);
sort(arr[2]+3, arr[2]+4);
sort(arr[3]+3, arr[3]+4);
sort(arr[4]+3, arr[4]+4);
cout << "Saralangan oxirgi ustun: ";
for (int i = 0; i < 5; i++) {
cout << arr[i][3] << " ";
}
cout << endl;
return 0;
}
4x3 massiv berilga. Uning 2 chi va 3 chi qatorlaridagi eng katta elementlar o’rinlarini almashtiring.
#include
#include
using namespace std;
int main() {
int arr[4][3] = {
{5, 9, 6},
{8, 7, 12},
{4, 16, 2},
{14, 3, 8}
};
int maxInRow2 = *max_element(arr[1], arr[1]+3);
int maxInRow3 = *max_element(arr[2], arr[2]+3);
int maxIndex2 = distance(arr[1], find(arr[1], arr[1]+3, maxInRow2));
int maxIndex3 = distance(arr[2], find(arr[2], arr[2]+3, maxInRow3));
swap(arr[1][maxIndex2], arr[2][maxIndex3]);
cout << "Yangi massiv:\n";
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 3; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
return 0;
}
Do'stlaringiz bilan baham: |