
С++ Дана квадратна матриця A порядку M. Знайти суму елементів її головної діагоналі.


Ответы на вопрос

Ответ:
#include <bits/stdc++.h>
using namespace std;
long long m,a[100][100],r;
int main()
{
cin >>m;
for (int i=0;i<m;i++)
{
for (int j=0;j<m;j++)
{
cin >>a[i][j];
}
}
for (int i=m-1;i>=0;i--)
{
r=r+a[i][i];
}
cout <<r;
return 0;
}
Объяснение:
-



Відповідь:
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
void randarr(int **arr,int M){
for(int i = 0; i < M; i++){
for(int j = 0; j < M; j++){
arr[i][j] = rand() % 10 + 1;
}
}
}
void printarr(int **arr,int M){
for(int i = 0; i < M; i++){
for(int j = 0; j < M; j++){
cout << arr[i][j] << "\t";
}
cout << endl;
}
}
int sumdiag(int **arr,int M){
int sum = 0;
for(int i = 0; i < M; i++){
sum += arr[i][i];
}
return sum;
}
int main(){
srand(time(NULL));
setlocale(LC_ALL , "Ukrainian");
int M;
cout << "Введiть порядок M: ";
cin >> M;
int **arr = new int *[M];
for (int i = 0; i < M; i++)
arr[i]=new int[M];
randarr(arr,M);
printarr(arr,M);
cout << "\nСума всiх елементiв головної дiагоналi = " << sumdiag(arr,M) << endl;
for (int i = 0; i < M; i++)
delete[]arr[i];
delete[]arr;
return 0;
}



Finding the Sum of Elements on the Main Diagonal of a Square Matrix
To find the sum of elements on the main diagonal of a square matrix, you need to add up the elements that are located on the diagonal running from the top left to the bottom right of the matrix.
Let's assume we have a square matrix A of order M. To find the sum of elements on its main diagonal, you can follow these steps:
1. Initialize a variable `sum` to 0. 2. Iterate over the elements of the main diagonal of the matrix. 3. Add each element to the `sum` variable. 4. After iterating over all the elements on the main diagonal, the value of `sum` will be the sum of the elements on the main diagonal.
Here's an example of how you can implement this algorithm in Python:
```python def sum_main_diagonal(matrix): n = len(matrix) # Assuming the matrix is square diagonal_sum = 0 for i in range(n): diagonal_sum += matrix[i][i] return diagonal_sum ```
In this code, `matrix` is the input square matrix, and `n` is the order of the matrix. The `for` loop iterates over the elements on the main diagonal, and each element is added to the `diagonal_sum` variable. Finally, the function returns the sum of the elements on the main diagonal.
Please note that the code assumes the matrix is represented as a list of lists, where each inner list represents a row of the matrix.
I hope this helps! Let me know if you have any further questions.


Топ вопросов за вчера в категории Информатика







Последние заданные вопросы в категории Информатика
-
Математика
-
Литература
-
Алгебра
-
Русский язык
-
Геометрия
-
Английский язык
-
Химия
-
Физика
-
Биология
-
Другие предметы
-
История
-
Обществознание
-
Окружающий мир
-
География
-
Українська мова
-
Информатика
-
Українська література
-
Қазақ тiлi
-
Экономика
-
Музыка
-
Право
-
Беларуская мова
-
Французский язык
-
Немецкий язык
-
МХК
-
ОБЖ
-
Психология
-
Физкультура и спорт
-
Астрономия
-
Кыргыз тили
-
Оʻzbek tili