Matrix83. Дана квадратная матрица A порядка M. Найти сумму элементов каждой ее диагонали, параллельной побочной (начиная с одноэлементной диагонали A1,1).
>Решение Pascal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | program Matrix83; type matrix = array [1..10,1..10] of integer; var a:matrix; Sum,M, i, j:Integer; begin Write('M: '); Readln(M); for j:=1 to M do begin writeln(j,': '); for i:=1 to M do begin Write(i,' : '); Read(a[i,j]); end; end; for j:=1 to M-1 do begin Sum:=0; for i:=1 to j do Sum:=Sum+a[i,j-i+1]; Writeln(Sum); end; for j:=M-1 downto 1 do begin Sum:=0; for i:=1 to j do Sum:=Sum+a[M-j+i,M-i+1]; Writeln(Sum); end; end. |
Решение C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | # include <iostream> # include <windows.h> # include <cmath> # include <iomanip> # include <fstream> # include "IOMatrix.h" using namespace std; int main () { SetConsoleCP(1251); SetConsoleOutputCP(1251); double A[RMAX][CMAX]; int n; cout << "Введите порядок матрицы: "; cin >> n; input (A,"Matrix83.txt",n,n); output (A,n,n); double sum; for (int row=0; row<2*n-1; ++row) { sum = 0; for (int col=0; col<=row; ++col) { if (col<n && row-col<n) { sum += A[col][row-col]; } } cout << "Сумма элементов " << row << " диагонали,"; cout << " параллельной побочной = " << sum << endl; } system ("pause"); return 0; } |
IOMatrix.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | /* Библиотека для работы с матрицами */ # include <iostream> # include <fstream> using namespace std; const int RMAX=100; const int CMAX=100; void input(double a[][CMAX],const char* name, int& nr, int& nc) { ifstream in (name); //cout << "Введите количество строк и столбцов матрицы: "; //cin >> nr >> nc; for (int row = 0; row < nr; ++row) { for (int col = 0; col < nc; ++col) { in >> a[row][col]; } } in.close(); } void output(const double a[][CMAX],int nr, int nc) { for (int row = 0; row < nr; ++row) { for (int col = 0; col < nc; ++col) { cout << '\t' << a[row][col] << " "; } cout << endl; } } void input(int a[][CMAX],const char* name, int& nr, int& nc) { ifstream in (name); //cout << "Введите количество строк и столбцов матрицы: "; //cin >> nr >> nc; for (int row = 0; row < nr; ++row) { for (int col = 0; col < nc; ++col) { in >> a[row][col]; } } in.close(); } void output(const int a[][CMAX],int nr, int nc) { for (int row = 0; row < nr; ++row) { for (int col = 0; col < nc; ++col) { cout << '\t' << a[row][col] << " "; } cout << endl; } } |
Matrix83.txt
1 2 3 4 5 | 10 20 30 40 50 1 1 1 1 1 5 7 5 7 8 3 3 3 3 3 9 9 9 9 4 |
можете решить на си пожалуйста
можете решить на си пожалуйста