Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime> // для srand(time(0))
- using namespace std;
- int main()
- {
- setlocale(LC_ALL,"Rus");
- srand(time(0)); // для того чтобы каждый раз случ знач были
- int n, sum1=0, sum2=0;
- int **matrix;
- cout << "Введите число n: "; cin >> n;
- matrix = new int*[n];
- for (int i=0; i<n; i++)
- matrix[i]=new int[n];
- cout << "Изначальная матрица:\n";
- for (int i=0; i<n; i++){
- for(int j=0; j<n; j++) {
- matrix[i][j]=rand()%20-10;
- cout << matrix[i][j]<< " ";
- } cout <<endl;
- }
- for (int i=0; i<n; i++)
- for (int j=0; j<n; j++){
- if (i==j) sum1+=matrix[i][j];
- }
- cout << "Сумма на главной диагонали = " << sum1 << endl;
- for (int i=0; i<n; i++)
- sum2+=matrix[i][n-1-i];
- cout << "Сумма на побочной диагонали = " << sum2;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment