информатика

Помогите пожалуйста в С++. Дана квадратная матрица А порядка М. Найти сумму элементов ее главной диагонали, то есть диагонали, содержащей следующие элементы: А1,1 , А2,2 , А3,3………. Ам, м. ----нашла в паскаль program Matrix80; type matrix = array [1..10,1..10] of integer; var a:matrix; Sum,M, i, j:Integer; begin Write('M: '); Readln(M); for i:=1 to M do begin writeln(i,': '); for j:=1 to M do begin Write(j,' : '); Read(a[i,j]); end; end; Sum:=0; for i:=1 to M do Sum:=Sum+a[i,i]; Writeln(Sum); end.

Оставить ответ
1

Ответ №1

//Dev-C++ 5.11

#include <iostream>
#include <stdlib.h>
#include <ctime>
using namespace std;

int main(void)
 {const int m=5;
 int Matrix[m][m],sum; sum=0;
 srand(time(NULL));
 cout<<"Matrix"<<endl;
 for(int i=0;i<m;i++)
  {for(int j=0;j<m;j++)
   {Matrix[i][j]=rand()%5;
   if(i==j)sum=sum+Matrix[i][j];
   cout<<Matrix[i][j]<<" ";}
  cout<<endl;}
 cout<<"Sum="<<sum;
 return 0;}

Знаете ответ?