Matrix89. Дана квадратная матрица порядка M. Обнулить элементы матрицы, лежащие выше побочной диагонали. Условный оператор не использовать.
>Решение 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 | program Matrix89; type matrix = array [1..10,1..10] of integer; var a:matrix; 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 for i:=1 to j do a[i,j-i+1]:=0 for j:=1 to M do begin for i:=1 to M do Write(' : ', a[i,j]); Writeln(' : '); 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 | # include <iostream> # include <windows.h> # include <cmath> # include <iomanip> # include <fstream> # include "IOMatrix.h" using namespace std; int main () { SetConsoleCP(1251); SetConsoleOutputCP(1251); int matr[RMAX][CMAX]; int n; cout << "Введите порядок матрицы: "; cin >> n; input (matr,"Matrix89.txt",n,n); output (matr,n,n); cout << endl; for (int row=0; row<n-1; ++row){ for (int col=0; col<n-1-row; ++col){ matr[row][col]=0; } } output (matr,n,n); 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; } } |
Matrix89.txt
1 2 3 4 | 4 4 4 4 6 6 6 6 7 7 8 8 1 1 1 1 |