Advertisement
kxcoze

lab8++_??

Mar 27th, 2020
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <time.h>
  4.  
  5. using namespace std;
  6.  
  7. double* createMat(int n, int m) {
  8.     double *matrix = (double *)malloc(n * m * sizeof(double));
  9.     for (int i = 0; i < n; i++) {
  10.         for (int j = 0; j < m; j++) {
  11.             *(matrix+i*m+j) = (double) rand() / RAND_MAX * 20 - 10;
  12.         }
  13.     }
  14.     return matrix;
  15. }
  16.  
  17. void printMat(double *mat, int n, int m) {
  18.     for (int i = 0; i < n; i++) {
  19.         for (int j = 0; j < m; j++) {
  20.             cout << right << fixed << setw(6) << setprecision(2) << *(mat+i*m+j) << ' ';
  21.         }
  22.         cout << '\n';
  23.     }
  24.     cout << '\n';
  25. }
  26.  
  27. int main() {
  28.     setlocale(LC_ALL, "Rus");
  29.     srand(time(NULL));
  30.     int n, m;
  31.     cin >> n >> m;
  32.     double *a = createMat(n, m);
  33.     printMat(a, n, m);
  34.  
  35.     double *b = createMat(n, m);
  36.     printMat(b, n, m);
  37.  
  38.     double *c = (double *)malloc(n * m * sizeof(double));
  39.     for (int i = 0; i < m; i++) {
  40.         int cnt = 0;
  41.         for (int j = 0; j < n; j++) {
  42.             *(c+i+j*m) = max(*(a+i+j*m), *(b+i+j*m));
  43.             if (*(c+i+j*m) > 0) cnt++;
  44.         }
  45.         cout << cnt << ' ';
  46.     }
  47.     cout << "\nANSWER\n";
  48.     printMat(c, n, m);
  49.     free(c);
  50.     free(b);
  51.     free(a);
  52.     system("pause");
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement