Вопрос задан 16.06.2023 в 07:22. Предмет Информатика. Спрашивает Будкеева Аня.

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

0 0
Перейти к ответам

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

Внимание! Ответы на вопросы дают живые люди. Они могут содержать ошибочную информацию, заблуждения, а также ответы могут быть сгенерированы нейросетями. Будьте внимательны. Если вы уверены, что ответ неверный, нажмите кнопку "Пожаловаться" под ответом.
Отвечает Горбач Кристина.

Ответ:

#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;

}

Объяснение:

-

0 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;

}

0 0
Отвечает нейросеть ChatGpt. Будьте внимательны, ответы нейросети могут содержать ошибочные или выдуманные данные.

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.

0 0

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

Последние заданные вопросы в категории Информатика

Задать вопрос