Определить, есть ли в двумерном массиве строка, в которой равное количество положительных и отрицательных элементов Нужна помощь с этим заданием
#include <iostream> using namespace std; int main() { int rows, columns; cin >> rows >> columns; int array[rows][columns]; for (int i = 0; i < rows; i++) { int positive = 0, negative = 0; for (int j = 0; j < columns; j++) { cin >> array[j]; if (array[j] > 0) positive++; else if (array[j] < 0) negative++; } if (positive == negative) { cout << "There is a row with equal number of positive and negative elements in the array" << endl; return 0; } } cout << "There is no row with equal number of positive and negative elements in the array" << endl; return 0; }
#include <iostream> #include <iomanip> using namespace std; int main() { setlocale (LC_ALL, "Russian"); int mas[20][20], x, n, m; cout << "Введите количество строк и столбцов: "; cin >> n >> m; cout << "Введите массив: "; for (int i=0; i<n; i++) for (int j=0; j<m; j++) cin >> mas[i][j]; for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { cout << setw(4) << mas[i][j]; } cout << "\n"; } for (int i=0; i<n; i++) { x=0; for (int j=0; j<m; j++) { if (mas[i][j]==0) x; else if (mas[i][j]>0) x++; else x--; } if (x==0) cout << "Строка с равным количестом отрицательных и положительных элементов: " << i+1 << "\n"; } return 0; } C #include <iostream> #include <iomanip> using namespace std; int main() { setlocale (LC_ALL, "Russian"); int mas[20][20], x, n, m; cout << "Введите количество строк и столбцов: "; cin >> n >> m; cout << "Введите массив: "; for (int i=0; i<n; i++) for (int j=0; j<m; j++) cin >> mas[i][j]; for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { cout << setw(4) << mas[i][j]; } cout << "\n"; } for (int i=0; i<n; i++) { x=0; for (int j=0; j<m; j++) { if (mas[i][j]==0) x; else if (mas[i][j]>0) x++; else x--; } if (x==0) cout << "Строка с равным количестом отрицательных и положительных элементов: " << i+1 << "\n"; } return 0; } (спизжено)
Богатый, вроде с динамическим #include <iostream> #include <iomanip> using namespace std; int main() { setlocale (LC_ALL, "Russian"); int x, n, m; cout << "Введите количество строк и столбцов: "; cin >> n >> m; int **mas = (int **)malloc(n*sizeof(int *)); for(int i = 0; i < n; i++) { mas[i] = (int *)malloc(m*sizeof(int)); } cout << "Введите массив: "; for (int i=0; i<n; i++) for (int j=0; j<m; j++) cin >> mas[i][j]; for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { cout << setw(4) << mas[i][j]; } cout << "\n"; } for (int i=0; i<n; i++) { x=0; for (int j=0; j<m; j++) { if (mas[i][j]==0) x; else if (mas[i][j]>0) x++; else x--; } if (x==0) cout << "Строка с равным количестом отрицательных и положительных элементов: " << i+1 << "\n"; } return 0; } C #include <iostream> #include <iomanip> using namespace std; int main() { setlocale (LC_ALL, "Russian"); int x, n, m; cout << "Введите количество строк и столбцов: "; cin >> n >> m; int **mas = (int **)malloc(n*sizeof(int *)); for(int i = 0; i < n; i++) { mas[i] = (int *)malloc(m*sizeof(int)); } cout << "Введите массив: "; for (int i=0; i<n; i++) for (int j=0; j<m; j++) cin >> mas[i][j]; for (int i=0; i<n; i++) { for (int j=0; j<m; j++) { cout << setw(4) << mas[i][j]; } cout << "\n"; } for (int i=0; i<n; i++) { x=0; for (int j=0; j<m; j++) { if (mas[i][j]==0) x; else if (mas[i][j]>0) x++; else x--; } if (x==0) cout << "Строка с равным количестом отрицательных и положительных элементов: " << i+1 << "\n"; } return 0; }
template<class T> bool foo(T** mas, size_t m, size_t n) { for (size_t i = 0; i < m; ++i) { size_t check = 0; for (size_t j = 0; j < n; ++j) if (mas[i][j] > 0) ++check; else if(mas[i][j] < 0) --check; if (check == 0) return true; } return false; } C template<class T> bool foo(T** mas, size_t m, size_t n) { for (size_t i = 0; i < m; ++i) { size_t check = 0; for (size_t j = 0; j < n; ++j) if (mas[i][j] > 0) ++check; else if(mas[i][j] < 0) --check; if (check == 0) return true; } return false; }