Для одномерного и двумерного сразу соединил в один код:
#include <iostream>
using namespace std;
void countElements(int arr[], int size, int& positive, int& negative, int& zero) {
positive = negative = zero = 0;
for (int i = 0; i < size; i++) {
if (arr[i] > 0) positive++;
else if (arr[i] < 0) negative++;
else zero++;
}
void countElements(int arr[][3], int rows, int cols, int& positive, int& negative, int& zero) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (arr[i][j] > 0) positive++;
else if (arr[i][j] < 0) negative++;
int main() {
int arr1[] = { 1, 2, -3, 0, 5 };
int arr2[][3] = { { 1, 2, 0 }, { -1, -2, 3 }, { 0, 5, -6 } };
int size1 = sizeof(arr1) / sizeof(arr1[0]);
int rows2 = sizeof(arr2) / sizeof(arr2[0]);
int cols2 = sizeof(arr2[0]) / sizeof(arr2[0][0]);
int positive1, negative1, zero1;
int positive2, negative2, zero2;
countElements(arr1, size1, positive1, negative1, zero1);
countElements(arr2, rows2, cols2, positive2, negative2, zero2);
cout << "One-dimensional array: positive=" << positive1 << ", negative=" << negative1 << ", zero=" << zero1 << endl;
cout << "Two-dimensional array: positive=" << positive2 << ", negative=" << negative2 << ", zero=" << zero2 << endl;
return 0;
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
Для одномерного и двумерного сразу соединил в один код:
#include <iostream>
using namespace std;
void countElements(int arr[], int size, int& positive, int& negative, int& zero) {
positive = negative = zero = 0;
for (int i = 0; i < size; i++) {
if (arr[i] > 0) positive++;
else if (arr[i] < 0) negative++;
else zero++;
}
}
void countElements(int arr[][3], int rows, int cols, int& positive, int& negative, int& zero) {
positive = negative = zero = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (arr[i][j] > 0) positive++;
else if (arr[i][j] < 0) negative++;
else zero++;
}
}
}
int main() {
int arr1[] = { 1, 2, -3, 0, 5 };
int arr2[][3] = { { 1, 2, 0 }, { -1, -2, 3 }, { 0, 5, -6 } };
int size1 = sizeof(arr1) / sizeof(arr1[0]);
int rows2 = sizeof(arr2) / sizeof(arr2[0]);
int cols2 = sizeof(arr2[0]) / sizeof(arr2[0][0]);
int positive1, negative1, zero1;
int positive2, negative2, zero2;
countElements(arr1, size1, positive1, negative1, zero1);
countElements(arr2, rows2, cols2, positive2, negative2, zero2);
cout << "One-dimensional array: positive=" << positive1 << ", negative=" << negative1 << ", zero=" << zero1 << endl;
cout << "Two-dimensional array: positive=" << positive2 << ", negative=" << negative2 << ", zero=" << zero2 << endl;
return 0;
}