Advertisement
JewishCat

SledMatrix

Jan 29th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. // SledMatrix.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <iomanip>
  7. #include <cmath>
  8.  
  9. using namespace std;
  10.  
  11. double random(double min, double max)
  12. {
  13.     return (double)(rand()) / RAND_MAX*(max - min) + min;
  14. }
  15.  
  16. double SledOfMatrix(double **Matrix, int stolbci, int stroki)
  17. {
  18.     double sum = 0;
  19.     for (int i = 0; i < stroki; i++) {
  20.         for (int j = 0; j < stolbci; j++) {
  21.             if (i == j)
  22.                 sum += Matrix[i][j];
  23.         }
  24.     }
  25.     return sum;
  26. }
  27.  
  28. int main()
  29. {
  30.  
  31.     int N = 5;
  32.     srand(time(0));
  33.     cout.precision(2);
  34.     double **Array = new double*[N];
  35.     for (int i = 0; i < N; i++) {
  36.         Array[i] = new double[N];
  37.     }
  38.     for (int y = 0; y<N; y++) {
  39.         for (int x = 0; x < N; x++)
  40.         {
  41.             Array[y][x] = random(0,20);
  42.             cout << fixed << setw(4) << setprecision(2) << Array[y][x] << "  ";
  43.         }
  44.         cout << endl;
  45.     }
  46.    
  47.     cout << fixed << (double)SledOfMatrix(Array, 5, 5) << endl;;
  48.     system("pause");
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement