Uncleeee

Untitled

May 22nd, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime> // для srand(time(0))
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. setlocale(LC_ALL,"Rus");
  8. srand(time(0)); // для того чтобы каждый раз случ знач были
  9. int n, sum1=0, sum2=0;
  10. int **matrix;
  11. cout << "Введите число n: "; cin >> n;
  12.  
  13. matrix = new int*[n];
  14. for (int i=0; i<n; i++)
  15. matrix[i]=new int[n];
  16.  
  17. cout << "Изначальная матрица:\n";
  18. for (int i=0; i<n; i++){
  19. for(int j=0; j<n; j++) {
  20. matrix[i][j]=rand()%20-10;
  21. cout << matrix[i][j]<< " ";
  22. } cout <<endl;
  23. }
  24.  
  25. for (int i=0; i<n; i++)
  26. for (int j=0; j<n; j++){
  27. if (i==j) sum1+=matrix[i][j];
  28. }
  29. cout << "Сумма на главной диагонали = " << sum1 << endl;
  30.  
  31. for (int i=0; i<n; i++)
  32. sum2+=matrix[i][n-1-i];
  33.  
  34. cout << "Сумма на побочной диагонали = " << sum2;
  35. return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment