С++ Дана матрица А(n x m). Создать функцию, в которой получить из матрицы новый массив, каждый элемент которого равен 1 , если количество положительных элементов в соответствующей строке больше отрицательных и -1 в противном случае. В новом массиве с помощью функции найти количество элементов до максимального значения.
void countPosNeg(int* A, int n, int m, int result) { for (int i = 0; i < n; i++) { int positiveCount = 0; int negativeCount = 0; // Подсчитываем количество положительных и отрицательных элементов в строке for (int j = 0; j < m; j++) { if (A[i][j] > 0) { positiveCount++; } else if (A[i][j] < 0) { negativeCount++; } } // Записываем значение в новый массив result[i] = (positiveCount > negativeCount) ? 1 : -1; } }
int maxIndex(int* arr, int n) { int maxVal = arr[0]; int maxIndex = 0; // Находим максимальное значение и его индекс в массиве for (int i = 1; i < n; i++) { if (arr[i] > maxVal) { maxVal = arr[i]; maxIndex = i; } } return maxIndex; }
int main() { int n, m; cout << "Enter the number of rows and columns: "; cin >> n >> m;
int** A = new int*[n]; for (int i = 0; i < n; i++) { A[i] = new int[m]; cout << "Enter elements of row " << i + 1 << ": "; for (int j = 0; j < m; j++) { cin >> A[i][j]; } } int* result = new int[n]; countPosNeg(A, n, m, result); int maxIdx = maxIndex(result, n); cout << "Index of the row with the maximum value in the new array: " << maxIdx << endl; for (int i = 0; i < n; i++) { delete[] A[i]; } delete[] A; delete[] result; return 0;
using namespace std;
void countPosNeg(int* A, int n, int m, int result) {
for (int i = 0; i < n; i++) {
int positiveCount = 0;
int negativeCount = 0;
// Подсчитываем количество положительных и отрицательных элементов в строке
for (int j = 0; j < m; j++) {
if (A[i][j] > 0) {
positiveCount++;
} else if (A[i][j] < 0) {
negativeCount++;
}
}
// Записываем значение в новый массив
result[i] = (positiveCount > negativeCount) ? 1 : -1;
}
}
int maxIndex(int* arr, int n) {
int maxVal = arr[0];
int maxIndex = 0;
// Находим максимальное значение и его индекс в массиве
for (int i = 1; i < n; i++) {
if (arr[i] > maxVal) {
maxVal = arr[i];
maxIndex = i;
}
}
return maxIndex;
}
int main() {
int** A = new int*[n];int n, m;
cout << "Enter the number of rows and columns: ";
cin >> n >> m;
for (int i = 0; i < n; i++) {
A[i] = new int[m];
cout << "Enter elements of row " << i + 1 << ": ";
for (int j = 0; j < m; j++) {
cin >> A[i][j];
}
}
int* result = new int[n];
countPosNeg(A, n, m, result);
int maxIdx = maxIndex(result, n);
cout << "Index of the row with the maximum value in the new array: " << maxIdx << endl;
for (int i = 0; i < n; i++) {
delete[] A[i];
}
delete[] A;
delete[] result;
return 0;
}