Advertisement
kirya_shkolnik

Сумма, количество, произведение положительных и отрицательных элементов

Sep 7th, 2022
854
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6.  
  7. int main()
  8. {
  9.     int n;
  10.     cout << "\nВведите размер массива: ";
  11.     cin >> n;
  12.    
  13.     int matrix[n];
  14.    
  15.    cout << "\nДана массив размером "<< n <<":\n";
  16.    int sumpol = 0, sumotr = 0, countpol = 0, countotr = 0, proizvpol = 1, proizvotr = 1;
  17.    for (int i = 0; i <n; ++i)
  18.    {
  19.         int coef = 1;
  20.         if(rand()%9+1 > 5) coef = -1;
  21.         matrix[i] = (rand()%9+1)*coef;
  22.         cout << matrix[i] << " ";
  23.        
  24.         if(matrix[i] < 0){
  25.             sumotr+=matrix[i];
  26.             countotr++;
  27.             proizvotr*=matrix[i];
  28.         }
  29.         else{
  30.             sumpol+=matrix[i];
  31.             countpol++;
  32.             proizvpol*=matrix[i];
  33.         }
  34.     }
  35.    
  36.     cout << "\n\n1. Сумма положительных элементов: " << sumpol << endl
  37.     << "2. Сумма отрицательных элементов: " << sumotr << endl
  38.     << "3. Количество положительных элементов: " << countpol << endl
  39.     << "4. Количество отрицательных элементов: " << countotr << endl
  40.     << "5. Произведение положительных элементов: " << proizvpol << endl
  41.     << "6. Произведение отрицательных элементов: " << proizvotr << endl;
  42.    
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement