Загрузка...

C++ Task on dynamic arrays

Thread in C/C++ created by Папонт Jan 28, 2023. 346 views

  1. Папонт
    Папонт Topic starter Jan 28, 2023 447 Nov 1, 2018
    Определить, есть ли в двумерном массиве строка, в которой равное количество положительных и отрицательных элементов

    Нужна помощь с этим заданием
     
  2. cloudyn
    #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;
    }
     
  3. Богатый
    Богатый Jan 28, 2023 По уши в дерьме, но хотя бы есть чем его хлебать 15,061 Aug 6, 2019
    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;
    }
    (спизжено)
     
    1. Daemon
      Богатый, Тут даже не динамический массив, не говоря о том, что номер строки искать не требовалось.
    2. Богатый
      Богатый,
      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;
      }
  4. Daemon
    Daemon Jan 28, 2023 1680 Jan 8, 2021
    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;
    }
     
Loading...
Top