const n=5; var a:array[1..n,1..n] of integer; x:array[1..n] of double; i,j,k:byte; begin Randomize; Writeln('*** Исходный массив ***'); for i:=1 to n do begin for j:=1 to n do begin a[i,j]:=Random(51)-25; Write(a[i,j]:4) end; Writeln end; Writeln('*** Массив x ***'); for j:=1 to n do begin x[j]:=0; k:=0; for i:=1 to n do if a[i,j] mod 2=0 then begin x[j]:=x[j]+a[i,j]; Inc(k) end; if k>0 then x[j]:=x[j]/k; Write(x[j]:0:5,' ') end; Writeln end.
Відповідь:
#include <iostream>
using namespace std;
int n, i, j, k;
double d, s;
int main()
{
cout << "Poryadok: " << endl;
cin >> n;
double **a = new double *[n];
for (i = 0; i <= n; i++)
a[i] = new double [n];
double **a1 = new double *[n];
for (i = 0; i <= n; i++)
a1[i] = new double [n];
double *b = new double [n];
double *x = new double [n];
cout << "Vvedite koefficienty i svobodnye chleny " << endl;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
cout << "a[ " << i << "," << j << "]= ";
cin >> a[i][j];
a1[i][j] = a[i][j];
}
cout << "b,[ " << i << "]= ";
cin >> b[i];
}
for (k = 1; k <= n; k++) // прямой ход
{
for (j = k + 1; j <= n; j++)
{
d = a[j][k] / a[k][k]; // формула (1)
for (i = k; i <= n; i++)
{
a[j][i] = a[j][i] - d * a[k][i]; // формула (2)
}
b[j] = b[j] - d * b[k]; // формула (3)
}
}
for (k = n; k >= 1; k--) // обратный ход
{
d = 0;
for (j = k + 1; j <= n; j++)
{
s = a[k][j] * x[j]; // формула (4)
d = d + s; // формула (4)
}
x[k] = (b[k] - d) / a[k][k]; // формула (4)
}
cout << "Korni sistemy: " << endl;
for( i = 1; i <= n; i++)
cout << "x[" << i << "]=" << x[i] << " " << endl;
return 0;
}
Пояснення:
const
n=5;
var
a:array[1..n,1..n] of integer;
x:array[1..n] of double;
i,j,k:byte;
begin
Randomize;
Writeln('*** Исходный массив ***');
for i:=1 to n do begin
for j:=1 to n do begin
a[i,j]:=Random(51)-25;
Write(a[i,j]:4)
end;
Writeln
end;
Writeln('*** Массив x ***');
for j:=1 to n do begin
x[j]:=0; k:=0;
for i:=1 to n do
if a[i,j] mod 2=0 then begin
x[j]:=x[j]+a[i,j]; Inc(k)
end;
if k>0 then x[j]:=x[j]/k;
Write(x[j]:0:5,' ')
end;
Writeln
end.
Тестовое решение:
*** Исходный массив ***
-10 18 -8 -15 5
-21 -18 6 -2 9
-7 22 -4 3 14
21 16 -10 -18 -9
17 3 -14 -18 12
*** Массив x ***
-10.00000 9.50000 -6.00000 -12.66667 13.00000